-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass.go
97 lines (76 loc) · 1.95 KB
/
class.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/julienschmidt/httprouter"
)
// Class Model for our data
type Class struct {
ID int `json:"id"`
Name string `json:"name"`
StartDate string `json:"startdate"`
EndDate string `json:"enddate"`
Capacity string `json:"capacity"`
}
var classes = []Class{}
// Function to add a new class to the studio
func addClass(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
failureResponse(w, err.Error())
return
}
type addClassRequestBody struct {
Name *string `json:"name"`
StartDate *string `json:"startdate"`
EndDate *string `json:"enddate"`
Capacity *string `json:"capacity"`
}
var classBody addClassRequestBody
err = json.Unmarshal(body, &classBody)
if err != nil {
failureResponse(w, err.Error())
return
}
if classBody.Name == nil {
failureResponse(w, `name parameter is not specified or empty`)
return
}
if classBody.StartDate == nil {
failureResponse(w, `startdate parameter is not specified or empty`)
return
}
if classBody.EndDate == nil {
failureResponse(w, `end date parameter is not specified or empty`)
return
}
newClass := Class{
ID: len(classes) + 1,
Name: *classBody.Name,
StartDate: *classBody.StartDate,
EndDate: *classBody.EndDate,
Capacity: *classBody.Capacity,
}
classes = append(classes, newClass)
type Output struct {
Message string `json:"message"`
}
var output Output
output.Message = "New Class created succesfully"
j, err := json.Marshal(output)
if err != nil {
failureResponse(w, `Internal Server Error! `)
return
}
successResponse(w, j)
}
// Function to get all classes in the studio
func listClass(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
classes, err := json.Marshal(classes)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(classes)
}