/
handler_pitch.go
135 lines (116 loc) · 4.55 KB
/
handler_pitch.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package theory
import (
"errors"
"net/http"
"strconv"
"github.com/edipermadi/music-db/internal/platform/api"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
type pitchHandlers interface {
GetPitch(writer http.ResponseWriter, request *http.Request)
ListPitchChords(writer http.ResponseWriter, request *http.Request)
ListPitchKeys(writer http.ResponseWriter, request *http.Request)
ListPitchScales(writer http.ResponseWriter, request *http.Request)
ListPitches(writer http.ResponseWriter, request *http.Request)
}
func (h theoryHandler) installPitchEndpoints(router *mux.Router) {
router.HandleFunc("/pitches", h.ListPitches).Methods(http.MethodGet).Name("LIST_PITCHES")
router.HandleFunc("/pitches/{id:[0-9]+}", h.GetPitch).Methods(http.MethodGet).Name("GET_PITCH")
router.HandleFunc("/pitches/{id:[0-9]+}/chords", h.ListPitchChords).Methods(http.MethodGet).Name("LIST_PITCH_CHORDS")
router.HandleFunc("/pitches/{id:[0-9]+}/scales", h.ListPitchScales).Methods(http.MethodGet).Name("LIST_PITCH_SCALES")
router.HandleFunc("/pitches/{id:[0-9]+}/keys", h.ListPitchKeys).Methods(http.MethodGet).Name("LIST_PITCH_KEYS")
}
func (h theoryHandler) GetPitch(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
pitchID, _ := strconv.ParseInt(mux.Vars(request)["id"], 10, 64)
pitch, err := h.service.GetPitch(ctx, pitchID)
switch {
case errors.Is(err, ErrPitchNotFound):
h.ReplyJSON(writer, http.StatusNotFound, api.ErrResourceNotFound)
case err != nil:
h.Logger().With(zap.Error(err)).Error("failed to get pitch")
h.ReplyJSON(writer, http.StatusInternalServerError, api.ErrInternalServer)
default:
h.ReplyJSON(writer, http.StatusOK, pitch)
}
}
func (h theoryHandler) ListPitchChords(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
type params struct {
ChordFilter
api.Pagination
}
var data params
if err := h.decoder.Decode(&data, request.URL.Query()); err != nil {
h.Logger().With(zap.Error(err)).Error("failed to list pitch chords")
h.ReplyJSON(writer, http.StatusBadRequest, api.ErrBadQueryParameter)
return
}
pitchID, _ := strconv.ParseInt(mux.Vars(request)["id"], 10, 64)
pitch, paginationOut, err := h.service.ListPitchChords(ctx, pitchID, data.ChordFilter, data.Pagination)
switch {
case err != nil:
h.Logger().With(zap.Error(err)).Error("failed to list pitch chords")
h.ReplyJSON(writer, http.StatusInternalServerError, api.ErrInternalServer)
default:
h.SetPagination(writer, paginationOut)
h.ReplyJSON(writer, http.StatusOK, pitch)
}
}
func (h theoryHandler) ListPitchKeys(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
type params struct {
KeyFilter
api.Pagination
}
var data params
if err := h.decoder.Decode(&data, request.URL.Query()); err != nil {
h.Logger().With(zap.Error(err)).Error("failed to list pitch keys")
h.ReplyJSON(writer, http.StatusBadRequest, api.ErrBadQueryParameter)
return
}
pitchID, _ := strconv.ParseInt(mux.Vars(request)["id"], 10, 64)
pitch, paginationOut, err := h.service.ListPitchKeys(ctx, pitchID, data.KeyFilter, data.Pagination)
switch {
case err != nil:
h.Logger().With(zap.Error(err)).Error("failed to list pitch keys")
h.ReplyJSON(writer, http.StatusInternalServerError, api.ErrInternalServer)
default:
h.SetPagination(writer, paginationOut)
h.ReplyJSON(writer, http.StatusOK, pitch)
}
}
func (h theoryHandler) ListPitchScales(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
type params struct {
ScaleFilter
api.Pagination
}
var data params
if err := h.decoder.Decode(&data, request.URL.Query()); err != nil {
h.Logger().With(zap.Error(err)).Error("failed to list pitch scales")
h.ReplyJSON(writer, http.StatusBadRequest, api.ErrBadQueryParameter)
return
}
pitchID, _ := strconv.ParseInt(mux.Vars(request)["id"], 10, 64)
pitch, paginationOut, err := h.service.ListPitchScales(ctx, pitchID, data.ScaleFilter, data.Pagination)
switch {
case err != nil:
h.Logger().With(zap.Error(err)).Error("failed to list pitch scales")
h.ReplyJSON(writer, http.StatusInternalServerError, api.ErrInternalServer)
default:
h.SetPagination(writer, paginationOut)
h.ReplyJSON(writer, http.StatusOK, pitch)
}
}
func (h theoryHandler) ListPitches(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
pitches, err := h.service.ListPitches(ctx)
if err != nil {
h.Logger().With(zap.Error(err)).Error("failed to list pitches")
h.ReplyJSON(writer, http.StatusInternalServerError, api.ErrInternalServer)
} else {
h.ReplyJSON(writer, http.StatusOK, pitches)
}
}