pcgamedb/server/handler/get_unorganized_games.go
nite07 356a76f5b4 add export cmd
add supplement cmd
rename GameDown to GameItem
2024-11-16 13:48:48 +08:00

67 lines
1.8 KiB
Go

package handler
import (
"net/http"
"github.com/nitezs/pcgamedb/db"
"github.com/nitezs/pcgamedb/model"
"github.com/gin-gonic/gin"
)
type GetUnorganizedGameItemsRequest struct {
Num int `json:"num" form:"num"`
}
type GetUnorganizedGameItemsResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
Size int `json:"size,omitempty"`
GameItems []*model.GameItem `json:"game_downloads,omitempty"`
}
// GetUnorganizedGameItems retrieves a list of unorganized game downloads.
// @Summary List unorganized game downloads
// @Description Retrieves game downloads that have not been organized
// @Tags game
// @Accept json
// @Produce json
// @Param num query int false "Number of game downloads to retrieve"
// @Success 200 {object} GetUnorganizedGameItemsResponse
// @Failure 400 {object} GetUnorganizedGameItemsResponse
// @Failure 500 {object} GetUnorganizedGameItemsResponse
// @Router /game/raw/unorganized [get]
func GetUnorganizedGameItemsHandler(c *gin.Context) {
var req GetUnorganizedGameItemsRequest
if err := c.ShouldBind(&req); err != nil {
c.JSON(http.StatusBadRequest, GetUnorganizedGameItemsResponse{
Status: "error",
Message: err.Error(),
})
return
}
if req.Num == 0 || req.Num < 0 {
req.Num = -1
}
gameDownloads, err := db.GetUnorganizedGameItems(req.Num)
if err != nil {
c.JSON(http.StatusInternalServerError, GetUnorganizedGameItemsResponse{
Status: "error",
Message: err.Error(),
})
return
}
if len(gameDownloads) == 0 {
c.JSON(http.StatusOK, GetUnorganizedGameItemsResponse{
Status: "ok",
Message: "No unorganized game downloads found",
})
return
}
c.JSON(http.StatusOK, GetUnorganizedGameItemsResponse{
Status: "ok",
GameItems: gameDownloads,
Size: len(gameDownloads),
})
}