/
feed_controller.go
68 lines (54 loc) · 1.24 KB
/
feed_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
61
62
63
64
65
66
67
68
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/asvins/core/models"
om "github.com/asvins/operations/models"
"github.com/asvins/router/errors"
sm "github.com/asvins/subscription/models"
)
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 handlePatientUpdated(msg []byte) {
var p models.Patient
err := json.Unmarshal(msg, &p)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
}
createFeedEvent(p)
}
func handleBoxCreated(msg []byte) {
var b om.Box
err := json.Unmarshal(msg, &b)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
}
createFeedEvent(b)
}
func handleSubscriptionUpdated(msg []byte) {
var s sm.Subscription
err := json.Unmarshal(msg, &s)
if err != nil {
fmt.Println("[ERROR] ", err.Error())
}
createFeedEvent(s)
}
func createFeedEvent(i interface{}) {
e := models.NewEvent(i)
e.Create(db)
}