-
Notifications
You must be signed in to change notification settings - Fork 0
/
adoptee.go
112 lines (90 loc) · 2.42 KB
/
adoptee.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type Adoptee struct {
ID int `json:"id"`
Name string `json:"name"`
Breed string `json:"breed"`
Gender string `json:"gender"`
Age string `json:"age"`
}
func (ar *AnimalRescue) CreateAdoptee(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
adoptee := new(Adoptee)
err := decoder.Decode(adoptee)
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
adoptee.ID = ar.AdopteeSeq()
ar.Adoptees[adoptee.ID] = adoptee
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(adoptee)
}
func (ar *AnimalRescue) GetAdoptee(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.Write([]byte("Adoptee ID not valid"))
return
}
w.Header().Set("Content-Type", "application/json")
adoptee := ar.Adoptees[id]
if adoptee != nil {
payload, _ := json.Marshal(adoptee)
w.Write([]byte(payload))
} else {
w.Write([]byte("Adoptee Not Found"))
}
}
func (ar *AnimalRescue) GetAdoptees(w http.ResponseWriter, r *http.Request) {
adoptees := make([]*Adoptee, 0, len(ar.Adoptees))
for _, adoptee := range ar.Adoptees {
adoptees = append(adoptees, adoptee)
}
json.NewEncoder(w).Encode(adoptees)
}
func (ar *AnimalRescue) UpdateAdoptee(w http.ResponseWriter, r *http.Request) {
// GET adoptee and update fields
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.Write([]byte("Adoptee ID not valid"))
return
}
adoptee := ar.Adoptees[id]
if adoptee == nil {
w.Write([]byte(fmt.Sprintf("Adoptee with ID %d not found", id)))
return
}
decoder := json.NewDecoder(r.Body)
updatedAdoptee := new(Adoptee)
err = decoder.Decode(updatedAdoptee)
if err == nil {
updatedAdoptee.ID = id
*adoptee = *updatedAdoptee
json.NewEncoder(w).Encode(adoptee)
}
}
func (ar *AnimalRescue) DeleteAdoptee(w http.ResponseWriter, r *http.Request) {
// GET adoptee and remove from list stored in memory
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
w.Write([]byte("Adoptee ID not valid"))
return
}
adoptee := ar.Adoptees[id]
if adoptee != nil {
delete(ar.Adoptees, id)
fmt.Fprintf(w, "The adoptee with ID %v has been deleted successfully", id)
} else {
fmt.Fprintf(w, "The adoptee with ID %v was not found", id)
}
}