-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
57 lines (46 loc) · 1.53 KB
/
handlers.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
package parkings
import (
"encoding/json"
"net/http"
"yaroslavl-parkings/data/parking"
)
type parkingsDependencies struct {
datatbase DatabaseInterface
}
// createParkingPlace - creates a parking place, to be later diplayed on the map
func (resource *parkingsDependencies) createParkingPlace(w http.ResponseWriter, r *http.Request) {
// the parsed values are going to be stored here
var parkingPlace parking.ParkingPlace
// parsing the body of json request
err := json.NewDecoder(r.Body).Decode(&parkingPlace)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// storing the new parking place into the database
resource.datatbase.StoreParkingPlace(&parkingPlace)
json.NewEncoder(w).Encode(parkingPlace)
}
// removeParkingByID - removes a parking in the database,
// if no parking id has been provided,
//returns bad request status code
func (resource *parkingsDependencies) removeParkingByID(w http.ResponseWriter, r *http.Request) {
jsonBody := struct {
ID uint `json:"ID"`
}{}
// decoding
err := json.NewDecoder(r.Body).Decode(&jsonBody)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
resource.datatbase.RemoveParkingPlaceByID(jsonBody.ID)
json.NewEncoder(w).Encode(jsonBody)
}
// getAllParkings - returns all parkings places from the database
func (resource *parkingsDependencies) getAllParkings(w http.ResponseWriter, r *http.Request) {
// querying the database
parkingsPlaces := resource.datatbase.GetAllParkingPlaces()
// sending to the user
json.NewEncoder(w).Encode(&parkingsPlaces)
}