-
Notifications
You must be signed in to change notification settings - Fork 0
/
pet_preference.go
87 lines (73 loc) · 2.21 KB
/
pet_preference.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type PetPreference struct {
ID int `json:"id"`
Breed string `json:"breed"`
Age string `json:"age"`
Gender string `json:"gender"`
}
func (ar *AnimalRescue) CreatePetPreference(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
petPreference := new(PetPreference)
err := decoder.Decode(petPreference)
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
petPreference.ID = ar.PetPreferenceSeq()
ar.PetPreferences[petPreference.ID] = petPreference
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(petPreference)
}
func (ar *AnimalRescue) GetPetPreferences(w http.ResponseWriter, r *http.Request) {
petPreferences := make([]*PetPreference, 0, len(ar.PetPreferences))
for _, pref := range ar.PetPreferences {
petPreferences = append(petPreferences, pref)
}
json.NewEncoder(w).Encode(petPreferences)
}
func (ar *AnimalRescue) UpdatePetPreference(w http.ResponseWriter, r *http.Request) {
// GET pet_preference and update fields
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.Write([]byte("Pet Preference ID not valid"))
return
}
petPreference := ar.PetPreferences[id]
if petPreference == nil {
w.Write([]byte(fmt.Sprintf("Pet Preference with ID %d not found", id)))
return
}
decoder := json.NewDecoder(r.Body)
updatedPetPreference := new(PetPreference)
err = decoder.Decode(updatedPetPreference)
if err == nil {
updatedPetPreference.ID = id
*petPreference = *updatedPetPreference
json.NewEncoder(w).Encode(petPreference)
}
}
func (ar *AnimalRescue) DeletePetPreference(w http.ResponseWriter, r *http.Request) {
// GET pet_preference and remove from list stored in memory
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.Write([]byte("Pet Preference ID not valid"))
return
}
petPreference := ar.PetPreferences[id]
if petPreference != nil {
delete(ar.PetPreferences, id)
fmt.Fprintf(w, "The pet_preference with ID %v has been deleted successfully", id)
} else {
fmt.Fprintf(w, "The pet_preference with ID %v was not found", id)
}
}