Add @Router /game/id/{id} [delete]

This commit is contained in:
Nite07 2024-09-25 21:33:46 +08:00
parent b9ca7e2338
commit 104352da40
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package handler
import (
"net/http"
"pcgamedb/db"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type DeleteGameInfoRequest struct {
ID string `uri:"id" binding:"required"`
}
type DeleteGameInfoResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
}
// DeleteGameInfoHandler is the handler for deleting game info
// @Summary Delete game info by ID
// @Description Delete game info by ID
// @Tags game
// @Produce json
// @Param Authorization header string true "Authorization: Bearer <api_key>"
// @Param id path string true "Game ID"
// @Success 200 {object} DeleteGameInfoResponse
// @Failure 400 {object} DeleteGameInfoResponse
// @Failure 500 {object} DeleteGameInfoResponse
// @Security BearerAuth
// @Router /game/id/{id} [delete]
func DeleteGameInfoHandler(c *gin.Context) {
var req DeleteGameInfoRequest
if err := c.ShouldBindUri(&req); err != nil {
c.JSON(http.StatusBadRequest, DeleteGameInfoResponse{
Status: "error",
Message: err.Error(),
})
return
}
objID, err := primitive.ObjectIDFromHex(req.ID)
if err != nil {
c.JSON(http.StatusBadRequest, DeleteGameInfoResponse{
Status: "error",
Message: "Invalid ID",
})
return
}
err = db.DeleteGameInfoByID(objID)
if err != nil {
c.JSON(http.StatusInternalServerError, DeleteGameInfoResponse{
Status: "error",
Message: "Failed to delete game",
})
return
}
c.JSON(http.StatusOK, DeleteGameInfoResponse{
Status: "success",
Message: "Game deleted successfully",
})
}

View File

@ -32,6 +32,7 @@ func initRoute(app *gin.Engine) {
GameInfoGroup.GET("/platform/:platform_type/:platform_id", handler.GetGameInfoByPlatformIDHandler)
GameInfoGroup.GET("/id/:id", handler.GetGameInfoByIDHandler)
GameInfoGroup.PUT("/update", middleware.Auth(), handler.UpdateGameInfoHandler)
GameInfoGroup.DELETE("/id/:id", middleware.Auth(), handler.DeleteGameInfoHandler)
app.GET("/ranking/:type", handler.GetRankingHandler)
app.GET("/healthcheck", handler.HealthCheckHandler)