/
pharmacist_controller.go
60 lines (46 loc) · 1.22 KB
/
pharmacist_controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"net/http"
"net/url"
"strconv"
"github.com/asvins/core/models"
"github.com/asvins/router/errors"
)
func FillPharmacistIdWIthUrlValue(p *models.Pharmacist, params url.Values) error {
id, err := strconv.Atoi(params.Get("id"))
if err != nil {
return err
}
p.ID = id
return nil
}
func handlePharmacistRetrieve(w http.ResponseWriter, r *http.Request) errors.Http {
p := models.Pharmacist{}
if err := BuildStructFromQueryString(&p, r.URL.Query()); err != nil {
return errors.BadRequest(err.Error())
}
p.Base.Query = r.URL.Query()
pharms, err := p.Retrieve(db)
if err != nil {
return errors.BadRequest(err.Error())
}
if len(pharms) == 0 {
return errors.NotFound("record not found")
}
rend.JSON(w, http.StatusOK, pharms)
return nil
}
func handlePharmacistUpdate(w http.ResponseWriter, r *http.Request) errors.Http {
p := models.Pharmacist{}
if err := BuildStructFromReqBody(&p, r.Body); err != nil {
return errors.BadRequest(err.Error())
}
if err := FillPharmacistIdWIthUrlValue(&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
}