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)
|
||||
}
|
||||
|
||||
20
internal/controller/response.go
Normal file
20
internal/controller/response.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func jsonResponse(w http.ResponseWriter, data interface{}, status int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func jsonError(w http.ResponseWriter, message string, status int) {
|
||||
jsonResponse(w, map[string]string{"error": message}, status)
|
||||
}
|
||||
|
||||
func jsonMsg(w http.ResponseWriter, message string) {
|
||||
jsonResponse(w, map[string]string{"msg": message}, http.StatusOK)
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"butterfliu/internal/service"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -20,15 +19,15 @@ func NewSongController(service *service.SongService) *SongController {
|
||||
func (c *SongController) GetAllWithDetails(w http.ResponseWriter, r *http.Request) {
|
||||
songs, err := c.service.GetAllWithDetails()
|
||||
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 *SongController) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
parmaID := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(parmaID)
|
||||
paramID := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(paramID)
|
||||
if err != nil {
|
||||
http.Error(w, "无效的歌曲 ID", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -12,7 +12,7 @@ func NewMediaRepository(db *sql.DB) *MediaRepository {
|
||||
|
||||
func (r *MediaRepository) Get(id int) (MediaFile, error) {
|
||||
var m MediaFile
|
||||
rows, err := r.db.Query("SELECT id, path, library_id FROM media_files WHERE id = $1", id)
|
||||
rows, err := r.db.Query("SELECT id, path, library_id FROM media_files WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return MediaFile{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user