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 FillPatientIdWIthUrlValue(p *models.Patient, params url.Values) error { id, err := strconv.Atoi(params.Get("id")) if err != nil { return err } p.ID = id return nil }
func handlePatientUpdate(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Patient{} if err := BuildStructFromReqBody(&p, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := FillPatientIdWIthUrlValue(&p, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := p.Update(db); err != nil { return errors.InternalServerError(err.Error()) } sendPatientUpdated(&p) rend.JSON(w, http.StatusOK, p) return nil }
func handlePatientRetrieve(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Patient{} if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } p.Base.Query = r.URL.Query() ps, err := p.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(ps) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, ps) return nil }
/* * Handlers */ func handleUserCreated(msg []byte) { var usr authModels.User err := json.Unmarshal(msg, &usr) if err != nil { fmt.Println("[ERROR] ", err.Error()) } switch usr.Scope { case "patient": p := models.Patient{} p.ID = usr.ID p.Name = usr.FirstName + " " + usr.LastName p.Email = usr.Email err = p.Save(db) createFeedEvent(p) return case "medic": m := models.Medic{} m.ID = usr.ID m.Name = usr.FirstName + " " + usr.LastName m.Email = usr.Email err = m.Create(db) return case "pharmacist": p := models.Pharmacist{} p.ID = usr.ID p.Name = usr.FirstName + " " + usr.LastName p.Email = usr.Email err = p.Save(db) return } if err != nil { fmt.Println("[ERROR] ", err.Error()) } }