feat: add invoice-configs management functionality
This commit is contained in:
192
internal/services/invoice_config_service.go
Normal file
192
internal/services/invoice_config_service.go
Normal file
@@ -0,0 +1,192 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// InvoiceConfigCreate creates a new invoice config.
|
||||
//
|
||||
// @Summary Create a new invoice config
|
||||
// @Description Create a new invoice config with the provided details
|
||||
// @Tags invoice-config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body requests.CreateInvoiceConfigRequest true "Invoice config request body"
|
||||
// @Success 201 {object} response.SuccessResponse{data=responses.CreateInvoiceConfigResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/invoice-configs [post]
|
||||
func InvoiceConfigCreate(c *gin.Context) error {
|
||||
requestBody := requests.CreateInvoiceConfigRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
invoiceConfigModel := &models.InvoiceConfig{
|
||||
Name: requestBody.Name,
|
||||
Type: requestBody.Type,
|
||||
Description: requestBody.Description,
|
||||
IsActive: requestBody.IsActive,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
invoiceConfig, err := repositories.CreateInvoiceConfig(c.Request.Context(), global.Queries, *invoiceConfigModel)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to create invoice config")
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create invoice config")
|
||||
return nil
|
||||
}
|
||||
response.Created(c, "Invoice config created successfully", &responses.CreateInvoiceConfigResponse{
|
||||
ID: invoiceConfig.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvoiceConfigGetByID retrieves a single invoice config by its ID.
|
||||
//
|
||||
// @Summary Get invoice config by ID
|
||||
// @Description Retrieve a single invoice config using its unique identifier
|
||||
// @Tags invoice-config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Invoice config ID"
|
||||
// @Success 200 {object} response.SuccessResponse{data=models.InvoiceConfig}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/invoice-configs/{id} [get]
|
||||
func InvoiceConfigGetByID(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
|
||||
}
|
||||
invoiceConfig, err := repositories.GetInvoiceConfigByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to get invoice config by ID: %d", id)
|
||||
response.NotFoundError(c, http.StatusNotFound, "Invoice config not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", invoiceConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvoiceConfigList retrieves all invoice configs.
|
||||
//
|
||||
// @Summary List all invoice configs
|
||||
// @Description Retrieve a list of all invoice configs ordered by creation date
|
||||
// @Tags invoice-config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.SuccessResponse{data=[]models.InvoiceConfig}
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/invoice-configs [get]
|
||||
func InvoiceConfigList(c *gin.Context) error {
|
||||
invoiceConfigs, err := repositories.ListInvoiceConfigs(c.Request.Context(), global.Queries)
|
||||
if err != nil {
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list invoice configs")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Success", invoiceConfigs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvoiceConfigUpdate updates an existing invoice config by its ID.
|
||||
//
|
||||
// @Summary Update invoice config
|
||||
// @Description Update an existing invoice config by its ID. Only non-empty fields will be updated.
|
||||
// @Tags invoice-config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Invoice config ID"
|
||||
// @Param body body requests.UpdateInvoiceConfigRequest true "Invoice config request body"
|
||||
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateInvoiceConfigResponse}
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 404 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/invoice-configs/{id} [put]
|
||||
func InvoiceConfigUpdate(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.UpdateInvoiceConfigRequest{}
|
||||
if helper.IsShouldBindJSON(c, &requestBody) {
|
||||
return nil
|
||||
}
|
||||
existing, err := repositories.GetInvoiceConfigByID(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Invoice config not found")
|
||||
return nil
|
||||
}
|
||||
if requestBody.Name != "" {
|
||||
existing.Name = requestBody.Name
|
||||
}
|
||||
if requestBody.Type != "" {
|
||||
existing.Type = requestBody.Type
|
||||
}
|
||||
if requestBody.Description != "" {
|
||||
existing.Description = requestBody.Description
|
||||
}
|
||||
if requestBody.IsActive != nil {
|
||||
existing.IsActive = *requestBody.IsActive
|
||||
}
|
||||
existing.UpdatedAt = time.Now()
|
||||
invoiceConfig, err := repositories.UpdateInvoiceConfig(c.Request.Context(), global.Queries, existing)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to update invoice config with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update invoice config")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Invoice config updated successfully", &responses.UpdateInvoiceConfigResponse{
|
||||
ID: invoiceConfig.ID,
|
||||
Name: invoiceConfig.Name,
|
||||
Type: invoiceConfig.Type,
|
||||
Description: invoiceConfig.Description,
|
||||
IsActive: invoiceConfig.IsActive,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// InvoiceConfigDelete deletes an invoice config by its ID.
|
||||
//
|
||||
// @Summary Delete invoice config
|
||||
// @Description Delete an invoice config by its unique identifier
|
||||
// @Tags invoice-config
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "Invoice config ID"
|
||||
// @Success 200 {object} response.SuccessResponse
|
||||
// @Failure 400 {object} response.ErrorResponse
|
||||
// @Failure 500 {object} response.ErrorResponse
|
||||
// @Router /v1/invoice-configs/{id} [delete]
|
||||
func InvoiceConfigDelete(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.DeleteInvoiceConfig(c.Request.Context(), global.Queries, id)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("Failed to delete invoice config with ID: %d", id)
|
||||
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete invoice config")
|
||||
return nil
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
response.NotFoundError(c, http.StatusNotFound, "Invoice config not found")
|
||||
return nil
|
||||
}
|
||||
response.Ok(c, "Delete Success", nil)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user