arimelody.me/api/track.go

153 lines
4.8 KiB
Go
Raw Normal View History

package api
import (
"encoding/json"
"fmt"
"net/http"
2024-09-03 07:07:45 +00:00
"arimelody-web/global"
music "arimelody-web/music/controller"
"arimelody-web/music/model"
)
2024-09-01 23:15:23 +00:00
type (
Track struct {
2024-09-03 07:07:45 +00:00
*model.Track
Releases []string `json:"releases"`
2024-09-01 23:15:23 +00:00
}
)
func ServeAllTracks() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-09-01 23:15:23 +00:00
type Track struct {
ID string `json:"id"`
Title string `json:"title"`
}
2024-09-01 23:15:23 +00:00
var tracks = []Track{}
2024-09-01 23:15:23 +00:00
var dbTracks = []*model.Track{}
dbTracks, err := music.GetAllTracks(global.DB)
if err != nil {
fmt.Printf("FATAL: Failed to pull tracks from DB: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
2024-09-01 23:15:23 +00:00
for _, track := range dbTracks {
tracks = append(tracks, Track{
ID: track.ID,
Title: track.Title,
})
}
w.Header().Add("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(tracks)
if err != nil {
fmt.Printf("FATAL: Failed to serve all tracks: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
}
2024-09-03 07:07:45 +00:00
func ServeTrack(track *model.Track) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2024-09-03 07:07:45 +00:00
dbReleases, err := music.GetTrackReleases(global.DB, track.ID, false)
if err != nil {
2024-09-01 23:15:23 +00:00
fmt.Printf("FATAL: Failed to pull track releases for %s from DB: %s\n", track.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
2024-09-03 07:07:45 +00:00
releases := []string{}
for _, release := range dbReleases {
releases = append(releases, release.ID)
}
w.Header().Add("Content-Type", "application/json")
2024-09-01 23:15:23 +00:00
err = json.NewEncoder(w).Encode(Track{ track, releases })
if err != nil {
2024-09-01 23:15:23 +00:00
fmt.Printf("FATAL: Failed to serve track %s: %s\n", track.ID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
}
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
}
2024-09-01 23:15:23 +00:00
id, err := music.CreateTrack(global.DB, &track)
if err != nil {
fmt.Printf("FATAL: Failed to create track: %s\n", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusCreated)
2024-09-01 23:15:23 +00:00
w.Write([]byte(id))
})
}
2024-09-03 07:07:45 +00:00
func UpdateTrack(track *model.Track) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut || r.URL.Path == "/" {
http.NotFound(w, r)
return
}
2024-09-01 23:15:23 +00:00
err := json.NewDecoder(r.Body).Decode(&track)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
2024-09-01 23:15:23 +00:00
if track.Title == "" {
http.Error(w, "Track title cannot be empty\n", http.StatusBadRequest)
return
}
2024-09-03 07:07:45 +00:00
err = music.UpdateTrack(global.DB, track)
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
}
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)
}
})
}
2024-09-03 07:07:45 +00:00
func DeleteTrack(track *model.Track) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete || r.URL.Path == "/" {
http.NotFound(w, r)
return
}
var trackID = r.URL.Path[1:]
2024-09-01 23:15:23 +00:00
err := music.DeleteTrack(global.DB, trackID)
if err != nil {
fmt.Printf("Failed to delete track %s: %s\n", trackID, err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
})
}