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"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
)
|
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
type GetGameItemByIDRequest struct {
|
2024-09-24 06:17:11 -04:00
|
|
|
ID string `uri:"id" binding:"required"`
|
|
|
|
}
|
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
type GetGameItemByIDResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Game *model.GameItem `json:"game,omitempty"`
|
2024-09-24 06:17:11 -04:00
|
|
|
}
|
|
|
|
|
2024-11-16 00:48:48 -05:00
|
|
|
// GetGameItemByID retrieves game download details by ID.
|
2024-09-24 06:17:11 -04:00
|
|
|
// @Summary Retrieve game download by ID
|
|
|
|
// @Description Retrieves details of a game download by game ID
|
|
|
|
// @Tags game
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "Game Download ID"
|
2024-11-16 00:48:48 -05:00
|
|
|
// @Success 200 {object} GetGameItemByIDResponse
|
|
|
|
// @Failure 400 {object} GetGameItemByIDResponse
|
|
|
|
// @Failure 500 {object} GetGameItemByIDResponse
|
2024-09-24 06:17:11 -04:00
|
|
|
// @Router /game/raw/id/{id} [get]
|
2024-11-16 00:48:48 -05:00
|
|
|
func GetGameItemByIDHanlder(c *gin.Context) {
|
|
|
|
var req GetGameItemByIDRequest
|
2024-09-24 06:17:11 -04:00
|
|
|
if err := c.ShouldBindUri(&req); err != nil {
|
2024-11-16 00:48:48 -05:00
|
|
|
c.JSON(http.StatusBadRequest, GetGameItemByIDResponse{
|
2024-09-24 06:17:11 -04:00
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := primitive.ObjectIDFromHex(req.ID)
|
|
|
|
if err != nil {
|
2024-11-16 00:48:48 -05:00
|
|
|
c.JSON(http.StatusBadRequest, GetGameItemByIDResponse{
|
2024-09-24 06:17:11 -04:00
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-11-16 00:48:48 -05:00
|
|
|
game, err := db.GetGameItemByID(id)
|
2024-09-24 06:17:11 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == mongo.ErrNoDocuments {
|
2024-11-16 00:48:48 -05:00
|
|
|
c.JSON(http.StatusOK, GetGameItemByIDResponse{
|
2024-09-24 06:17:11 -04:00
|
|
|
Status: "ok",
|
|
|
|
Message: "No results found",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-11-16 00:48:48 -05:00
|
|
|
c.JSON(http.StatusInternalServerError, GetGameItemByIDResponse{
|
2024-09-24 06:17:11 -04:00
|
|
|
Status: "error",
|
|
|
|
Message: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2024-11-16 00:48:48 -05:00
|
|
|
c.JSON(http.StatusOK, GetGameItemByIDResponse{
|
2024-09-24 06:17:11 -04:00
|
|
|
Status: "ok",
|
|
|
|
Game: game,
|
|
|
|
})
|
|
|
|
}
|