feat: add favorites page and web starring

This commit is contained in:
2026-04-03 01:36:43 +03:00
parent b3723b2167
commit 1e6f200433
8 changed files with 322 additions and 19 deletions

View File

@@ -64,6 +64,9 @@ func NewRouter(cfg config.Config, database *sql.DB, scanService *scanner.Service
private.Get("/tracks", application.tracks)
private.Get("/tracks/{id}", application.trackByID)
private.Get("/search", application.search)
private.Get("/favorites", application.favorites)
private.Post("/favorites", application.starFavorites)
private.Delete("/favorites", application.unstarFavorites)
private.Get("/playlists", application.playlistsList)
private.Post("/playlists", application.createPlaylist)
private.Get("/playlists/{id}", application.playlistByID)
@@ -234,6 +237,62 @@ func (a app) search(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, results)
}
func (a app) favorites(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
results, err := a.library.Starred(r.Context(), user.ID)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load favorites"})
return
}
writeJSON(w, http.StatusOK, results)
}
func (a app) starFavorites(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
var payload struct {
TrackIDs []string `json:"trackIds"`
AlbumIDs []string `json:"albumIds"`
ArtistIDs []string `json:"artistIds"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
if err := a.library.Star(r.Context(), user.ID, payload.TrackIDs, payload.AlbumIDs, payload.ArtistIDs); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to star favorites"})
return
}
results, err := a.library.Starred(r.Context(), user.ID)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load favorites"})
return
}
writeJSON(w, http.StatusOK, results)
}
func (a app) unstarFavorites(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
var payload struct {
TrackIDs []string `json:"trackIds"`
AlbumIDs []string `json:"albumIds"`
ArtistIDs []string `json:"artistIds"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
if err := a.library.Unstar(r.Context(), user.ID, payload.TrackIDs, payload.AlbumIDs, payload.ArtistIDs); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to unstar favorites"})
return
}
results, err := a.library.Starred(r.Context(), user.ID)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "failed to load favorites"})
return
}
writeJSON(w, http.StatusOK, results)
}
func (a app) playlistsList(w http.ResponseWriter, r *http.Request) {
user := currentUserFromContext(r)
items, err := a.playlists.List(r.Context(), user.ID)