feat: add invoice and alternative_componen management functionality
This commit is contained in:
194
internal/services/alternative_component_service.go
Normal file
194
internal/services/alternative_component_service.go
Normal file
@@ -0,0 +1,194 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"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"
|
||||
)
|
||||
|
||||
// AlternativeComponentCreate creates a new alternative component.
|
||||
//
|
||||
// @Summary Create a new alternative component
|
||||
// @Description Create a new alternative component with the provided details
|
||||
// @Tags alternative-component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateAlternativeComponentRequest true "Alternative component request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateAlternativeComponentResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/alternative-components [post]
|
||||
func AlternativeComponentCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateAlternativeComponentRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
entityModel := &models.AlternativeComponent{
|
||||
InvoiceConfigItemID: requestBody.InvoiceConfigItemID,
|
||||
AlternativeComponentID: requestBody.AlternativeComponentID,
|
||||
ConversionRatio: requestBody.ConversionRatio,
|
||||
Priority: requestBody.Priority,
|
||||
Note: requestBody.Note,
|
||||
}
|
||||
entity, err := repositories.CreateAlternativeComponent(c.Request.Context(), global.Queries, *entityModel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create alternative component")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create alternative component")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Alternative component created successfully", &responses.CreateAlternativeComponentResponse{
|
||||
ID: entity.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// AlternativeComponentGetByID retrieves a single alternative component by its ID.
|
||||
//
|
||||
// @Summary Get alternative component by ID
|
||||
// @Description Retrieve a single alternative component using its unique identifier
|
||||
// @Tags alternative-component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Alternative component ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.AlternativeComponent}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/alternative-components/{id} [get]
|
||||
func AlternativeComponentGetByID(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
|
||||
}
|
||||
entity, err := repositories.GetAlternativeComponentByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to get alternative component by ID: %d", id)
|
||||
response.NotFoundError(c, http.StatusNotFound, "Alternative component not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", entity)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AlternativeComponentList retrieves all alternative components.
|
||||
//
|
||||
// @Summary List all alternative components
|
||||
// @Description Retrieve a list of all alternative components
|
||||
// @Tags alternative-component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.AlternativeComponent}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/alternative-components [get]
|
||||
func AlternativeComponentList(c *gin.Context) error {
|
||||
entities, err := repositories.ListAlternativeComponents(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list alternative components")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", entities)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AlternativeComponentUpdate updates an existing alternative component by its ID.
|
||||
//
|
||||
// @Summary Update alternative component
|
||||
// @Description Update an existing alternative component by its ID. Only non-empty fields will be updated.
|
||||
// @Tags alternative-component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Alternative component ID"
|
||||
// @Param body body requests.UpdateAlternativeComponentRequest true "Alternative component request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateAlternativeComponentResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/alternative-components/{id} [put]
|
||||
func AlternativeComponentUpdate(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.UpdateAlternativeComponentRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
existing, err := repositories.GetAlternativeComponentByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Alternative component not found")
|
||||
return nil
|
||||
}
|
||||
if requestBody.InvoiceConfigItemID != nil {
|
||||
existing.InvoiceConfigItemID = *requestBody.InvoiceConfigItemID
|
||||
}
|
||||
if requestBody.AlternativeComponentID != nil {
|
||||
existing.AlternativeComponentID = *requestBody.AlternativeComponentID
|
||||
}
|
||||
if requestBody.ConversionRatio != nil {
|
||||
existing.ConversionRatio = *requestBody.ConversionRatio
|
||||
}
|
||||
if requestBody.Priority != nil {
|
||||
existing.Priority = *requestBody.Priority
|
||||
}
|
||||
if requestBody.Note != "" {
|
||||
existing.Note = requestBody.Note
|
||||
}
|
||||
entity, err := repositories.UpdateAlternativeComponent(c.Request.Context(), global.Queries, existing)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to update alternative component with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update alternative component")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Alternative component updated successfully", &responses.UpdateAlternativeComponentResponse{
|
||||
ID: entity.ID,
|
||||
InvoiceConfigItemID: entity.InvoiceConfigItemID,
|
||||
AlternativeComponentID: entity.AlternativeComponentID,
|
||||
ConversionRatio: entity.ConversionRatio,
|
||||
Priority: entity.Priority,
|
||||
Note: entity.Note,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// AlternativeComponentDelete deletes an alternative component by its ID.
|
||||
//
|
||||
// @Summary Delete alternative component
|
||||
// @Description Delete an alternative component by its unique identifier
|
||||
// @Tags alternative-component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Alternative component ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/alternative-components/{id} [delete]
|
||||
func AlternativeComponentDelete(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.DeleteAlternativeComponent(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to delete alternative component with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete alternative component")
|
||||
return nil
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Alternative component not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Delete Success", nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user