This commit is contained in:
@@ -3,7 +3,6 @@ package controller
|
||||
import (
|
||||
"butterfliu/internal/service"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -23,69 +22,84 @@ func NewLibraryController(service *service.LibraryService) *LibraryController {
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetAll(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
libraries, err := c.service.GetAll()
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(libraries)
|
||||
jsonResponse(w, libraries, http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *LibraryController) Create(w http.ResponseWriter, r *http.Request) {
|
||||
var library map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&library)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, err = c.service.Create(library["name"].(string), library["path"].(string))
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
if req.Name == "" || req.Path == "" {
|
||||
jsonError(w, "name and path are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Add library successfully."})
|
||||
_, err := c.service.Create(req.Name, req.Path)
|
||||
if err != nil {
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
jsonMsg(w, "Add library successfully.")
|
||||
}
|
||||
|
||||
func (c *LibraryController) UpdateName(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
name, _ := io.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = c.service.UpdateName(id, string(name))
|
||||
|
||||
var name string
|
||||
if err := json.NewDecoder(r.Body).Decode(&name); err != nil {
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.service.UpdateName(id, name)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Updated library name."})
|
||||
jsonMsg(w, "Updated library name.")
|
||||
}
|
||||
|
||||
func (c *LibraryController) UpdatePath(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
path, _ := io.ReadAll(r.Body)
|
||||
defer r.Body.Close()
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = c.service.UpdatePath(id, string(path))
|
||||
|
||||
var path string
|
||||
if err := json.NewDecoder(r.Body).Decode(&path); err != nil {
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = c.service.UpdatePath(id, path)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Updated library path."})
|
||||
jsonMsg(w, "Updated library path.")
|
||||
}
|
||||
|
||||
func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
||||
c.scanMutex.Lock()
|
||||
if c.isScanning {
|
||||
c.scanMutex.Unlock()
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "Another scan is already in progress"})
|
||||
jsonError(w, "Another scan is already in progress", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
c.isScanning = true
|
||||
@@ -97,7 +111,7 @@ func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
||||
c.scanMutex.Lock()
|
||||
c.isScanning = false
|
||||
c.scanMutex.Unlock()
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -117,56 +131,60 @@ func (c *LibraryController) Scan(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Scan completed successfully for library %d", id)
|
||||
}()
|
||||
|
||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Library scan started."})
|
||||
jsonMsg(w, "Library scan started.")
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetScanStatus(w http.ResponseWriter, r *http.Request) {
|
||||
c.scanMutex.Lock()
|
||||
scanning := c.isScanning
|
||||
c.scanMutex.Unlock()
|
||||
json.NewEncoder(w).Encode(map[string]bool{"scanning": scanning})
|
||||
jsonResponse(w, map[string]bool{"scanning": scanning}, http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *LibraryController) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
id, _ := strconv.Atoi(idParam)
|
||||
err := c.service.Delete(id)
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]string{"msg": "Deleted library."})
|
||||
err = c.service.Delete(id)
|
||||
if err != nil {
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
jsonMsg(w, "Deleted library.")
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetSongs(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
songs, err := c.service.GetSongs(id)
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(songs)
|
||||
jsonResponse(w, songs, http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetArtists(w http.ResponseWriter, r *http.Request) {
|
||||
artists, err := c.service.GetArtists()
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(artists)
|
||||
jsonResponse(w, artists, http.StatusOK)
|
||||
}
|
||||
|
||||
func (c *LibraryController) GetAlbums(w http.ResponseWriter, r *http.Request) {
|
||||
albums, err := c.service.GetAlbums()
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||
jsonError(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(albums)
|
||||
jsonResponse(w, albums, http.StatusOK)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user