HEAVY: finish music service migration, tidy up services, more tests
This commit is contained in:
parent
9e311df462
commit
90a671982c
47 changed files with 2698 additions and 902 deletions
55
api/track.go
55
api/track.go
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"arimelody-web/controller"
|
||||
"arimelody-web/errors"
|
||||
"arimelody-web/model"
|
||||
"arimelody-web/model/app"
|
||||
)
|
||||
|
|
@ -26,7 +26,7 @@ func ServeAllTracks(app *app.AppState) http.Handler {
|
|||
var tracks = []Track{}
|
||||
|
||||
var dbTracks = []*model.Track{}
|
||||
dbTracks, err := controller.GetAllTracks(app.DB)
|
||||
dbTracks, err := app.MusicService.GetAllTracks()
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to pull tracks from DB: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
@ -52,7 +52,7 @@ func ServeAllTracks(app *app.AppState) http.Handler {
|
|||
|
||||
func ServeTrack(app *app.AppState, track *model.Track) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
dbReleases, err := controller.GetTrackReleases(app.DB, track.ID, false)
|
||||
dbReleases, err := app.MusicService.GetTrackReleases(track.ID)
|
||||
if err != nil {
|
||||
fmt.Printf("WARN: Failed to pull track releases for %s from DB: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
|
|
@ -78,26 +78,30 @@ func CreateTrack(app *app.AppState) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var track model.Track
|
||||
err := json.NewDecoder(r.Body).Decode(&track)
|
||||
type CreateTrackDTO struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Lyrics string `json:"lyrics"`
|
||||
}
|
||||
var dto CreateTrackDTO
|
||||
err := json.NewDecoder(r.Body).Decode(&dto)
|
||||
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
|
||||
}
|
||||
|
||||
id, err := controller.CreateTrack(app.DB, &track)
|
||||
id, err := app.MusicService.CreateTrack(dto.Title, dto.Description, dto.Lyrics, "")
|
||||
if err != nil {
|
||||
if errors.IsValidationError(err) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to create track: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
app.LogService.Info(model.LOG_MUSIC, "Track \"%s\" (%s) created by \"%s\".", track.Title, track.ID, session.Account.Username)
|
||||
app.LogService.Info(model.LOG_MUSIC, "Track \"%s\" (%s) created by \"%s\".", dto.Title, id, session.Account.Username)
|
||||
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
|
|
@ -114,19 +118,28 @@ func UpdateTrack(app *app.AppState, track *model.Track) http.Handler {
|
|||
|
||||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&track)
|
||||
type UpdateTrackDTO struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Lyrics string `json:"lyrics"`
|
||||
}
|
||||
var dto UpdateTrackDTO
|
||||
err := json.NewDecoder(r.Body).Decode(&dto)
|
||||
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
|
||||
}
|
||||
track.Title = dto.Title
|
||||
track.Description = dto.Description
|
||||
track.Lyrics = dto.Lyrics
|
||||
|
||||
err = controller.UpdateTrack(app.DB, track)
|
||||
err = app.MusicService.UpdateTrack(track)
|
||||
if err != nil {
|
||||
if errors.IsValidationError(err) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to update track %s: %s\n", track.ID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
|
|
@ -154,8 +167,12 @@ func DeleteTrack(app *app.AppState, track *model.Track) http.Handler {
|
|||
session := r.Context().Value("session").(*model.Session)
|
||||
|
||||
var trackID = r.URL.Path[1:]
|
||||
err := controller.DeleteTrack(app.DB, trackID)
|
||||
err := app.MusicService.DeleteTrack(trackID)
|
||||
if err != nil {
|
||||
if errors.IsNotExistError(err) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Printf("WARN: Failed to delete track %s: %s\n", trackID, err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue