90 lines
2.2 KiB
Go
90 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/fujin/music-agregator/internal/services"
|
|
)
|
|
|
|
func (h *Handlers) Sync(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Artist string `json:"artist"`
|
|
Album *string `json:"album,omitempty"`
|
|
Download *bool `json:"download,omitempty"`
|
|
Store *bool `json:"store,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
download := true
|
|
if req.Download != nil {
|
|
download = *req.Download
|
|
}
|
|
store := true
|
|
if req.Store != nil {
|
|
store = *req.Store
|
|
}
|
|
|
|
options := services.SyncOptions{
|
|
Artist: req.Artist,
|
|
Album: req.Album,
|
|
Download: download,
|
|
Store: store,
|
|
}
|
|
|
|
result, err := services.Sync(r.Context(), options, h.MetadataClient, h.IndexerService, h.TorrentService, h.DB)
|
|
if err != nil {
|
|
if _, ok := err.(*services.NotFoundError); ok {
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
return
|
|
}
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, result)
|
|
}
|
|
|
|
func (h *Handlers) AddToBlocklist(w http.ResponseWriter, r *http.Request) {
|
|
if h.DB == nil {
|
|
writeError(w, http.StatusServiceUnavailable, "database not connected")
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
AlbumID string `json:"album_id"`
|
|
SourceTitle string `json:"source_title"`
|
|
GUID *string `json:"guid"`
|
|
TorrentHash *string `json:"torrent_hash"`
|
|
Indexer *string `json:"indexer"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request body")
|
|
return
|
|
}
|
|
|
|
albumID, err := parseUUID(req.AlbumID)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid album_id")
|
|
return
|
|
}
|
|
|
|
artistID, err := h.DB.GetArtistIDByAlbum(r.Context(), albumID)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "album not found")
|
|
return
|
|
}
|
|
|
|
if err := h.DB.AddToBlocklist(r.Context(), *artistID, albumID, req.SourceTitle, req.TorrentHash, req.Indexer); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{
|
|
"added": true,
|
|
})
|
|
}
|