feat: add invoice and alternative_componen management functionality
This commit is contained in:
67
internal/mapper/alternative_component_mapper.go
Normal file
67
internal/mapper/alternative_component_mapper.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func numericToString(n pgtype.Numeric) string {
|
||||
if !n.Valid {
|
||||
return ""
|
||||
}
|
||||
b, _ := json.Marshal(n)
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func stringToNumeric(s string) pgtype.Numeric {
|
||||
var n pgtype.Numeric
|
||||
if s == "" {
|
||||
return n
|
||||
}
|
||||
_ = json.Unmarshal([]byte(s), &n)
|
||||
return n
|
||||
}
|
||||
|
||||
func ToDomainAlternativeComponent(r db.AlternativeComponent) *models.AlternativeComponent {
|
||||
return &models.AlternativeComponent{
|
||||
ID: r.ID,
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: numericToString(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: r.Note.String,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelAlternativeComponent(r *models.AlternativeComponent) *db.CreateAlternativeComponentParams {
|
||||
return &db.CreateAlternativeComponentParams{
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelAlternativeComponent(r *models.AlternativeComponent) *db.UpdateAlternativeComponentParams {
|
||||
return &db.UpdateAlternativeComponentParams{
|
||||
InvoiceConfigItemID: r.InvoiceConfigItemID,
|
||||
AlternativeComponentID: r.AlternativeComponentID,
|
||||
ConversionRatio: stringToNumeric(r.ConversionRatio),
|
||||
Priority: r.Priority,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
78
internal/mapper/invoice_mapper.go
Normal file
78
internal/mapper/invoice_mapper.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func timestampToTime(t pgtype.Timestamptz) string {
|
||||
if !t.Valid {
|
||||
return ""
|
||||
}
|
||||
return t.Time.String()
|
||||
}
|
||||
|
||||
func ToDomainInvoice(r db.Invoice) *models.Invoice {
|
||||
return &models.Invoice{
|
||||
ID: r.ID,
|
||||
InvoiceCode: r.InvoiceCode,
|
||||
Type: string(r.Type),
|
||||
Status: string(r.Status),
|
||||
InvoiceConfigID: r.InvoiceConfigID.Int64,
|
||||
TotalItems: r.TotalItems,
|
||||
Note: r.Note.String,
|
||||
CreatedBy: r.CreatedBy.String,
|
||||
ApprovedBy: r.ApprovedBy.String,
|
||||
CompletedAt: r.CompletedAt.Time,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelInvoice(r *models.Invoice) *db.CreateInvoiceParams {
|
||||
return &db.CreateInvoiceParams{
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Status: db.InvoiceStatusEnum(r.Status),
|
||||
InvoiceConfigID: pgtype.Int8{
|
||||
Int64: r.InvoiceConfigID,
|
||||
Valid: r.InvoiceConfigID != 0,
|
||||
},
|
||||
TotalItems: r.TotalItems,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
CreatedBy: pgtype.Text{
|
||||
String: r.CreatedBy,
|
||||
Valid: r.CreatedBy != "",
|
||||
},
|
||||
ApprovedBy: pgtype.Text{
|
||||
String: r.ApprovedBy,
|
||||
Valid: r.ApprovedBy != "",
|
||||
},
|
||||
CreatedAt: r.CreatedAt,
|
||||
Metadata: r.Metadata,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelInvoice(r *models.Invoice) *db.UpdateInvoiceParams {
|
||||
return &db.UpdateInvoiceParams{
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Status: db.InvoiceStatusEnum(r.Status),
|
||||
InvoiceConfigID: pgtype.Int8{
|
||||
Int64: r.InvoiceConfigID,
|
||||
Valid: r.InvoiceConfigID != 0,
|
||||
},
|
||||
TotalItems: r.TotalItems,
|
||||
Note: pgtype.Text{
|
||||
String: r.Note,
|
||||
Valid: r.Note != "",
|
||||
},
|
||||
Metadata: r.Metadata,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
11
internal/models/alternative_component_model.go
Normal file
11
internal/models/alternative_component_model.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package models
|
||||
|
||||
type AlternativeComponent struct {
|
||||
ID int64 `json:"id"`
|
||||
InvoiceConfigItemID int64 `json:"invoiceConfigItemId"`
|
||||
AlternativeComponentID int64 `json:"alternativeComponentId"`
|
||||
ConversionRatio string `json:"conversionRatio"`
|
||||
Priority int32 `json:"priority"`
|
||||
Note string `json:"note"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
}
|
||||
19
internal/models/invoice_model.go
Normal file
19
internal/models/invoice_model.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type Invoice struct {
|
||||
ID int64 `json:"id"`
|
||||
InvoiceCode string `json:"invoiceCode"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
InvoiceConfigID int64 `json:"invoiceConfigId"`
|
||||
TotalItems int32 `json:"totalItems"`
|
||||
Note string `json:"note"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
ApprovedBy string `json:"approvedBy"`
|
||||
CompletedAt time.Time `json:"completedAt"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
}
|
||||
17
internal/models/requests/alternative_component_request.go
Normal file
17
internal/models/requests/alternative_component_request.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package requests
|
||||
|
||||
type CreateAlternativeComponentRequest struct {
|
||||
InvoiceConfigItemID int64 `json:"invoiceConfigItemId" binding:"required"`
|
||||
AlternativeComponentID int64 `json:"alternativeComponentId" binding:"required"`
|
||||
ConversionRatio string `json:"conversionRatio" binding:"required"`
|
||||
Priority int32 `json:"priority" binding:"required"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
type UpdateAlternativeComponentRequest struct {
|
||||
InvoiceConfigItemID *int64 `json:"invoiceConfigItemId"`
|
||||
AlternativeComponentID *int64 `json:"alternativeComponentId"`
|
||||
ConversionRatio *string `json:"conversionRatio"`
|
||||
Priority *int32 `json:"priority"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
19
internal/models/requests/invoice_request.go
Normal file
19
internal/models/requests/invoice_request.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package requests
|
||||
|
||||
type CreateInvoiceRequest struct {
|
||||
Type string `json:"type" binding:"required"`
|
||||
Status string `json:"status" binding:"required"`
|
||||
InvoiceConfigID int64 `json:"invoiceConfigId"`
|
||||
TotalItems int32 `json:"totalItems"`
|
||||
Note string `json:"note"`
|
||||
CreatedBy string `json:"createdBy"`
|
||||
ApprovedBy string `json:"approvedBy"`
|
||||
}
|
||||
|
||||
type UpdateInvoiceRequest struct {
|
||||
Type *string `json:"type"`
|
||||
Status *string `json:"status"`
|
||||
InvoiceConfigID *int64 `json:"invoiceConfigId"`
|
||||
TotalItems *int32 `json:"totalItems"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
14
internal/models/responses/alternative_component_response.go
Normal file
14
internal/models/responses/alternative_component_response.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package responses
|
||||
|
||||
type CreateAlternativeComponentResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type UpdateAlternativeComponentResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
InvoiceConfigItemID int64 `json:"invoiceConfigItemId"`
|
||||
AlternativeComponentID int64 `json:"alternativeComponentId"`
|
||||
ConversionRatio string `json:"conversionRatio"`
|
||||
Priority int32 `json:"priority"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
16
internal/models/responses/invoice_response.go
Normal file
16
internal/models/responses/invoice_response.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package responses
|
||||
|
||||
type CreateInvoiceResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
InvoiceCode string `json:"invoiceCode"`
|
||||
}
|
||||
|
||||
type UpdateInvoiceResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
InvoiceCode string `json:"invoiceCode"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
InvoiceConfigID int64 `json:"invoiceConfigId"`
|
||||
TotalItems int32 `json:"totalItems"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
52
internal/repositories/alternative_component_repository.go
Normal file
52
internal/repositories/alternative_component_repository.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
func CreateAlternativeComponent(ctx context.Context, queries *db.Queries, body models.AlternativeComponent) (models.AlternativeComponent, error) {
|
||||
result, err := queries.CreateAlternativeComponent(ctx, *mapper.ToModelAlternativeComponent(&body))
|
||||
if err != nil {
|
||||
return models.AlternativeComponent{}, err
|
||||
}
|
||||
return *mapper.ToDomainAlternativeComponent(result), nil
|
||||
}
|
||||
|
||||
func GetAlternativeComponentByID(ctx context.Context, queries *db.Queries, id int64) (models.AlternativeComponent, error) {
|
||||
result, err := queries.GetAlternativeComponentByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.AlternativeComponent{}, err
|
||||
}
|
||||
return *mapper.ToDomainAlternativeComponent(result), nil
|
||||
}
|
||||
|
||||
func ListAlternativeComponents(ctx context.Context, queries *db.Queries) ([]models.AlternativeComponent, error) {
|
||||
results, err := queries.ListAlternativeComponents(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.AlternativeComponent
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainAlternativeComponent(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateAlternativeComponent(ctx context.Context, queries *db.Queries, body models.AlternativeComponent) (models.AlternativeComponent, error) {
|
||||
result, err := queries.UpdateAlternativeComponent(ctx, *mapper.ToUpdateModelAlternativeComponent(&body))
|
||||
if err != nil {
|
||||
return models.AlternativeComponent{}, err
|
||||
}
|
||||
return *mapper.ToDomainAlternativeComponent(result), nil
|
||||
}
|
||||
|
||||
func DeleteAlternativeComponent(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
|
||||
rowsAffected, err := queries.DeleteAlternativeComponent(ctx, id)
|
||||
if err != nil {
|
||||
return rowsAffected, err
|
||||
}
|
||||
return rowsAffected, nil
|
||||
}
|
||||
52
internal/repositories/invoice_repository.go
Normal file
52
internal/repositories/invoice_repository.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"wm-backend/internal/mapper"
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
)
|
||||
|
||||
func CreateInvoice(ctx context.Context, queries *db.Queries, body models.Invoice) (models.Invoice, error) {
|
||||
result, err := queries.CreateInvoice(ctx, *mapper.ToModelInvoice(&body))
|
||||
if err != nil {
|
||||
return models.Invoice{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoice(result), nil
|
||||
}
|
||||
|
||||
func GetInvoiceByID(ctx context.Context, queries *db.Queries, id int64) (models.Invoice, error) {
|
||||
result, err := queries.GetInvoiceByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.Invoice{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoice(result), nil
|
||||
}
|
||||
|
||||
func ListInvoices(ctx context.Context, queries *db.Queries) ([]models.Invoice, error) {
|
||||
results, err := queries.ListInvoices(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.Invoice
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainInvoice(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateInvoice(ctx context.Context, queries *db.Queries, body models.Invoice) (models.Invoice, error) {
|
||||
result, err := queries.UpdateInvoice(ctx, *mapper.ToUpdateModelInvoice(&body))
|
||||
if err != nil {
|
||||
return models.Invoice{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoice(result), nil
|
||||
}
|
||||
|
||||
func DeleteInvoice(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
|
||||
rowsAffected, err := queries.DeleteInvoice(ctx, id)
|
||||
if err != nil {
|
||||
return rowsAffected, err
|
||||
}
|
||||
return rowsAffected, nil
|
||||
}
|
||||
@@ -129,6 +129,24 @@ func NewRouter() *gin.Engine {
|
||||
invoiceConfigItem.PUT("/:id", utils.AsyncHandler(services.InvoiceConfigItemUpdate))
|
||||
invoiceConfigItem.DELETE("/:id", utils.AsyncHandler(services.InvoiceConfigItemDelete))
|
||||
}
|
||||
|
||||
invoice := v1.Group(constants.API_GROUP_INVOICE)
|
||||
{
|
||||
invoice.GET("", utils.AsyncHandler(services.InvoiceList))
|
||||
invoice.GET("/:id", utils.AsyncHandler(services.InvoiceGetByID))
|
||||
invoice.POST("", utils.AsyncHandler(services.InvoiceCreate))
|
||||
invoice.PUT("/:id", utils.AsyncHandler(services.InvoiceUpdate))
|
||||
invoice.DELETE("/:id", utils.AsyncHandler(services.InvoiceDelete))
|
||||
}
|
||||
|
||||
alternativeComponent := v1.Group(constants.API_GROUP_ALTERNATIVE_COMPONENT)
|
||||
{
|
||||
alternativeComponent.GET("", utils.AsyncHandler(services.AlternativeComponentList))
|
||||
alternativeComponent.GET("/:id", utils.AsyncHandler(services.AlternativeComponentGetByID))
|
||||
alternativeComponent.POST("", utils.AsyncHandler(services.AlternativeComponentCreate))
|
||||
alternativeComponent.PUT("/:id", utils.AsyncHandler(services.AlternativeComponentUpdate))
|
||||
alternativeComponent.DELETE("/:id", utils.AsyncHandler(services.AlternativeComponentDelete))
|
||||
}
|
||||
}
|
||||
|
||||
r.GET(constants.API_PATH_PING, services.PingHandler)
|
||||
|
||||
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
|
||||
}
|
||||
201
internal/services/invoice_service.go
Normal file
201
internal/services/invoice_service.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user