194 lines
7.7 KiB
Go
194 lines
7.7 KiB
Go
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"
|
|
)
|
|
|
|
// InvoiceConfigItemCreate creates a new invoice config item.
|
|
//
|
|
// @Summary Create a new invoice config item
|
|
// @Description Create a new invoice config item with the provided details
|
|
// @Tags invoice-config-item
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body requests.CreateInvoiceConfigItemRequest true "Invoice config item request body"
|
|
// @Success 201 {object} response.SuccessResponse{data=responses.CreateInvoiceConfigItemResponse}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/invoice-config-items [post]
|
|
func InvoiceConfigItemCreate(c *gin.Context) error {
|
|
requestBody := requests.CreateInvoiceConfigItemRequest{}
|
|
if helper.IsShouldBindJSON(c, &requestBody) {
|
|
return nil
|
|
}
|
|
invoiceConfigItemModel := &models.InvoiceConfigItem{
|
|
InvoiceConfigID: requestBody.InvoiceConfigID,
|
|
ComponentID: requestBody.ComponentID,
|
|
RequiredQuantity: requestBody.RequiredQuantity,
|
|
AllowAlternative: requestBody.AllowAlternative,
|
|
PriorityOrder: requestBody.PriorityOrder,
|
|
Note: requestBody.Note,
|
|
}
|
|
invoiceConfigItem, err := repositories.CreateInvoiceConfigItem(c.Request.Context(), global.Queries, *invoiceConfigItemModel)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("Failed to create invoice config item")
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create invoice config item")
|
|
return nil
|
|
}
|
|
response.Created(c, "Invoice config item created successfully", &responses.CreateInvoiceConfigItemResponse{
|
|
ID: invoiceConfigItem.ID,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// InvoiceConfigItemGetByID retrieves a single invoice config item by its ID.
|
|
//
|
|
// @Summary Get invoice config item by ID
|
|
// @Description Retrieve a single invoice config item using its unique identifier
|
|
// @Tags invoice-config-item
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Invoice config item ID"
|
|
// @Success 200 {object} response.SuccessResponse{data=models.InvoiceConfigItem}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 404 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/invoice-config-items/{id} [get]
|
|
func InvoiceConfigItemGetByID(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
|
|
}
|
|
invoiceConfigItem, err := repositories.GetInvoiceConfigItemByID(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
log.Error().Err(err).Msgf("Failed to get invoice config item by ID: %d", id)
|
|
response.NotFoundError(c, http.StatusNotFound, "Invoice config item not found")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", invoiceConfigItem)
|
|
return nil
|
|
}
|
|
|
|
// InvoiceConfigItemList retrieves all invoice config items.
|
|
//
|
|
// @Summary List all invoice config items
|
|
// @Description Retrieve a list of all invoice config items
|
|
// @Tags invoice-config-item
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.SuccessResponse{data=[]models.InvoiceConfigItem}
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/invoice-config-items [get]
|
|
func InvoiceConfigItemList(c *gin.Context) error {
|
|
invoiceConfigItems, err := repositories.ListInvoiceConfigItems(c.Request.Context(), global.Queries)
|
|
if err != nil {
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list invoice config items")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Success", invoiceConfigItems)
|
|
return nil
|
|
}
|
|
|
|
// InvoiceConfigItemUpdate updates an existing invoice config item by its ID.
|
|
//
|
|
// @Summary Update invoice config item
|
|
// @Description Update an existing invoice config item by its ID. Only non-empty fields will be updated.
|
|
// @Tags invoice-config-item
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Invoice config item ID"
|
|
// @Param body body requests.UpdateInvoiceConfigItemRequest true "Invoice config item request body"
|
|
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateInvoiceConfigItemResponse}
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 404 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/invoice-config-items/{id} [put]
|
|
func InvoiceConfigItemUpdate(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.UpdateInvoiceConfigItemRequest{}
|
|
if helper.IsShouldBindJSON(c, &requestBody) {
|
|
return nil
|
|
}
|
|
existing, err := repositories.GetInvoiceConfigItemByID(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
response.NotFoundError(c, http.StatusNotFound, "Invoice config item not found")
|
|
return nil
|
|
}
|
|
if requestBody.RequiredQuantity != nil {
|
|
existing.RequiredQuantity = *requestBody.RequiredQuantity
|
|
}
|
|
if requestBody.AllowAlternative != nil {
|
|
existing.AllowAlternative = *requestBody.AllowAlternative
|
|
}
|
|
if requestBody.PriorityOrder != nil {
|
|
existing.PriorityOrder = *requestBody.PriorityOrder
|
|
}
|
|
if requestBody.Note != "" {
|
|
existing.Note = requestBody.Note
|
|
}
|
|
invoiceConfigItem, err := repositories.UpdateInvoiceConfigItem(c.Request.Context(), global.Queries, existing)
|
|
if err != nil {
|
|
log.Error().Err(err).Msgf("Failed to update invoice config item with ID: %d", id)
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update invoice config item")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Invoice config item updated successfully", &responses.UpdateInvoiceConfigItemResponse{
|
|
ID: invoiceConfigItem.ID,
|
|
InvoiceConfigID: invoiceConfigItem.InvoiceConfigID,
|
|
ComponentID: invoiceConfigItem.ComponentID,
|
|
RequiredQuantity: invoiceConfigItem.RequiredQuantity,
|
|
AllowAlternative: invoiceConfigItem.AllowAlternative,
|
|
PriorityOrder: invoiceConfigItem.PriorityOrder,
|
|
Note: invoiceConfigItem.Note,
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// InvoiceConfigItemDelete deletes an invoice config item by its ID.
|
|
//
|
|
// @Summary Delete invoice config item
|
|
// @Description Delete an invoice config item by its unique identifier
|
|
// @Tags invoice-config-item
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "Invoice config item ID"
|
|
// @Success 200 {object} response.SuccessResponse
|
|
// @Failure 400 {object} response.ErrorResponse
|
|
// @Failure 500 {object} response.ErrorResponse
|
|
// @Router /v1/invoice-config-items/{id} [delete]
|
|
func InvoiceConfigItemDelete(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.DeleteInvoiceConfigItem(c.Request.Context(), global.Queries, id)
|
|
if err != nil {
|
|
log.Error().Err(err).Msgf("Failed to delete invoice config item with ID: %d", id)
|
|
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete invoice config item")
|
|
return nil
|
|
}
|
|
if rowsAffected == 0 {
|
|
response.NotFoundError(c, http.StatusNotFound, "Invoice config item not found")
|
|
return nil
|
|
}
|
|
response.Ok(c, "Delete Success", nil)
|
|
return nil
|
|
}
|