func deleteTreatment(w http.ResponseWriter, r *http.Request) errors.Http { t := models.Treatment{} if err := FillTreatmentIdWIthUrlValue(&t, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := t.Delete(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, t.ID) return nil }
func insertTreatment(w http.ResponseWriter, r *http.Request) errors.Http { t := models.Treatment{} if err := BuildStructFromReqBody(&t, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := t.Save(db); err != nil { return errors.InternalServerError(err.Error()) } sendTreatmentCreated(&t) rend.JSON(w, http.StatusOK, t) return nil }
func sendTreatmentCreated(t *models.Treatment) { topic, _ := common_io.BuildTopicFromCommonEvent(common_io.EVENT_CREATED, "treatment") /* * marshal */ patient := models.Patient{} patient.ID = t.PatientId ps, err := patient.Retrieve(db) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } if len(ps) != 1 { fmt.Println("[FATAL] More then one patient with the same ID...") return } t.Email = ps[0].Email b, err := json.Marshal(&t) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } producer.Publish(topic, b) }
func updateTreatment(w http.ResponseWriter, r *http.Request) errors.Http { t := models.Treatment{} if err := BuildStructFromReqBody(&t, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := FillTreatmentIdWIthUrlValue(&t, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := t.Update(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, t) return nil }
func FillTreatmentIdWIthUrlValue(t *models.Treatment, params url.Values) error { id, err := strconv.Atoi(params.Get("id")) if err != nil { return err } t.ID = id return nil }
func retreiveTreatments(w http.ResponseWriter, r *http.Request) errors.Http { t := models.Treatment{} if err := BuildStructFromQueryString(&t, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } t.Base.Query = r.URL.Query() treatments, err := t.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(treatments) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, treatments) return nil }
func handleTreatmentApproved(msg []byte) { t := models.Treatment{} if err := json.Unmarshal(msg, &t); err != nil { fmt.Println("[ERROR] ", err.Error()) return } ts, err := t.Retrieve(db) if err != nil { fmt.Println("[ERROR] ", err.Error()) return } if len(ts) != 1 { fmt.Println("[ERROR] Not able to handle treatment approved msg... query for given id returned len != 1") return } createFeedEvent(ts[0]) }