feat: add invoice and alternative_componen management functionality

This commit is contained in:
Tran Anh Tuan
2026-05-12 11:57:11 +07:00
parent c39b010e5e
commit e81a248a61
23 changed files with 3325 additions and 2 deletions

View 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
}

View File

@@ -0,0 +1,201 @@
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"
)
// InvoiceCreate creates a new invoice.
//
// @Summary Create a new invoice
// @Description Create a new invoice with the provided details
// @Tags invoice
// @Accept json
// @Produce json
// @Param body body requests.CreateInvoiceRequest true "Invoice request body"
// @Success 201 {object} response.SuccessResponse{data=responses.CreateInvoiceResponse}
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /v1/invoices [post]
func InvoiceCreate(c *gin.Context) error {
requestBody := requests.CreateInvoiceRequest{}
if helper.IsShouldBindJSON(c, &requestBody) {
return nil
}
invoiceModel := &models.Invoice{
Type: requestBody.Type,
Status: requestBody.Status,
InvoiceConfigID: requestBody.InvoiceConfigID,
TotalItems: requestBody.TotalItems,
Note: requestBody.Note,
CreatedBy: requestBody.CreatedBy,
ApprovedBy: requestBody.ApprovedBy,
CreatedAt: time.Now(),
}
invoice, err := repositories.CreateInvoice(c.Request.Context(), global.Queries, *invoiceModel)
if err != nil {
log.Error().Err(err).Msg("Failed to create invoice")
response.InternalServerError(c, http.StatusInternalServerError, "Failed to create invoice")
return nil
}
response.Created(c, "Invoice created successfully", &responses.CreateInvoiceResponse{
ID: invoice.ID,
InvoiceCode: invoice.InvoiceCode,
})
return nil
}
// InvoiceGetByID retrieves a single invoice by its ID.
//
// @Summary Get invoice by ID
// @Description Retrieve a single invoice using its unique identifier
// @Tags invoice
// @Accept json
// @Produce json
// @Param id path int true "Invoice ID"
// @Success 200 {object} response.SuccessResponse{data=models.Invoice}
// @Failure 400 {object} response.ErrorResponse
// @Failure 404 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /v1/invoices/{id} [get]
func InvoiceGetByID(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
}
invoice, err := repositories.GetInvoiceByID(c.Request.Context(), global.Queries, id)
if err != nil {
log.Error().Err(err).Msgf("Failed to get invoice by ID: %d", id)
response.NotFoundError(c, http.StatusNotFound, "Invoice not found")
return nil
}
response.Ok(c, "Success", invoice)
return nil
}
// InvoiceList retrieves all invoices.
//
// @Summary List all invoices
// @Description Retrieve a list of all invoices ordered by creation date
// @Tags invoice
// @Accept json
// @Produce json
// @Success 200 {object} response.SuccessResponse{data=[]models.Invoice}
// @Failure 500 {object} response.ErrorResponse
// @Router /v1/invoices [get]
func InvoiceList(c *gin.Context) error {
invoices, err := repositories.ListInvoices(c.Request.Context(), global.Queries)
if err != nil {
response.InternalServerError(c, http.StatusInternalServerError, "Failed to list invoices")
return nil
}
response.Ok(c, "Success", invoices)
return nil
}
// InvoiceUpdate updates an existing invoice by its ID.
//
// @Summary Update invoice
// @Description Update an existing invoice by its ID. Only non-empty fields will be updated.
// @Tags invoice
// @Accept json
// @Produce json
// @Param id path int true "Invoice ID"
// @Param body body requests.UpdateInvoiceRequest true "Invoice request body"
// @Success 200 {object} response.SuccessResponse{data=responses.UpdateInvoiceResponse}
// @Failure 400 {object} response.ErrorResponse
// @Failure 404 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /v1/invoices/{id} [put]
func InvoiceUpdate(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.UpdateInvoiceRequest{}
if helper.IsShouldBindJSON(c, &requestBody) {
return nil
}
existing, err := repositories.GetInvoiceByID(c.Request.Context(), global.Queries, id)
if err != nil {
response.NotFoundError(c, http.StatusNotFound, "Invoice not found")
return nil
}
if requestBody.Type != nil {
existing.Type = *requestBody.Type
}
if requestBody.Status != nil {
existing.Status = *requestBody.Status
}
if requestBody.InvoiceConfigID != nil {
existing.InvoiceConfigID = *requestBody.InvoiceConfigID
}
if requestBody.TotalItems != nil {
existing.TotalItems = *requestBody.TotalItems
}
if requestBody.Note != "" {
existing.Note = requestBody.Note
}
existing.UpdatedAt = time.Now()
invoice, err := repositories.UpdateInvoice(c.Request.Context(), global.Queries, existing)
if err != nil {
log.Error().Err(err).Msgf("Failed to update invoice with ID: %d", id)
response.InternalServerError(c, http.StatusInternalServerError, "Failed to update invoice")
return nil
}
response.Ok(c, "Invoice updated successfully", &responses.UpdateInvoiceResponse{
ID: invoice.ID,
InvoiceCode: invoice.InvoiceCode,
Type: invoice.Type,
Status: invoice.Status,
InvoiceConfigID: invoice.InvoiceConfigID,
TotalItems: invoice.TotalItems,
Note: invoice.Note,
})
return nil
}
// InvoiceDelete deletes an invoice by its ID.
//
// @Summary Delete invoice
// @Description Delete an invoice by its unique identifier
// @Tags invoice
// @Accept json
// @Produce json
// @Param id path int true "Invoice ID"
// @Success 200 {object} response.SuccessResponse
// @Failure 400 {object} response.ErrorResponse
// @Failure 500 {object} response.ErrorResponse
// @Router /v1/invoices/{id} [delete]
func InvoiceDelete(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.DeleteInvoice(c.Request.Context(), global.Queries, id)
if err != nil {
log.Error().Err(err).Msgf("Failed to delete invoice with ID: %d", id)
response.InternalServerError(c, http.StatusInternalServerError, "Failed to delete invoice")
return nil
}
if rowsAffected == 0 {
response.NotFoundError(c, http.StatusNotFound, "Invoice not found")
return nil
}
response.Ok(c, "Delete Success", nil)
return nil
}