feat: add invoice-configs management functionality
This commit is contained in:
50
internal/mapper/invoice_config_mapper.go
Normal file
50
internal/mapper/invoice_config_mapper.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package mapper
|
||||
|
||||
import (
|
||||
"wm-backend/internal/models"
|
||||
db "wm-backend/sqlc_gen"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func ToDomainInvoiceConfig(r db.InvoiceConfig) *models.InvoiceConfig {
|
||||
return &models.InvoiceConfig{
|
||||
ID: r.ID,
|
||||
Name: r.Name,
|
||||
Type: string(r.Type),
|
||||
Description: r.Description.String,
|
||||
IsActive: r.IsActive,
|
||||
Metadata: r.Metadata,
|
||||
CreatedAt: r.CreatedAt,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToModelInvoiceConfig(r *models.InvoiceConfig) *db.CreateInvoiceConfigParams {
|
||||
return &db.CreateInvoiceConfigParams{
|
||||
Name: r.Name,
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
IsActive: r.IsActive,
|
||||
Metadata: r.Metadata,
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ToUpdateModelInvoiceConfig(r *models.InvoiceConfig) *db.UpdateInvoiceConfigParams {
|
||||
return &db.UpdateInvoiceConfigParams{
|
||||
Name: r.Name,
|
||||
Type: db.InvoiceTypeEnum(r.Type),
|
||||
Description: pgtype.Text{
|
||||
String: r.Description,
|
||||
Valid: r.Description != "",
|
||||
},
|
||||
IsActive: r.IsActive,
|
||||
Metadata: r.Metadata,
|
||||
UpdatedAt: r.UpdatedAt,
|
||||
ID: r.ID,
|
||||
}
|
||||
}
|
||||
14
internal/models/invoice_config_model.go
Normal file
14
internal/models/invoice_config_model.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type InvoiceConfig struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description"`
|
||||
IsActive bool `json:"isActive"`
|
||||
Metadata []byte `json:"metadata"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
}
|
||||
15
internal/models/requests/invoice_config_request.go
Normal file
15
internal/models/requests/invoice_config_request.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package requests
|
||||
|
||||
type CreateInvoiceConfigRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
IsActive bool `json:"isActive"`
|
||||
}
|
||||
|
||||
type UpdateInvoiceConfigRequest struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description"`
|
||||
IsActive *bool `json:"isActive"`
|
||||
}
|
||||
13
internal/models/responses/invoice_config_response.go
Normal file
13
internal/models/responses/invoice_config_response.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package responses
|
||||
|
||||
type CreateInvoiceConfigResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
}
|
||||
|
||||
type UpdateInvoiceConfigResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Description string `json:"description"`
|
||||
IsActive bool `json:"isActive"`
|
||||
}
|
||||
52
internal/repositories/invoice_config_repository.go
Normal file
52
internal/repositories/invoice_config_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 CreateInvoiceConfig(ctx context.Context, queries *db.Queries, body models.InvoiceConfig) (models.InvoiceConfig, error) {
|
||||
result, err := queries.CreateInvoiceConfig(ctx, *mapper.ToModelInvoiceConfig(&body))
|
||||
if err != nil {
|
||||
return models.InvoiceConfig{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoiceConfig(result), nil
|
||||
}
|
||||
|
||||
func GetInvoiceConfigByID(ctx context.Context, queries *db.Queries, id int64) (models.InvoiceConfig, error) {
|
||||
result, err := queries.GetInvoiceConfigByID(ctx, id)
|
||||
if err != nil {
|
||||
return models.InvoiceConfig{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoiceConfig(result), nil
|
||||
}
|
||||
|
||||
func ListInvoiceConfigs(ctx context.Context, queries *db.Queries) ([]models.InvoiceConfig, error) {
|
||||
results, err := queries.ListInvoiceConfigs(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var items []models.InvoiceConfig
|
||||
for _, r := range results {
|
||||
items = append(items, *mapper.ToDomainInvoiceConfig(r))
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func UpdateInvoiceConfig(ctx context.Context, queries *db.Queries, body models.InvoiceConfig) (models.InvoiceConfig, error) {
|
||||
result, err := queries.UpdateInvoiceConfig(ctx, *mapper.ToUpdateModelInvoiceConfig(&body))
|
||||
if err != nil {
|
||||
return models.InvoiceConfig{}, err
|
||||
}
|
||||
return *mapper.ToDomainInvoiceConfig(result), nil
|
||||
}
|
||||
|
||||
func DeleteInvoiceConfig(ctx context.Context, queries *db.Queries, id int64) (int64, error) {
|
||||
rowsAffected, err := queries.DeleteInvoiceConfig(ctx, id)
|
||||
if err != nil {
|
||||
return rowsAffected, err
|
||||
}
|
||||
return rowsAffected, nil
|
||||
}
|
||||
@@ -111,6 +111,15 @@ func NewRouter() *gin.Engine {
|
||||
componentItem.PUT("/:id", utils.AsyncHandler(services.ComponentItemUpdate))
|
||||
componentItem.DELETE("/:id", utils.AsyncHandler(services.ComponentItemDelete))
|
||||
}
|
||||
|
||||
invoiceConfig := v1.Group(constants.API_GROUP_INVOICE_CONFIG)
|
||||
{
|
||||
invoiceConfig.GET("", utils.AsyncHandler(services.InvoiceConfigList))
|
||||
invoiceConfig.GET("/:id", utils.AsyncHandler(services.InvoiceConfigGetByID))
|
||||
invoiceConfig.POST("", utils.AsyncHandler(services.InvoiceConfigCreate))
|
||||
invoiceConfig.PUT("/:id", utils.AsyncHandler(services.InvoiceConfigUpdate))
|
||||
invoiceConfig.DELETE("/:id", utils.AsyncHandler(services.InvoiceConfigDelete))
|
||||
}
|
||||
}
|
||||
|
||||
r.GET(constants.API_PATH_PING, services.PingHandler)
|
||||
|
||||
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