mirror of
https://github.com/truewhile/MeBox.git
synced 2026-09-29 03:26:37 +08:00
44ca451cd4
优化
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
// Package handler — playlist reordering + per-item-id removal that the
|
|
// Vue UI uses on top of the basic /playlists/:id/items surface.
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/ShukeBta/MMTL/internal/model"
|
|
"github.com/ShukeBta/MMTL/internal/service"
|
|
)
|
|
|
|
type reorderReq struct {
|
|
// Order is a list of media IDs in the desired playback order.
|
|
Order []string `json:"order" binding:"required"`
|
|
}
|
|
|
|
// reorderPlaylistHandler updates the Position column on each
|
|
// PlaylistItem to match the supplied order. Items missing from the
|
|
// order keep their existing position.
|
|
func reorderPlaylistHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
var req reorderReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
pid := c.Param("id")
|
|
for i, mid := range req.Order {
|
|
if err := svc.Repo.DB.WithContext(c.Request.Context()).
|
|
Model(&model.PlaylistItem{}).
|
|
Where("playlist_id = ? AND media_id = ?", pid, mid).
|
|
Update("position", i).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|
|
|
|
// deletePlaylistItemByIDHandler is the alternate route at
|
|
// /playlists/:id/items/:item_id (vs. the existing /:media_id variant).
|
|
func deletePlaylistItemByIDHandler(svc *service.Container) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if err := svc.Repo.DB.WithContext(c.Request.Context()).
|
|
Where("playlist_id = ? AND id = ?", c.Param("id"), c.Param("item_id")).
|
|
Delete(&model.PlaylistItem{}).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
}
|