-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.go
148 lines (129 loc) · 3.75 KB
/
track.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
136
137
138
139
140
141
142
143
144
145
146
147
148
package handlers
import (
"net/http"
"github.com/porium/porium/models"
log "github.com/sirupsen/logrus"
"github.com/labstack/echo"
)
func (h *Handler) NewTrack(c echo.Context) error {
addr, err := walletAddrFromReq(c)
if err != nil {
log.WithError(err).Error("failed to get user id")
return &echo.HTTPError{
Code: http.StatusForbidden,
Message: "Failed to validate request sender",
}
}
u, err := h.userRepo.GetByWalletAddr(addr)
if err != nil {
log.WithError(err).Error("failed to retrieve user for wallet address")
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Failed to validate request sender",
}
}
if u == nil {
log.WithError(err).WithField("walletAddress", addr).Error("no user for wallet address found")
return &echo.HTTPError{
Code: http.StatusForbidden,
Message: "invalid request sender",
}
}
type newTrackReq struct {
CID string
Title string
ParentTrackID uint
Instruments []string
}
req := new(newTrackReq)
if err := c.Bind(req); err != nil {
log.WithError(err).Error("failed to bind request body to new track request structure")
return &echo.HTTPError{
Code: http.StatusBadRequest,
Message: "failed to process request",
}
}
parentTrack, err := h.trackRepo.GetByTrackID(req.ParentTrackID)
if err != nil {
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "failed to add track information",
}
}
var instruments []models.Instrument
if parentTrack != nil {
instruments = parentTrack.Instruments
log.Info(parentTrack)
}
for _, insName := range req.Instruments {
ins, err := h.instrumentRepo.GetByName(insName)
if err != nil {
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "failed to add track information",
}
} else if ins == nil {
log.Errorln("instrument name not found: ", insName)
i := &models.Instrument{Name: insName}
if err = h.instrumentRepo.Create(i); err != nil {
log.Errorln("failed to add new instrument: ", insName)
continue
}
instruments = append(instruments, *i)
} else {
instruments = append(instruments, *ins)
}
}
t := models.Track{
CID: req.CID,
Instruments: instruments,
Title: req.Title,
ParentTrackID: req.ParentTrackID,
}
if err := h.trackRepo.Create(&t, u); err != nil {
log.WithError(err).Error("failed to persist new track information")
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "failed to store new track information",
}
}
return c.JSON(http.StatusOK, t)
}
func (h *Handler) GetUserFeed(c echo.Context) error {
addr, err := walletAddrFromReq(c)
if err != nil {
log.WithError(err).Error("failed to get user id")
return &echo.HTTPError{
Code: http.StatusForbidden,
Message: "Failed to validate request sender",
}
}
u, err := h.userRepo.GetByWalletAddr(addr)
if err != nil {
log.WithError(err).Error("failed to retrieve user for wallet address")
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Failed to validate request sender",
}
}
if u == nil {
log.WithError(err).WithField("walletAddress", addr).Error("no user for wallet address found")
return &echo.HTTPError{
Code: http.StatusForbidden,
Message: "invalid request sender",
}
}
instruments := make([]string, 0)
for _, instrument := range u.Instruments {
instruments = append(instruments, instrument.Name)
}
tracksFiltered, err := h.trackRepo.GetTracksByInstrument(instruments)
if err != nil {
log.WithError(err).Error("failed to get tracks for instruments")
return &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Failed to get tracks",
}
}
return c.JSON(http.StatusOK, tracksFiltered)
}