Example #1
0
// UpdateContact calls the Update method from Contacts via RPC (Contact client). Returns results via jsonapi.
func UpdateContact(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.ContactArgs{Contact: &models.Contact{GroupID: router.Context(r).Env["GroupID"].(uint)}}
	)

	if err := Request(&views.Contact{Contact: args.Contact}, r); err != nil {
		logs.Debug(err)
		Fail(w, r, map[string]interface{}{"contact": err.Error()}, http.StatusBadRequest)
		return
	}
	var coordonnees [2]string = Geoloc(&args)
	if coordonnees[0] == "noresponse" {
		err := errors.New("noresponse")
		Error(w, r, err.Error(), http.StatusBadRequest)
		return
	}
	args.Contact.Address.Latitude = coordonnees[0]
	args.Contact.Address.Longitude = coordonnees[1]

	var (
		contactClient = rpcClient(r, "ContactClient")
		reply         = models.ContactReply{}
	)
	args.Contact.ID = uint(id)

	err = contactClient.Call("Contact.Update", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	rep := models.ContactReply{}
	args.Contact = reply.Contact

	err = contactClient.Call("Search.Index", args, &rep)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}
	Success(w, r, views.Contact{Contact: reply.Contact}, http.StatusOK)
}
Example #2
0
// CreateContact calls the Create method from Contacts via RPC (Contact client) and then calls the Index method from Contacts via RPC (Search client). Returns results via jsonapi.
func CreateContact(w http.ResponseWriter, r *http.Request) {

	var (
		args = models.ContactArgs{Contact: &models.Contact{GroupID: router.Context(r).Env["GroupID"].(uint)}}
		//dataGEO = models.Ban{}
	)

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

	var (
		contactClient = rpcClient(r, "ContactClient")
		reply         = models.ContactReply{}
	)

	var coordonnees [2]string
	coordonnees = Geoloc(&args)
	if coordonnees[0] == "noresponse" {
		err := errors.New("noresponse")
		Error(w, r, err.Error(), http.StatusBadRequest)
		return
	}
	args.Contact.Address.Latitude = coordonnees[0]
	args.Contact.Address.Longitude = coordonnees[1]

	err := contactClient.Call("Contact.Create", args, &reply)
	if err != nil {
		Error(w, r, err.Error(), http.StatusInternalServerError)
		return
	}

	rep := models.ContactReply{}
	args.Contact = reply.Contact

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

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