pcgamedb/server/handler/get_game_by_raw_name.go
nite07 8702d3e93f fix: OrganizeGameItem
fix: save twitchtoken
remove: getSteamID
remove: update cmd&api
add: getIGDBIDBySteamSearch
add: update game info task
2024-11-28 18:37:01 +08:00

70 lines
1.7 KiB
Go

package handler
import (
"net/http"
"pcgamedb/db"
"pcgamedb/model"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
)
type GetGameItemByRawNameRequest struct {
Name string `uri:"name" binding:"required"`
}
type GetGameItemByRawNameResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
GameItem []*model.GameItem `json:"games,omitempty"`
}
// GetGameItemByRawName retrieves game download details by raw name.
// @Summary Retrieve game download by raw name
// @Description Retrieves details of a game download by its raw name
// @Tags game
// @Accept json
// @Produce json
// @Param name path string true "Game Raw Name"
// @Success 200 {object} GetGameItemByRawNameResponse
// @Failure 400 {object} GetGameItemByRawNameResponse
// @Failure 500 {object} GetGameItemByRawNameResponse
// @Router /game/raw/name/{name} [get]
func GetGameItemByRawNameHandler(c *gin.Context) {
var req GetGameItemByRawNameRequest
if err := c.ShouldBindUri(&req); err != nil {
c.JSON(http.StatusBadRequest, GetGameItemByRawNameResponse{
Status: "error",
Message: err.Error(),
})
return
}
gameDownload, err := db.GetGameItemByRawName(req.Name)
if err != nil {
if err == mongo.ErrNoDocuments {
c.JSON(http.StatusOK, GetGameItemByRawNameResponse{
Status: "ok",
Message: "No results found",
})
return
}
c.JSON(http.StatusInternalServerError, GetGameItemByRawNameResponse{
Status: "error",
Message: err.Error(),
})
return
}
if gameDownload == nil {
c.JSON(http.StatusOK, GetGameItemByRawNameResponse{
Status: "ok",
Message: "No results found",
})
return
}
c.JSON(http.StatusOK, GetGameItemByRawNameResponse{
Status: "ok",
GameItem: gameDownload,
})
}