pcgamedb/server/handler/search_games.go

77 lines
1.9 KiB
Go
Raw Normal View History

2024-09-24 06:17:11 -04:00
package handler
import (
"net/http"
2024-11-15 02:02:45 -05:00
2024-11-20 06:09:04 -05:00
"pcgamedb/db"
"pcgamedb/model"
2024-09-24 06:17:11 -04:00
"github.com/gin-gonic/gin"
)
type SearchGamesRequest struct {
Keyword string `form:"keyword" json:"keyword" binding:"required,min=1,max=64"`
2024-09-24 06:17:11 -04:00
Page int `form:"page" json:"page"`
PageSize int `form:"page_size" json:"page_size"`
}
type SearchGamesResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
TotalPage int `json:"total_page,omitempty"`
GameInfos []*model.GameInfo `json:"game_infos,omitempty"`
}
// SearchGames searches for games based on a keyword.
// @Summary Search games
// @Description Searches for games based on the provided keyword
// @Tags game
// @Accept json
// @Produce json
// @Param keyword query string true "Search keyword"
// @Param page query int false "Page number"
// @Param page_size query int false "Number of items per page"
// @Success 200 {object} SearchGamesResponse
// @Failure 400 {object} SearchGamesResponse
// @Failure 500 {object} SearchGamesResponse
// @Router /game/search [get]
func SearchGamesHandler(c *gin.Context) {
var req SearchGamesRequest
if err := c.ShouldBind(&req); err != nil {
c.JSON(http.StatusBadRequest, SearchGamesResponse{
Status: "error",
Message: err.Error(),
})
return
}
if req.Page == 0 || req.Page < 0 {
req.Page = 1
}
if req.PageSize == 0 || req.PageSize < 0 {
req.PageSize = 10
}
if req.PageSize > 10 {
req.PageSize = 10
}
items, totalPage, err := db.SearchGameInfos(req.Keyword, req.Page, req.PageSize)
2024-09-24 06:17:11 -04:00
if err != nil {
c.JSON(http.StatusInternalServerError, SearchGamesResponse{
Status: "error",
Message: err.Error(),
})
return
}
if len(items) == 0 {
c.JSON(http.StatusOK, SearchGamesResponse{
Status: "ok",
Message: "No results found",
})
return
}
c.JSON(http.StatusOK, SearchGamesResponse{
Status: "ok",
TotalPage: totalPage,
GameInfos: items,
})
}