i think that's all the api endpoints!
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
494b29def3
commit
05e16a0867
17 changed files with 810 additions and 231 deletions
157
api/track.go
157
api/track.go
|
@ -10,6 +10,54 @@ import (
|
|||
controller "arimelody.me/arimelody.me/music/controller"
|
||||
)
|
||||
|
||||
func ServeAllTracks() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// type trackJSON struct {
|
||||
// model.Track
|
||||
// Release string `json:"release"`
|
||||
// }
|
||||
// var tracks = []trackJSON{}
|
||||
//
|
||||
// for _, track := range global.Tracks {
|
||||
// for _, release := range global. {
|
||||
// tracks = append(tracks, {
|
||||
// track,
|
||||
// Release
|
||||
// })
|
||||
// }
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(global.Tracks)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeTrack() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
ServeAllTracks().ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err := json.NewEncoder(w).Encode(track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func CreateTrack() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
|
@ -31,7 +79,7 @@ func CreateTrack() http.Handler {
|
|||
|
||||
trackID, err := controller.CreateTrackDB(global.DB, &track)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to create credit: %s\n", err)
|
||||
fmt.Printf("Failed to create track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
@ -44,3 +92,110 @@ func CreateTrack() http.Handler {
|
|||
err = json.NewEncoder(w).Encode(track)
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateTrack() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var data model.Track
|
||||
err := json.NewDecoder(r.Body).Decode(&data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.Error(w, fmt.Sprintf("Track %s does not exist\n", trackID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = trackID
|
||||
|
||||
if data.Title == "" {
|
||||
http.Error(w, "Track title cannot be blank\n", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = controller.UpdateTrackDB(global.DB, &data)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to update track %s: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
track.Title = data.Title
|
||||
track.Description = data.Description
|
||||
track.Lyrics = data.Lyrics
|
||||
track.PreviewURL = data.PreviewURL
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
err = json.NewEncoder(w).Encode(track)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteTrack() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" {
|
||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
var track = global.GetTrack(trackID)
|
||||
if track == nil {
|
||||
http.Error(w, fmt.Sprintf("Track %s does not exist\n", trackID), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := controller.DeleteTrackDB(global.DB, track)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to delete track %s: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// clear track from releases
|
||||
for _, release := range global.Releases {
|
||||
release.Tracks = func () []*model.Track {
|
||||
var tracks = []*model.Track{}
|
||||
for _, t := range release.Tracks {
|
||||
if t.ID == track.ID { continue }
|
||||
tracks = append(tracks, t)
|
||||
}
|
||||
return tracks
|
||||
}()
|
||||
}
|
||||
|
||||
global.Tracks = func () []*model.Track {
|
||||
var tracks = []*model.Track{}
|
||||
for _, t := range global.Tracks {
|
||||
if t.ID == track.ID { continue }
|
||||
tracks = append(tracks, t)
|
||||
}
|
||||
return tracks
|
||||
}()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(fmt.Sprintf("Track %s has been deleted\n", track.ID)))
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue