func consumeProduct(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Product{} params := r.URL.Query() if err := FillProductIdWithUrlValue(&p, params); err != nil { return errors.BadRequest(err.Error()) } qt, err := strconv.Atoi(params.Get("quantity")) if err != nil { return errors.BadRequest(err.Error()) } if err := p.Consume(db, qt); err != nil { return errors.InternalServerError(err.Error()) } ps, err := p.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(ps) != 1 { return errors.InternalServerError("[ERROR] Unexpected error occured during exeuction of /api/inventory/product/:id/consume/:quantity") } rend.JSON(w, http.StatusOK, ps[0]) return nil }
func DefRoutes() *router.Router { r := router.NewRouter() r.Handle("/api/discovery", router.GET, func(w http.ResponseWriter, r *http.Request) errors.Http { fmt.Fprint(w, "NOTIFICATION MODULE - TODO") return nil }, []router.Interceptor{}) r.Handle("/api/mailTest", router.GET, func(w http.ResponseWriter, r *http.Request) errors.Http { m := mailer.Mail{ To: []string{"*****@*****.**"}, Subject: "Está na hora de tomar sua medicação!", Body: mailer.TemplatePackTime, } b, err := json.Marshal(&m) if err != nil { return errors.BadRequest(err.Error()) } producer.Publish("send_mail", b) fmt.Fprintf(w, "[INFO] Email sent") return nil }, []router.Interceptor{}) return r }
func validateRecipe(w http.ResponseWriter, r *http.Request) errors.Http { pres_id, err := strconv.Atoi(r.URL.Query().Get("prescription_id")) if err != nil { return errors.BadRequest(err.Error()) } rcpt := models.FetchReceipt(pres_id, db) if r.ParseForm() != nil { return errors.BadRequest("Invalid input") } if rcpt.UpdateStatus(models.RecipeStringToStatus(r.Form.Get("status")), db) != nil { return errors.NotFound("Not found") } rend.JSON(w, 200, "{}") return nil }
func retreiveOpenOrder(w http.ResponseWriter, r *http.Request) errors.Http { order, err := models.GetOpenOrder(db) if err != nil { return errors.BadRequest(err.Error()) } rend.JSON(w, http.StatusOK, order) return nil }
func updateProduct(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Product{} if err := BuildStructFromReqBody(&p, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := FillProductIdWithUrlValue(&p, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := p.Update(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, p) return nil }
func handleMedicUpdate(w http.ResponseWriter, r *http.Request) errors.Http { m := models.Medic{} if err := BuildStructFromReqBody(&m, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := FillMedicIdWIthUrlValue(&m, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := m.Update(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, m) return nil }
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 updatePurchaseProductOnQuantity(w http.ResponseWriter, r *http.Request) errors.Http { pp := models.PurchaseProduct{} if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } quantity, err := strconv.Atoi(r.URL.Query().Get("quantity")) if err != nil { errors.BadRequest(err.Error()) } if err := pp.UpdateQuantity(db, quantity); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, pp) return nil }
func updatePurchaseProductOnValue(w http.ResponseWriter, r *http.Request) errors.Http { pp := models.PurchaseProduct{} if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } value, err := strconv.ParseFloat(r.URL.Query().Get("value"), 64) if err != nil { errors.BadRequest(err.Error()) } if err := pp.UpdateValue(db, value); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, pp) return nil }
func updateMedication(w http.ResponseWriter, r *http.Request) errors.Http { im := models.IntermediateMedication{} if err := BuildStructFromReqBody(&im, r.Body); err != nil { return errors.BadRequest(err.Error()) } m := im.Medication() if err := FillMedicationIdWIthUrlValue(m, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := m.Update(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, m) return nil }
func retreiveMedication(w http.ResponseWriter, r *http.Request) errors.Http { m := models.Medication{} if err := BuildStructFromQueryString(&m, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } m.Base.Query = r.URL.Query() medications, err := m.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(medications) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, medications) return nil }
func retrievePacks(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Pack{} if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } p.Base.Query = r.URL.Query() packs, err := p.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(packs) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, packs) return nil }
func retrieveBoxes(w http.ResponseWriter, r *http.Request) errors.Http { b := models.Box{} if err := BuildStructFromQueryString(&b, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } b.Base.Query = r.URL.Query() boxes, err := b.Retrieve(db) if err != nil { return errors.BadRequest(err.Error()) } if len(boxes) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, boxes) 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 deleteMedication(w http.ResponseWriter, r *http.Request) errors.Http { m := models.Medication{} if err := FillMedicationIdWIthUrlValue(&m, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := m.Delete(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, m.ID) return nil }
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 insertProduct(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Product{} if err := BuildStructFromReqBody(&p, r.Body); err != nil { return errors.BadRequest(err.Error()) } if err := p.Save(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, p) return nil }
func updateSubscription(w http.ResponseWriter, req *http.Request) errors.Http { subs := Subscription{} if err := BuildStructFromReqBody(&subs, req.Body); err != nil { return errors.BadRequest("[ERROR] Malformed request body") } email := req.URL.Query().Get("email") if email == "" { return errors.BadRequest("[ERROR] email is mandatory") } db := postgres.GetDatabase(DBConfig()) if err := subs.UpdateByEmail(email, db); err != nil { return errors.InternalServerError(err.Error()) } sendSubscriptionUpdated(&subs) rend := render.New() rend.JSON(w, http.StatusOK, subs) return nil }
func validataTreatment(w http.ResponseWriter, r *http.Request) errors.Http { id, err := strconv.Atoi(r.URL.Query().Get("treatment_id")) if err != nil { return errors.BadRequest(err.Error()) } if err := db.Exec("UPDATE treatments SET status = 0 WHERE id = ?", id).Error; err != nil { return errors.InternalServerError(err.Error()) } t := models.Treatment{ID: id} sendTreatmentApproved(&t) return nil }
func cancelOrder(w http.ResponseWriter, r *http.Request) errors.Http { order := models.Order{} if err := FillOrderIdWithUrlValue(&order, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } if err := order.Cancel(db); err != nil { return errors.InternalServerError(err.Error()) } rend.JSON(w, http.StatusOK, order.ID) return nil }
func handleGetFeed(w http.ResponseWriter, req *http.Request) errors.Http { patientId := req.URL.Query().Get("patient_id") from := time.Now().AddDate(0, 0, -1) patientID, err := strconv.Atoi(patientId) if err != nil { return errors.BadRequest("Invalid patient id") } es, err := models.FindFeedEvents(from, patientID, db) if err != nil { return errors.NotFound("No events") } rend.JSON(w, 200, es) 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 insertMedication(w http.ResponseWriter, r *http.Request) errors.Http { im := models.IntermediateMedication{} if err := BuildStructFromReqBody(&im, r.Body); err != nil { return errors.BadRequest(err.Error()) } m := im.Medication() if err := m.Save(db); err != nil { return errors.InternalServerError(err.Error()) } sendProductCreated(m) rend.JSON(w, http.StatusOK, m) return nil }
func retreiveProduct(w http.ResponseWriter, r *http.Request) errors.Http { p := models.Product{} if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } products, err := p.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(products) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, products) return nil }
func retreiveOrderById(w http.ResponseWriter, r *http.Request) errors.Http { o := models.Order{} if err := FillOrderIdWithUrlValue(&o, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } orders, err := o.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(orders) != 1 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, orders[0]) return nil }
func retreivePurchaseProductsById(w http.ResponseWriter, r *http.Request) errors.Http { pp := models.PurchaseProduct{} if err := FillPurchaseProductIdWithUrlValue(&pp, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } pproducts, err := pp.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(pproducts) != 1 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, pproducts[0]) return nil }
func uploadRecipe(w http.ResponseWriter, r *http.Request) errors.Http { prescriptionId := r.URL.Query().Get("prescription_id") file, _, err := r.FormFile("receipt") if err != nil { fmt.Println(err) return errors.BadRequest("Invalid file") } defer file.Close() pid, _ := strconv.Atoi(prescriptionId) rcpt := &models.Receipt{FilePath: "", Status: models.ReceiptStatusUndecided, PrescriptionId: pid} rcpt.Create(db) rcpt.FilePath = "upload/" + prescriptionId + "/" + strconv.Itoa(int(rcpt.ID)) rcpt.Save(db) // ID incremental :'( prescr := models.Prescription{ID: rcpt.PrescriptionId} fmt.Println("[DEBUG] prescri query obj: ", prescr) prescrs, err := prescr.Retreive(db) if err != nil { fmt.Println("[ERROR] ", err.Error()) } fmt.Println("[DEBUG] prescrs retrieved ", prescrs) if prescrs != nil && len(prescrs) >= 1 { fmt.Println("[DEBUG] Saving rcpt.id: ", rcpt.ID, " on prescriptionId: ", prescrs[0].ID) prescrs[0].ReceiptId = rcpt.ID if err := prescrs[0].Update(db); err != nil { fmt.Println("[ERROR] ", err.Error()) } } os.MkdirAll("upload/"+prescriptionId, 0777) out, err := os.Create("upload/" + prescriptionId + "/" + strconv.Itoa(int(rcpt.ID))) fmt.Println(err) _, err = io.Copy(out, file) fmt.Println(err) if err != nil { return errors.InternalServerError("Error uploading image") } rend.JSON(w, 200, rcpt) return nil }
func retreiveWithdrawal(w http.ResponseWriter, r *http.Request) errors.Http { withd := models.Withdrawal{} if err := BuildStructFromQueryString(&withd, r.URL.Query()); err != nil { return errors.BadRequest(err.Error()) } withd.Query = r.URL.Query() ws, err := withd.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(ws) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, ws) return nil }
func retreiveOrder(w http.ResponseWriter, r *http.Request) errors.Http { queryString := r.URL.Query() var o models.Order decoder := decoder.NewDecoder() if err := decoder.DecodeURLValues(&o, queryString); err != nil { return errors.BadRequest(err.Error()) } orders, err := o.Retreive(db) if err != nil { return errors.InternalServerError(err.Error()) } if len(orders) == 0 { return errors.NotFound("record not found") } rend.JSON(w, http.StatusOK, orders) return nil }