/
patients.go
59 lines (49 loc) · 1.69 KB
/
patients.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
package http
import (
"github.com/dgmann/document-manager/api/internal/datastore"
"github.com/dgmann/document-manager/api/internal/storage"
"github.com/go-chi/chi/v5"
"net/http"
"net/url"
)
type PatientController struct {
records datastore.RecordService
tags datastore.TagService
categories datastore.CategoryService
images storage.ImageService
}
func (controller *PatientController) Router() http.Handler {
r := chi.NewRouter()
r.Get("/{patientId}/tags", controller.Tags)
r.Get("/{patientId}/categories", controller.Categories)
r.Get("/{patientId}/records", controller.Records)
return r
}
func (controller *PatientController) Tags(w http.ResponseWriter, req *http.Request) {
id := URLParamFromContext(req.Context(), "patientId")
tags, err := controller.tags.ByPatient(req.Context(), id)
if err != nil {
NewErrorResponse(w, err, 404).WriteJSON()
return
}
NewResponse(w, tags).WriteJSON()
}
func (controller *PatientController) Categories(w http.ResponseWriter, req *http.Request) {
id := URLParamFromContext(req.Context(), "patientId")
categories, err := controller.categories.FindByPatient(req.Context(), id)
if err != nil {
NewErrorResponse(w, err, 404).WriteJSON()
return
}
NewResponse(w, categories).WriteJSON()
}
func (controller *PatientController) Records(w http.ResponseWriter, req *http.Request) {
id := URLParamFromContext(req.Context(), "patientId")
records, err := controller.records.Query(req.Context(), datastore.NewRecordQuery(datastore.WithPatientId(id)))
if err != nil {
NewErrorResponse(w, err, 400).WriteJSON()
return
}
withUrl := SetURLForRecordList(records, url.URL{Scheme: req.URL.Scheme, Host: req.Host})
NewResponse(w, withUrl).WriteJSON()
}