-
Notifications
You must be signed in to change notification settings - Fork 2
/
campaigns.go
137 lines (126 loc) Β· 3.31 KB
/
campaigns.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/gocraft/web"
"log"
"github.com/ubyssey/chatbotmgmt/models"
mgo "gopkg.in/mgo.v2"
)
// GET /campaigns
func (reqctx *RequestContext) ListCampaigns(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
campaigns := make([]models.Campaign, 0)
if err := models.GetAllCampaigns(ctx, &campaigns); err != nil {
rw.WriteHeader(500)
return
}
// nil out values we don't care about
for i, _ := range campaigns {
campaigns[i].Nodes = nil
campaigns[i].RootNode = nil
}
j, err := json.Marshal(map[string](interface{}){
"results": campaigns,
})
if err != nil {
log.Print("list campaigns: failed to encode as json: ", err)
rw.WriteHeader(500)
return
}
fmt.Fprint(rw, string(j))
}
// POST /campaigns
func (reqctx *RequestContext) CreateCampaign(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
decoder := json.NewDecoder(req.Body)
var c models.Campaign
if err := decoder.Decode(&c); err != nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "the request body could not be parsed as json or contained an improperly formatted field")
log.Print("create campaign: failed to parse body: ", err)
return
}
if err := c.Save(ctx); err != nil {
switch err.(type) {
case *models.ValidationError:
rw.WriteHeader(400)
fmt.Fprint(rw, err)
default:
rw.WriteHeader(500)
log.Print("create campaign: failed to save: ", err)
}
return
}
cpg_url := fmt.Sprintf("/campaigns/%s", *c.UUID)
rw.Header().Set("Location", cpg_url)
rw.WriteHeader(201)
}
// GET /campaigns/:uuid
func (reqctx *ResourceRequestContext) GetCampaign(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
var c models.Campaign
err := c.GetById(ctx, reqctx.rid)
if err != nil {
if err == mgo.ErrNotFound {
rw.WriteHeader(404)
} else {
rw.WriteHeader(500)
log.Print(err)
}
return
}
j, err := json.Marshal(c)
if err != nil {
log.Print("get campaign: failed to encode as json: ", err)
rw.WriteHeader(500)
return
}
fmt.Fprint(rw, string(j))
}
// PATCH /campaigns/:uuid
func (ctx *ResourceRequestContext) UpdateCampaign(rw web.ResponseWriter, req *web.Request) {
fmt.Fprintf(rw, "PATCH /campaigns/%s", ctx.rid)
}
// DELETE /campaigns/:uuid
func (reqctx *ResourceRequestContext) DeleteCampaign(rw web.ResponseWriter, req *web.Request) {
ctx := context.Background()
decoder := json.NewDecoder(req.Body)
var c models.Campaign
if err := decoder.Decode(&c); err != nil {
rw.WriteHeader(400)
fmt.Fprint(rw, "the request body could not be parsed as json or contained an improperly formatted field")
log.Print("delete campaign: failed to parse body: ", err)
return
}
if c.UUID == nil {
c.UUID = &reqctx.rid
} else {
if *c.UUID != reqctx.rid {
rw.WriteHeader(400)
fmt.Fprint(rw, "the uuid values in the body and url must match")
return
}
}
err := c.Delete(ctx)
if err != nil {
if err == mgo.ErrNotFound {
rw.WriteHeader(404)
return
}
switch err.(type) {
case *models.ValidationError:
rw.WriteHeader(400)
fmt.Fprint(rw, err)
case *models.DependentResourceError:
rw.WriteHeader(412)
fmt.Fprint(rw, err) // TODO this error message should be formatted according to spec
default:
rw.WriteHeader(500)
log.Print("delete campaign: ", err)
}
return
}
rw.WriteHeader(204)
}