-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbooking.go
86 lines (66 loc) · 1.61 KB
/
booking.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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/julienschmidt/httprouter"
)
// Booking Model for our data
type Booking struct {
ID int `json:"id"`
Name string `json:"name"`
Date string `json:"date"`
}
var bookings = []Booking{}
// Function to make a new Booking to the studio
func addNewBooking(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
failureResponse(w, err.Error())
return
}
type newBookingRequestBody struct {
Name *string `json:"name"`
Date *string `json:"date"`
}
var bookingBody newBookingRequestBody
err = json.Unmarshal(body, &bookingBody)
if err != nil {
failureResponse(w, err.Error())
return
}
if bookingBody.Name == nil {
failureResponse(w, `name parameter is not specified or empty`)
return
}
if bookingBody.Date == nil {
failureResponse(w, `Date parameter is not specified or empty`)
return
}
addBooking := Booking{
ID: len(classes) + 1,
Name: *bookingBody.Name,
Date: *bookingBody.Date,
}
bookings = append(bookings, addBooking)
type Output struct {
Message string `json:"message"`
}
var output Output
output.Message = "New Booking added succesfully"
j, err := json.Marshal(output)
if err != nil {
failureResponse(w, `Internal Server Error! `)
return
}
successResponse(w, j)
}
// Function to get all bookings by clients
func allBookings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
bookings, err := json.Marshal(bookings)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(bookings)
}