x-movie/server/handler.go
2023-07-08 17:44:51 +08:00

71 lines
1.1 KiB
Go

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)
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
}
var page int = 0
spage := c.Query("page")
if spage != "" {
p, err := strconv.Atoi(spage)
if err != nil {
log.Println(err)
return
}
page = p - 1
}
var limit int = 12
slimit := c.Query("limit")
if slimit != "" {
l, err := strconv.Atoi(slimit)
if err != nil {
log.Println(err)
return
}
limit = l
}
start := page * limit
var end int = start + limit
if start > len(movies) {
start = len(movies)
}
if end > len(movies) {
end = len(movies)
}
response["code"] = http.StatusOK
data["items"] = movies[start:end]
data["total"] = len(movies)
}