diff --git a/server/handler/delete_game_info.go b/server/handler/delete_game_info.go new file mode 100644 index 0000000..2dda0e9 --- /dev/null +++ b/server/handler/delete_game_info.go @@ -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 " +// @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", + }) +} diff --git a/server/route.go b/server/route.go index 6effbdb..09936f4 100644 --- a/server/route.go +++ b/server/route.go @@ -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)