138 lines
3.3 KiB
Go
138 lines
3.3 KiB
Go
package main
|
||
|
||
import (
|
||
"log"
|
||
"net/http"
|
||
"sort"
|
||
"strconv"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// MovieList 电影列表
|
||
func MovieList(c *gin.Context) {
|
||
response := gin.H{
|
||
"code": http.StatusInternalServerError,
|
||
"message": "An unexpected error occurred.",
|
||
"data": gin.H{"items": []Movie{}, "total": 0},
|
||
}
|
||
|
||
var moviesToPaginate []Movie
|
||
categoryName := c.Query("category")
|
||
searchQuery := c.Query("search") // 获取搜索参数
|
||
|
||
// === 第一步:基础过滤(类别)===
|
||
if categoryName != "" && categoryName != "最新添加" {
|
||
// 普通类别:直接过滤
|
||
for _, m := range movies {
|
||
if m.TimeCategory == categoryName {
|
||
moviesToPaginate = append(moviesToPaginate, m)
|
||
}
|
||
}
|
||
} else {
|
||
// 最新添加/所有电影:使用全局movies
|
||
moviesToPaginate = make([]Movie, len(movies))
|
||
copy(moviesToPaginate, movies)
|
||
}
|
||
|
||
// === 第二步:搜索过滤 ===
|
||
if searchQuery != "" {
|
||
var searchResults []Movie
|
||
lowerSearch := strings.ToLower(searchQuery)
|
||
|
||
for _, m := range moviesToPaginate {
|
||
// 检查文件名是否包含搜索词(不区分大小写)
|
||
if strings.Contains(strings.ToLower(m.FileName), lowerSearch) {
|
||
searchResults = append(searchResults, m)
|
||
}
|
||
}
|
||
moviesToPaginate = searchResults
|
||
}
|
||
|
||
// === 第三步:排序处理 ===
|
||
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
|
||
spage := c.Query("page")
|
||
if spage != "" {
|
||
p, err := strconv.Atoi(spage)
|
||
if err != nil || p < 1 {
|
||
log.Println("Invalid page number:", spage, err)
|
||
response["code"] = http.StatusBadRequest
|
||
response["message"] = "Invalid page number. Must be a positive integer."
|
||
c.JSON(http.StatusBadRequest, response)
|
||
return
|
||
}
|
||
page = p - 1
|
||
}
|
||
|
||
limit := 12
|
||
slimit := c.Query("limit")
|
||
if slimit != "" {
|
||
l, err := strconv.Atoi(slimit)
|
||
if err != nil || l < 1 {
|
||
log.Println("Invalid limit number:", slimit, err)
|
||
response["code"] = http.StatusBadRequest
|
||
response["message"] = "Invalid limit number. Must be a positive integer."
|
||
c.JSON(http.StatusBadRequest, response)
|
||
return
|
||
}
|
||
limit = l
|
||
}
|
||
|
||
start := page * limit
|
||
end := start + limit
|
||
total := len(moviesToPaginate)
|
||
|
||
// 处理超出范围的情况
|
||
if start >= total {
|
||
response["data"] = gin.H{
|
||
"items": []Movie{},
|
||
"total": total,
|
||
}
|
||
response["code"] = http.StatusOK
|
||
response["message"] = "Success"
|
||
c.JSON(http.StatusOK, response)
|
||
return
|
||
}
|
||
|
||
if end > total {
|
||
end = total
|
||
}
|
||
|
||
responseData := gin.H{
|
||
"items": moviesToPaginate[start:end],
|
||
"total": total,
|
||
}
|
||
|
||
response["code"] = http.StatusOK
|
||
response["message"] = "Success"
|
||
response["data"] = responseData
|
||
c.JSON(http.StatusOK, response)
|
||
}
|