Exemple #1
0
// CreateForm calls the Create method from Forms via RPC (Form client) and then calls the Index method from Forms via RPC (Search client). Returns results via jsonapi.
func CreateForm(w http.ResponseWriter, r *http.Request) {
	var (
		args = models.FormArgs{Form: &models.Form{GroupID: router.Context(r).Env["GroupID"].(uint)}}
	)

	if err := Request(&views.Form{Form: args.Form}, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"form": err.Error()}, http.StatusBadRequest)
		return
	}

	var (
		formClient = rpcClient(r, "FormClient")
		reply      = models.FormReply{}
	)
	err := formClient.Call("Form.Create", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	//rep := models.FormReply{}
	args.Form = reply.Form

	Success(w, r, views.Form{Form: reply.Form}, http.StatusOK)
}
Exemple #2
0
// UpdateForm calls the Update method from Forms via RPC (Form client). Returns results via jsonapi.
func UpdateForm(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.Atoi(router.Context(r).Param("id"))
	if err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"id": "not integer"}, http.StatusBadRequest)
		return
	}

	var (
		args = models.FormArgs{Form: &models.Form{GroupID: router.Context(r).Env["GroupID"].(uint)}}
	)

	if err := Request(&views.Form{Form: args.Form}, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"form": err.Error()}, http.StatusBadRequest)
		return
	}

	var (
		formClient = rpcClient(r, "FormClient")
		reply      = models.FormReply{}
	)
	args.Form.ID = uint(id)
	err = formClient.Call("Form.Update", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	rep := models.FormReply{}
	args.Form = reply.Form

	err = formClient.Call("Search.Index", args, &rep)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	Success(w, r, views.Form{Form: reply.Form}, http.StatusOK)
}