x-movie/server/handler.go

71 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-07-04 15:47:01 +00:00
package main
import (
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
// MovieList 电影列表
func MovieList(c *gin.Context) {
data := gin.H{}
response := gin.H{
"code": http.StatusInternalServerError,
"message": "",
"data": data,
}
defer c.JSON(0, response)
2023-07-08 09:44:51 +00:00
category := c.Query("category")
var movies []Movie
if category != "" {
cateidx, err := strconv.Atoi(category)
if err != nil {
log.Println(err)
return
}
movies = categories[cateidx].Movies
}
2023-07-04 15:47:01 +00:00
var page int = 0
spage := c.Query("page")
if spage != "" {
p, err := strconv.Atoi(spage)
if err != nil {
log.Println(err)
2023-07-08 09:44:51 +00:00
return
2023-07-04 15:47:01 +00:00
}
page = p - 1
}
2023-07-08 09:44:51 +00:00
var limit int = 12
2023-07-04 15:47:01 +00:00
slimit := c.Query("limit")
if slimit != "" {
l, err := strconv.Atoi(slimit)
if err != nil {
log.Println(err)
2023-07-08 09:44:51 +00:00
return
2023-07-04 15:47:01 +00:00
}
limit = l
}
start := page * limit
var end int = start + limit
2023-07-08 09:44:51 +00:00
if start > len(movies) {
start = len(movies)
}
2023-07-04 15:47:01 +00:00
if end > len(movies) {
end = len(movies)
}
response["code"] = http.StatusOK
data["items"] = movies[start:end]
data["total"] = len(movies)
}