202 lines
7.1 KiB
Go
202 lines
7.1 KiB
Go
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"
|
|
)
|
|
|
|
// ComponentCreate creates a new component.
|
|
// It validates the request body and creates the component in the database.
|
|
//
|
|
// @Summary Create a new component
|
|
// @Description Create a new component with the provided details
|
|
// @Tags component
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body requests.CreateComponentRequest true "Component request body"
|
|
// @Success 201 {object} response.SuccessResponse{data=responses.CreateComponentResponse}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /api/v1/components [post]
|
|
func ComponentCreate(c *gin.Context) error {
|
|
requestBody := requests.CreateComponentRequest{}
|
|
if helper.IsShouldBindJSON(c, &requestBody) {
|
|
return nil
|
|
}
|
|
componentModel := &models.Component{
|
|
ComponentTypeID: requestBody.ComponentTypeID,
|
|
Name: requestBody.Name,
|
|
Description: requestBody.Description,
|
|
Unit: requestBody.Unit,
|
|
MinQuantity: requestBody.MinQuantity,
|
|
Metadata: requestBody.Metadata,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
component, err := repositories.CreateComponent(c.Request.Context(), global.Queries, *componentModel)
|
|
if err != nil {
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create component")
|
|
return nil
|
|
}
|
|
response.Created(c, "Component created successfully", &responses.CreateComponentResponse{
|
|
ID: component.ID,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// ComponentGetByID retrieves a single component by its ID.
|
|
//
|
|
// @Summary Get component by ID
|
|
// @Description Retrieve a single component using its unique identifier
|
|
// @Tags component
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Component ID"
|
|
// @Success 200 {object} response.SuccessResponse{data=models.Component}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 404 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /api/v1/components/{id} [get]
|
|
func ComponentGetByID(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
|
|
}
|
|
component, err := repositories.GetComponentByID(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
response.NotFoundError(c, http.StatusNotFound, "Component not found")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", component)
|
|
return nil
|
|
}
|
|
|
|
// ComponentList retrieves all components.
|
|
//
|
|
// @Summary List all components
|
|
// @Description Retrieve a list of all components ordered by creation date
|
|
// @Tags component
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.SuccessResponse{data=[]models.Component}
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /api/v1/components [get]
|
|
func ComponentList(c *gin.Context) error {
|
|
components, err := repositories.ListComponents(c.Request.Context(), global.Queries)
|
|
if err != nil {
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list components")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", components)
|
|
return nil
|
|
}
|
|
|
|
// ComponentUpdate updates an existing component by its ID.
|
|
// It validates the request body, fetches the existing record,
|
|
// merges non-empty fields from the request, and updates the component in the database.
|
|
//
|
|
// @Summary Update component
|
|
// @Description Update an existing component by its ID. Only non-empty fields will be updated.
|
|
// @Tags component
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Component ID"
|
|
// @Param body body requests.UpdateComponentRequest true "Component request body"
|
|
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateComponentResponse}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 404 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /api/v1/components/{id} [put]
|
|
func ComponentUpdate(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.UpdateComponentRequest{}
|
|
if helper.IsShouldBindJSON(c, &requestBody) {
|
|
return nil
|
|
}
|
|
existing, err := repositories.GetComponentByID(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
response.NotFoundError(c, http.StatusNotFound, "Component not found")
|
|
return nil
|
|
}
|
|
if requestBody.ComponentTypeID != 0 {
|
|
existing.ComponentTypeID = requestBody.ComponentTypeID
|
|
}
|
|
if requestBody.Name != "" {
|
|
existing.Name = requestBody.Name
|
|
}
|
|
if requestBody.Description != "" {
|
|
existing.Description = requestBody.Description
|
|
}
|
|
if requestBody.Unit != "" {
|
|
existing.Unit = requestBody.Unit
|
|
}
|
|
if requestBody.MinQuantity != 0 {
|
|
existing.MinQuantity = requestBody.MinQuantity
|
|
}
|
|
if len(requestBody.Metadata) > 0 {
|
|
existing.Metadata = requestBody.Metadata
|
|
}
|
|
existing.UpdatedAt = time.Now()
|
|
component, err := repositories.UpdateComponent(c.Request.Context(), global.Queries, existing)
|
|
if err != nil {
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update component")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Component updated successfully", &responses.UpdateComponentResponse{
|
|
ID: component.ID,
|
|
ComponentTypeID: component.ComponentTypeID,
|
|
Name: component.Name,
|
|
Description: component.Description,
|
|
Unit: component.Unit,
|
|
MinQuantity: component.MinQuantity,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// ComponentDelete deletes a component by its ID.
|
|
//
|
|
// @Summary Delete component
|
|
// @Description Delete a component by its unique identifier
|
|
// @Tags component
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Component ID"
|
|
// @Success 200 {object} response.SuccessResponse
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /api/v1/components/{id} [delete]
|
|
func ComponentDelete(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.DeleteComponent(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
log.Error().Err(err).Msgf("Failed to delete component with ID: %d", id)
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete component")
|
|
return nil
|
|
}
|
|
if rowsAffected == 0 {
|
|
response.NotFoundError(c, http.StatusNotFound, "Component not found")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Delete Success", nil)
|
|
return nil
|
|
}
|