Base Project
This commit is contained in:
171
internal/services/warehouse_service.go
Normal file
171
internal/services/warehouse_service.go
Normal file
@@ -0,0 +1,171 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// WareHouseCreate creates a new warehouse.
|
||||
// It validates the request body and creates the warehouse in the database.
|
||||
//
|
||||
// @Summary Create a new warehouse
|
||||
// @Description Create a new warehouse with the provided details
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateWarehouseRequest true "Warehouse request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateWarehouseResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses [post]
|
||||
func WareHouseCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateWarehouseRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
warehouseModel := &models.Warehouse{
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
Address: requestBody.Address,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
warehouse, err := repositories.CreateWareHouse(c.Request.Context(), global.Queries, *warehouseModel)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Warehouse created successfully", &responses.CreateWarehouseResponse{
|
||||
ID: warehouse.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseGetByID retrieves a single warehouse by its ID.
|
||||
//
|
||||
// @Summary Get warehouse by ID
|
||||
// @Description Retrieve a single warehouse using its unique identifier
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.Warehouse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [get]
|
||||
func WareHouseGetByID(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
|
||||
}
|
||||
warehouse, err := repositories.GetWarehouseByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Warehouse not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", warehouse)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseList retrieves all warehouses.
|
||||
//
|
||||
// @Summary List all warehouses
|
||||
// @Description Retrieve a list of all warehouses ordered by creation date
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.Warehouse}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses [get]
|
||||
func WareHouseList(c *gin.Context) error {
|
||||
warehouses, err := repositories.ListWarehouses(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list warehouses")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", warehouses)
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseUpdate updates an existing warehouse by its ID.
|
||||
// It validates the request body and updates the warehouse in the database.
|
||||
//
|
||||
// @Summary Update warehouse
|
||||
// @Description Update an existing warehouse by its ID with the provided details
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Param body body requests.UpdateWarehouseRequest true "Warehouse request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateWarehouseResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [put]
|
||||
func WareHouseUpdate(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.UpdateWarehouseRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
warehouseModel := &models.Warehouse{
|
||||
ID: id,
|
||||
Name: requestBody.Name,
|
||||
Description: requestBody.Description,
|
||||
Address: requestBody.Address,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
warehouse, err := repositories.UpdateWarehouse(c.Request.Context(), global.Queries, *warehouseModel)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Warehouse updated successfully", &responses.UpdateWarehouseResponse{
|
||||
ID: warehouse.ID,
|
||||
Name: warehouse.Name,
|
||||
Description: warehouse.Description,
|
||||
Address: warehouse.Address,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// WareHouseDelete deletes a warehouse by its ID.
|
||||
//
|
||||
// @Summary Delete warehouse
|
||||
// @Description Delete a warehouse by its unique identifier
|
||||
// @Tags warehouse
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Warehouse ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/warehouses/{id} [delete]
|
||||
func WareHouseDelete(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
|
||||
}
|
||||
err = repositories.DeleteWarehouse(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete warehouse")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Đã xóa thành công", nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user