x-movie/server/handler.go

230 lines
5.9 KiB
Go
Raw Normal View History

2023-07-04 23:47:01 +08:00
package main
import (
"log"
"net/http"
2025-06-02 06:20:16 +08:00
"sort"
2023-07-04 23:47:01 +08:00
"strconv"
2025-06-02 06:20:16 +08:00
"strings"
2023-07-04 23:47:01 +08:00
"github.com/gin-gonic/gin"
)
2025-06-02 21:29:31 +08:00
// 重命名电影的处理函数
func PostRename(c *gin.Context) {
response := gin.H{
"code": http.StatusInternalServerError,
"message": "An unexpected error occurred.",
}
// 定义请求结构体
type RenameRequest struct {
OldName string `json:"old_name" binding:"required"`
NewName string `json:"new_name" binding:"required"`
}
var req RenameRequest
if err := c.ShouldBindJSON(&req); err != nil {
response["code"] = http.StatusBadRequest
response["message"] = "Invalid request body: " + err.Error()
c.JSON(http.StatusBadRequest, response)
return
}
oldName := req.OldName
newName := req.NewName
if oldName == "" || newName == "" {
response["code"] = http.StatusBadRequest
response["message"] = "Old name and new name are required."
c.JSON(http.StatusBadRequest, response)
return
}
if oldName == newName {
response["code"] = http.StatusBadRequest
response["message"] = "Old name and new name cannot be the same."
c.JSON(http.StatusBadRequest, response)
return
}
MovieDictLock.Lock()
defer MovieDictLock.Unlock()
// 检查新名称是否已存在
if _, exists := MovieDict[newName]; exists {
response["code"] = http.StatusConflict
response["message"] = "New name already exists."
c.JSON(http.StatusConflict, response)
return
}
// 查找旧名称对应的电影
movie, exists := MovieDict[oldName]
if exists {
// 更新电影信息
err := movie.RenameVideo(newName) // 重命名视频文件
if err != nil {
response["code"] = http.StatusInternalServerError
response["message"] = "Failed to rename video file."
c.JSON(http.StatusInternalServerError, response)
return
}
delete(MovieDict, oldName) // 删除旧名称的条目
MovieDict[newName] = movie // 添加新名称的条目
response["code"] = http.StatusOK
response["message"] = "Movie renamed successfully."
response["data"] = gin.H{"old_name": oldName, "new_name": newName}
c.JSON(http.StatusOK, response)
log.Printf("Movie renamed from %s to %s successfully", oldName, newName)
return
}
// 如果旧名称不存在返回404错误
response["code"] = http.StatusNotFound
response["message"] = "Movie not found."
c.JSON(http.StatusNotFound, response)
log.Printf("Movie rename failed: %s not found", oldName)
}
func GetCategoryList(c *gin.Context) {
response := gin.H{
"code": http.StatusInternalServerError,
"message": "An unexpected error occurred.",
"data": []string{},
}
response["code"] = http.StatusOK
response["message"] = "Success"
response["data"] = Categories
c.JSON(http.StatusOK, response)
}
2023-07-04 23:47:01 +08:00
// MovieList 电影列表
func MovieList(c *gin.Context) {
response := gin.H{
"code": http.StatusInternalServerError,
2025-06-02 06:20:16 +08:00
"message": "An unexpected error occurred.",
2025-06-02 03:19:36 +08:00
"data": gin.H{"items": []Movie{}, "total": 0},
2023-07-04 23:47:01 +08:00
}
2025-06-02 21:29:31 +08:00
var moviesToPaginate []*Movie
2025-06-02 06:20:16 +08:00
categoryName := c.Query("category")
searchQuery := c.Query("search") // 获取搜索参数
2025-06-02 03:19:36 +08:00
2025-06-02 06:20:16 +08:00
// === 第一步:基础过滤(类别)===
if categoryName != "" && categoryName != "最新添加" {
// 普通类别:直接过滤
2025-06-02 21:29:31 +08:00
for _, m := range Movies {
2025-06-02 03:19:36 +08:00
if m.TimeCategory == categoryName {
moviesToPaginate = append(moviesToPaginate, m)
}
2023-07-08 17:44:51 +08:00
}
2025-06-02 03:19:36 +08:00
} else {
2025-06-02 06:20:16 +08:00
// 最新添加/所有电影使用全局movies
2025-06-02 21:29:31 +08:00
moviesToPaginate = make([]*Movie, len(Movies))
copy(moviesToPaginate, Movies)
2025-06-02 06:20:16 +08:00
}
// === 第二步:搜索过滤 ===
if searchQuery != "" {
2025-06-02 21:29:31 +08:00
var searchResults []*Movie
2025-06-02 06:20:16 +08:00
lowerSearch := strings.ToLower(searchQuery)
for _, m := range moviesToPaginate {
// 检查文件名是否包含搜索词(不区分大小写)
if strings.Contains(strings.ToLower(m.FileName), lowerSearch) {
searchResults = append(searchResults, m)
}
}
moviesToPaginate = searchResults
2023-07-08 17:44:51 +08:00
}
2025-06-02 06:20:16 +08:00
// === 第三步:排序处理 ===
sortBy := c.Query("sort")
order := c.Query("order")
if sortBy == "" {
sortBy = "created_time" // 默认按创建时间排序
}
// 应用排序
sort.Slice(moviesToPaginate, func(i, j int) bool {
switch sortBy {
case "created_time":
if order == "desc" {
return moviesToPaginate[i].CreatedTime > moviesToPaginate[j].CreatedTime
}
return moviesToPaginate[i].CreatedTime < moviesToPaginate[j].CreatedTime
case "duration":
if order == "desc" {
return moviesToPaginate[i].Duration > moviesToPaginate[j].Duration
}
return moviesToPaginate[i].Duration < moviesToPaginate[j].Duration
default:
// 默认按创建时间降序
return moviesToPaginate[i].CreatedTime > moviesToPaginate[j].CreatedTime
}
})
// === 第四步:分页处理 ===
page := 0
2023-07-04 23:47:01 +08:00
spage := c.Query("page")
if spage != "" {
p, err := strconv.Atoi(spage)
2025-06-02 06:20:16 +08:00
if err != nil || p < 1 {
2025-06-02 03:19:36 +08:00
log.Println("Invalid page number:", spage, err)
response["code"] = http.StatusBadRequest
response["message"] = "Invalid page number. Must be a positive integer."
2025-06-02 06:20:16 +08:00
c.JSON(http.StatusBadRequest, response)
2023-07-08 17:44:51 +08:00
return
2023-07-04 23:47:01 +08:00
}
2025-06-02 06:20:16 +08:00
page = p - 1
2023-07-04 23:47:01 +08:00
}
2025-06-02 06:20:16 +08:00
limit := 12
2023-07-04 23:47:01 +08:00
slimit := c.Query("limit")
if slimit != "" {
l, err := strconv.Atoi(slimit)
2025-06-02 06:20:16 +08:00
if err != nil || l < 1 {
2025-06-02 03:19:36 +08:00
log.Println("Invalid limit number:", slimit, err)
response["code"] = http.StatusBadRequest
response["message"] = "Invalid limit number. Must be a positive integer."
2025-06-02 06:20:16 +08:00
c.JSON(http.StatusBadRequest, response)
2023-07-08 17:44:51 +08:00
return
2023-07-04 23:47:01 +08:00
}
limit = l
}
start := page * limit
2025-06-02 03:19:36 +08:00
end := start + limit
2025-06-02 06:20:16 +08:00
total := len(moviesToPaginate)
2025-06-02 03:19:36 +08:00
2025-06-02 06:20:16 +08:00
// 处理超出范围的情况
if start >= total {
2025-06-02 03:19:36 +08:00
response["data"] = gin.H{
"items": []Movie{},
2025-06-02 06:20:16 +08:00
"total": total,
2025-06-02 03:19:36 +08:00
}
response["code"] = http.StatusOK
response["message"] = "Success"
c.JSON(http.StatusOK, response)
return
}
2023-07-08 17:44:51 +08:00
2025-06-02 06:20:16 +08:00
if end > total {
end = total
2023-07-08 17:44:51 +08:00
}
2025-06-02 03:19:36 +08:00
responseData := gin.H{
"items": moviesToPaginate[start:end],
2025-06-02 06:20:16 +08:00
"total": total,
2023-07-04 23:47:01 +08:00
}
response["code"] = http.StatusOK
2025-06-02 03:19:36 +08:00
response["message"] = "Success"
response["data"] = responseData
c.JSON(http.StatusOK, response)
2023-07-04 23:47:01 +08:00
}