-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbum.go
43 lines (39 loc) · 784 Bytes
/
album.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
package main
import (
"errors"
"net/http"
)
func getAlbums(h httpHelper) {
id, idExists := h.GetQueryParam("id")
if idExists {
getAlbumByID(h, id)
return
} else {
albumsFromDB := readAlbumsFromDB()
h.WriteJson(albumsFromDB)
}
}
func getAlbumByID(h httpHelper, id string) {
result, err := readByIDFromDB(id)
if err != nil {
h.WriteError(httpError{http.StatusNotFound, "album not found"})
} else {
h.WriteJson(result)
}
}
func postAlbums(h httpHelper) {
var newAlbum album
var err error
err = h.BindJSON(&newAlbum)
if err == nil {
success := writeAlbumToDB(newAlbum)
if success {
h.WriteJson(newAlbum)
} else {
err = errors.New("write failed")
}
}
if err != nil {
h.WriteError(httpError{http.StatusInternalServerError, err.Error()})
}
}