Refactored transaction input handling and removed unused receipt-related definitions in Swagger.
This commit is contained in:
@@ -6,13 +6,19 @@ import (
|
||||
"FamilyHub/src/domain"
|
||||
"FamilyHub/src/utils"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PhotoCreateTransactionFields struct {
|
||||
Image []byte
|
||||
FamilyID *int64
|
||||
CreatedBy *int64
|
||||
Type *string
|
||||
Category *string
|
||||
Description *string
|
||||
}
|
||||
|
||||
func BuildCreateTransactionInput(req dto.CreateTransactionRequest) (services.CreateTransactionInput, error) {
|
||||
if req.ReceiptNumber != nil || req.ReceiptDate != nil {
|
||||
receiptReq, err := BuildReceiptTransactionRequest(req)
|
||||
@@ -30,24 +36,15 @@ func BuildCreateTransactionInput(req dto.CreateTransactionRequest) (services.Cre
|
||||
return services.CreateTransactionInput{Manual: &manualReq}, nil
|
||||
}
|
||||
|
||||
func BuildPhotoCreateTransactionInput(c *gin.Context, image []byte) (services.CreateTransactionInput, error) {
|
||||
familyID, err := parseOptionalInt64Form(c, "family_id")
|
||||
if err != nil {
|
||||
return services.CreateTransactionInput{}, err
|
||||
}
|
||||
createdBy, err := parseOptionalInt64Form(c, "created_by")
|
||||
if err != nil {
|
||||
return services.CreateTransactionInput{}, err
|
||||
}
|
||||
|
||||
func BuildPhotoCreateTransactionInput(fields PhotoCreateTransactionFields) (services.CreateTransactionInput, error) {
|
||||
return services.CreateTransactionInput{
|
||||
Photo: &services.CreateTransactionPhotoInput{
|
||||
Image: image,
|
||||
FamilyID: familyID,
|
||||
CreatedBy: createdBy,
|
||||
Type: parseOptionalStringForm(c, "type"),
|
||||
Category: parseOptionalStringForm(c, "category"),
|
||||
Description: parseOptionalStringForm(c, "description"),
|
||||
Image: fields.Image,
|
||||
FamilyID: fields.FamilyID,
|
||||
CreatedBy: fields.CreatedBy,
|
||||
Type: trimOptionalString(fields.Type),
|
||||
Category: trimOptionalString(fields.Category),
|
||||
Description: trimOptionalString(fields.Description),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -128,26 +125,3 @@ func trimOptionalString(value *string) *string {
|
||||
|
||||
return &trimmed
|
||||
}
|
||||
|
||||
func parseOptionalInt64Form(c *gin.Context, key string) (*int64, error) {
|
||||
value := strings.TrimSpace(c.PostForm(key))
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil {
|
||||
return nil, errors.New(key + " must be int64")
|
||||
}
|
||||
|
||||
return &parsed, nil
|
||||
}
|
||||
|
||||
func parseOptionalStringForm(c *gin.Context, key string) *string {
|
||||
value := strings.TrimSpace(c.PostForm(key))
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &value
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user