// CreateFact calls the Create method from Contacts via RPC (Fact client). Returns results via jsonapi. func CreateFact(w http.ResponseWriter, r *http.Request) { var args = models.FactArgs{Fact: &models.Fact{GroupID: router.Context(r).Env["GroupID"].(uint)}} if err := Request(&views.Fact{Fact: args.Fact}, r); err != nil { logs.Debug(err) Fail(w, r, map[string]interface{}{"fact": err.Error()}, http.StatusBadRequest) return } var ( contactClient = rpcClient(r, "ContactClient") reply = models.FactReply{} ) err := contactClient.Call("Fact.Create", args, &reply) if err != nil { Error(w, r, err.Error(), http.StatusInternalServerError) return } args.Fact = reply.Fact err = contactClient.Call("Search.IndexFact", args, &reply) if err != nil { Error(w, r, err.Error(), http.StatusInternalServerError) return } Success(w, r, views.Fact{Fact: reply.Fact}, http.StatusOK) }
// CreateFacts receive a polygon and retrieves every contact in this polygon. It then calls the CreateFact method for each contact. func CreateFactsByShape(w http.ResponseWriter, r *http.Request) { var sourceArgs = &models.FactsJson{GroupID: router.Context(r).Env["GroupID"].(uint)} if err := Request(sourceArgs, r); err != nil { logs.Debug(err) Fail(w, r, map[string]interface{}{"sourceArgs": err.Error()}, http.StatusBadRequest) return } var ( contactClient = rpcClient(r, "ContactClient") args = models.SearchArgs{} reply = models.SearchReply{} ) args.Search = new(models.Search) args.Search.Polygon = sourceArgs.Points args.Search.Filter = sourceArgs.Filter err := contactClient.Call("Search.SearchIDViaGeoPolygon", args, &reply) if err != nil { logs.Error(err) Error(w, r, err.Error(), http.StatusInternalServerError) return } var ( replyFacts = models.FactReply{} factArgs = models.FactArgs{Fact: &models.Fact{GroupID: sourceArgs.GroupID}} ) DefaultFact := &models.Fact{} DefaultFact = &models.Fact{} DefaultFact.GroupID = sourceArgs.GroupID DefaultFact.Action.Name = sourceArgs.Name DefaultFact.Action.Pitch = sourceArgs.Pitch DefaultFact.Action.TypeData = sourceArgs.TypeData DefaultFact.Status = sourceArgs.Status if reply.IDs != nil { for _, ID := range reply.IDs { factArgs.Fact = DefaultFact factArgs.Fact.ContactID = ID err := contactClient.Call("Fact.Create", factArgs, &replyFacts) if err != nil { Error(w, r, err.Error(), http.StatusInternalServerError) return } if replyFacts.Fact.Contact.ID == 0 { logs.Debug("Contact %d didn't exist. Fact %d may therefore be incorrect", ID, replyFacts.Fact.ID) } factArgs.Fact = replyFacts.Fact err = contactClient.Call("Search.IndexFact", factArgs, &replyFacts) if err != nil { Error(w, r, err.Error(), http.StatusInternalServerError) return } logs.Debug("Fact %d created and indexed", replyFacts.Fact.ID) replyFacts.Facts = append(replyFacts.Facts, *replyFacts.Fact) } } Success(w, r, views.Facts{Facts: replyFacts.Facts}, http.StatusOK) }