-
Notifications
You must be signed in to change notification settings - Fork 0
/
shows.go
153 lines (134 loc) · 3.8 KB
/
shows.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/go-chi/chi"
)
func (b *BaseHandler) createSpace(w http.ResponseWriter, r *http.Request) {
txn := b.db.MustBeginTx(r.Context(), nil)
defer txn.Commit()
var createRequest SpaceCreateRequest
body, err := io.ReadAll(r.Body)
json.Unmarshal(body, &createRequest)
if err != nil {
txn.Rollback()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("could not unmarshal request json"))
b.log.Error(err.Error())
return
}
var entries []Space
for _, area := range createRequest.Areas {
seats, err := json.Marshal(area.Seats)
if err != nil {
continue
}
entries = append(entries, Space{
Name: createRequest.Name,
Section: area.Section,
Seats: seats,
})
}
_, err = txn.NamedExec(`INSERT INTO gyft.spaces (space_nm, space_section_nm, space_section_seats) VALUES (:space_nm, :space_section_nm, :space_section_seats)`, entries)
if err != nil {
txn.Rollback()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error inserting records into db"))
b.log.Error(err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}
func (b *BaseHandler) getSpace(w http.ResponseWriter, r *http.Request) {
spaceName := chi.URLParam(r, "spaceName")
var rows []Space
err := b.db.Select(&rows, `SELECT * FROM gyft.spaces WHERE space_nm=?`, spaceName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error querying db"))
b.log.Error(err.Error())
return
}
resp := SpaceCreateRequest{
Name: spaceName,
}
for _, area := range rows {
var seats []string
err := json.Unmarshal(area.Seats, &seats)
if err != nil {
continue
}
resp.Areas = append(resp.Areas, Area{
Section: area.Section,
Seats: seats,
})
}
respBytes, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error marshaling json response"))
b.log.Error(err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(respBytes)
}
func (b *BaseHandler) updateSpace(w http.ResponseWriter, r *http.Request) {
txn := b.db.MustBeginTx(r.Context(), nil)
defer txn.Commit()
spaceName := chi.URLParam(r, "spaceName")
var createRequest SpaceCreateRequest
body, err := io.ReadAll(r.Body)
json.Unmarshal(body, &createRequest)
if err != nil {
txn.Rollback()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("could not unmarshal request json"))
b.log.Error(err.Error())
return
}
_, err = txn.NamedExec(`DELETE * FROM gyft.spaces WHERE space_nm = $1`, spaceName)
if err != nil {
txn.Rollback()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error accessing db"))
b.log.Error(err.Error())
return
}
var entries []Space
for _, area := range createRequest.Areas {
seats, err := json.Marshal(area.Seats)
if err != nil {
continue
}
entries = append(entries, Space{
Name: createRequest.Name,
Section: area.Section,
Seats: seats,
})
}
_, err = txn.NamedExec(`INSERT INTO gyft.spaces (space_nm, space_section_nm, space_section_seats) VALUES (:space_nm, :space_section_nm, :space_section_seats)`, entries)
if err != nil {
txn.Rollback()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error inserting records into db"))
b.log.Error(err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(body)
}
func (b *BaseHandler) deleteSpace(w http.ResponseWriter, r *http.Request) {
spaceName := chi.URLParam(r, "spaceName")
_, err := b.db.Exec(`DELETE * FROM gyft.spaces WHERE space_nm = $1`, spaceName)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("error accessing db"))
b.log.Error(err.Error())
return
}
w.Write([]byte(fmt.Sprintf(`{"name": "%s"}`, spaceName)))
}