199 lines
5.9 KiB
Go
199 lines
5.9 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"arimelody.me/arimelody.me/global"
|
|
"arimelody.me/arimelody.me/music/model"
|
|
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 {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
var track model.Track
|
|
err := json.NewDecoder(r.Body).Decode(&track)
|
|
if err != nil {
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if track.Title == "" {
|
|
http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
trackID, err := controller.CreateTrackDB(global.DB, &track)
|
|
if err != nil {
|
|
fmt.Printf("Failed to create track: %s\n", err)
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
track.ID = trackID
|
|
global.Tracks = append(global.Tracks, &track)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
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 == "" { data.Title = track.Title }
|
|
|
|
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)))
|
|
})
|
|
}
|