package services import ( "net/http" "strconv" "time" "wm-backend/global" "wm-backend/internal/models" "wm-backend/internal/models/requests" "wm-backend/internal/models/responses" "wm-backend/internal/repositories" "wm-backend/pkg/helper" "wm-backend/response" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" ) // ShelveCreate creates a new shelve. // It validates the request body and creates the shelve in the database. // // @Summary Create a new shelve // @Description Create a new shelve with the provided details // @Tags shelve // @Accept json // @Produce json // @Param body body requests.CreateShelveRequest true "Shelve request body" // @Success 201 {object} response.SuccessResponse{data=responses.CreateShelveResponse} // @Failure 400 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /v1/shelves [post] func ShelveCreate(c *gin.Context) error { requestBody := requests.CreateShelveRequest{} if helper.IsShouldBindJSON(c, &requestBody) { return nil } shelveModel := &models.Shelve{ CabinetID: requestBody.CabinetID, Name: requestBody.Name, LevelIndex: requestBody.LevelIndex, Description: requestBody.Description, CreatedAt: time.Now(), } shelve, err := repositories.CreateShelve(c.Request.Context(), global.Queries, *shelveModel) if err != nil { log.Error().Err(err).Msg("Failed to create shelve") response.InternalServerError(c, http.StatusInternalServerError, "Failed to create shelve") return nil } response.Created(c, "Shelve created successfully", &responses.CreateShelveResponse{ ID: shelve.ID, }) return nil } // ShelveGetByID retrieves a single shelve by its ID. // // @Summary Get shelve by ID // @Description Retrieve a single shelve using its unique identifier // @Tags shelve // @Accept json // @Produce json // @Param id path int true "Shelve ID" // @Success 200 {object} response.SuccessResponse{data=models.Shelve} // @Failure 400 {object} response.ErrorResponse // @Failure 404 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /v1/shelves/{id} [get] func ShelveGetByID(c *gin.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { response.BadRequestError(c, http.StatusBadRequest, "Invalid ID") return nil } shelve, err := repositories.GetShelveByID(c.Request.Context(), global.Queries, id) if err != nil { log.Error().Err(err).Msgf("Failed to get shelve by ID: %d", id) response.NotFoundError(c, http.StatusNotFound, "Shelve not found") return nil } response.Ok(c, "Success", shelve) return nil } // ShelveList retrieves all shelves. // // @Summary List all shelves // @Description Retrieve a list of all shelves ordered by creation date // @Tags shelve // @Accept json // @Produce json // @Success 200 {object} response.SuccessResponse{data=[]models.Shelve} // @Failure 500 {object} response.ErrorResponse // @Router /v1/shelves [get] func ShelveList(c *gin.Context) error { shelves, err := repositories.ListShelves(c.Request.Context(), global.Queries) if err != nil { response.InternalServerError(c, http.StatusInternalServerError, "Failed to list shelves") return nil } response.Ok(c, "Success", shelves) return nil } // ShelveUpdate updates an existing shelve by its ID. // It validates the request body, fetches the existing record, // merges non-empty fields from the request, and updates the shelve in the database. // // @Summary Update shelve // @Description Update an existing shelve by its ID. Only non-empty fields will be updated. // @Tags shelve // @Accept json // @Produce json // @Param id path int true "Shelve ID" // @Param body body requests.UpdateShelveRequest true "Shelve request body" // @Success 200 {object} response.SuccessResponse{data=responses.UpdateShelveResponse} // @Failure 400 {object} response.ErrorResponse // @Failure 404 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /v1/shelves/{id} [put] func ShelveUpdate(c *gin.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { response.BadRequestError(c, http.StatusBadRequest, "Invalid ID") return nil } requestBody := requests.UpdateShelveRequest{} if helper.IsShouldBindJSON(c, &requestBody) { return nil } existing, err := repositories.GetShelveByID(c.Request.Context(), global.Queries, id) if err != nil { response.NotFoundError(c, http.StatusNotFound, "Shelve not found") return nil } if requestBody.Name != "" { existing.Name = requestBody.Name } if requestBody.LevelIndex != 0 { existing.LevelIndex = requestBody.LevelIndex } if requestBody.Description != "" { existing.Description = requestBody.Description } existing.UpdatedAt = time.Now() shelve, err := repositories.UpdateShelve(c.Request.Context(), global.Queries, existing) if err != nil { log.Error().Err(err).Msgf("Failed to update shelve with ID: %d", id) response.InternalServerError(c, http.StatusInternalServerError, "Failed to update shelve") return nil } response.Ok(c, "Shelve updated successfully", &responses.UpdateShelveResponse{ ID: shelve.ID, CabinetID: shelve.CabinetID, Name: shelve.Name, LevelIndex: shelve.LevelIndex, Description: shelve.Description, }) return nil } // ShelveDelete deletes a shelve by its ID. // // @Summary Delete shelve // @Description Delete a shelve by its unique identifier // @Tags shelve // @Accept json // @Produce json // @Param id path int true "Shelve ID" // @Success 200 {object} response.SuccessResponse // @Failure 400 {object} response.ErrorResponse // @Failure 500 {object} response.ErrorResponse // @Router /v1/shelves/{id} [delete] func ShelveDelete(c *gin.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { response.BadRequestError(c, http.StatusBadRequest, "Invalid ID") return nil } rowsAffected, err := repositories.DeleteShelve(c.Request.Context(), global.Queries, id) if err != nil { log.Error().Err(err).Msgf("Failed to delete shelve with ID: %d", id) response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete shelve") return nil } if rowsAffected == 0 { response.NotFoundError(c, http.StatusNotFound, "Shelve not found") return nil } response.Ok(c, "Delete Success", nil) return nil }