Enrich cards API responses with joined relations and share common helpers.
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled

Nest user/branch/cardType/cardState on card, order, pin-order, and visa-master
reads; accept flexible dates/IDs on pin create; widen pin region; ignore undefined
list query params; extract internal/cards/shared to remove duplicated types and helpers.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
guwandykandCursor committed 2026-08-26 12:46:50 +05:00
1 parent 1fd0abbcb2
commit 0098422798
27 files changed
+942 -777

No files matched your search

+131 -41
View File
@@ -4,9 +4,9 @@ import (
"context"
"fmt"
"strings"
"time"
"online-back/internal/cards/cardorder"
"online-back/internal/cards/shared"
"online-back/internal/identity/rbac"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
@@ -41,6 +41,15 @@ var cardOrderColumnsSQL = strings.Join(cardOrderColumns, ", ")
var cardOrderWritableColumnsSQL = strings.Join(cardOrderWritableColumns, ", ")
const cardOrderFromClause = ` FROM card_orders co
LEFT JOIN card_states cs ON cs.id = co.card_state_id
LEFT JOIN card_types ct ON ct.id = co.card_type_id
LEFT JOIN branches b ON b.id = co.branch_id
LEFT JOIN users u ON u.id = co.user_id`
var cardOrderSelectColumns = shared.PrefixedColumns("co", cardOrderColumns) + `,
cs.name, ct.name, b.name, ` + shared.UserDisplaySQL + `, COALESCE(u.username, ''), COALESCE(u.phone, ''), COALESCE(u.email, '')`
func resolveStatus(status *string) *string {
if status == nil || strings.TrimSpace(*status) == "" {
pending := "pending"
@@ -59,46 +68,56 @@ func resolvePaid(paid *bool) bool {
func createArgs(dto cardorder.CreateCardOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, dto.CardStateId.Int64(), dto.CardTypeId.Int64(), dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, optionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.OptionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy, dto.BornPlace,
dto.JobLocation, dto.PassportAddress, dto.RealAddress, dto.Phone, dto.PhoneAdditional,
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, optionalInt64(dto.UserId), resolvePaid(dto.Paid),
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, shared.OptionalInt64(dto.UserId), resolvePaid(dto.Paid),
}
}
func updateArgs(dto cardorder.UpdateCardOrderDTO) []interface{} {
return []interface{}{
dto.UniqueId, dto.CardStateId.Int64(), dto.CardTypeId.Int64(), dto.Region, dto.BranchId.Int64(),
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, optionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
dto.CustomerName, dto.CustomerSurname, dto.CustomerPatronicName, shared.OptionalTime(dto.BornAt), dto.OldSurname, dto.Citizenship,
dto.PassportSerie, dto.PassportId, dto.PassportGivenAt.Time(), dto.PassportGivenBy, dto.BornPlace,
dto.JobLocation, dto.PassportAddress, dto.RealAddress, dto.Phone, dto.PhoneAdditional,
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, optionalInt64(dto.UserId), dto.Paid,
resolveStatus(dto.Status), dto.PassportOne, dto.PassportTwo, dto.PassportThree, dto.PassportFour, dto.Notes, shared.OptionalInt64(dto.UserId), dto.Paid,
}
}
func optionalTime(t *cardorder.FlexibleTime) *time.Time {
if t == nil || t.IsZero() {
return nil
}
tt := t.Time()
return &tt
}
func optionalInt64(n *cardorder.FlexibleInt64) *int64 {
if n == nil {
return nil
}
v := n.Int64()
return &v
}
func (r *repository) List(ctx context.Context, filter cardorder.ListFilter) (cardorder.ListCardOrderDTO, error) {
whereClause, args := buildListQuery(filter)
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
var count int64
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) FROM card_orders co`+whereClause, args...).Scan(&count); err != nil {
return cardorder.ListCardOrderDTO{}, err
}
offset := (filter.Page - 1) * filter.Limit
limitPlaceholder := len(args) + 1
offsetPlaceholder := len(args) + 2
query := fmt.Sprintf(
`SELECT %s%s%s ORDER BY co.id LIMIT $%d OFFSET $%d`,
cardOrderSelectColumns, cardOrderFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
)
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
if err != nil {
return cardorder.ListCardOrderDTO{}, err
}
defer rows.Close()
var items []cardorder.CardOrderDTO
for rows.Next() {
item, err := scanCardOrderWithJoins(rows)
if err != nil {
return cardorder.ListCardOrderDTO{}, err
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return cardorder.ListCardOrderDTO{}, err
}
return cardorder.ListCardOrderDTO{Count: count, CardOrders: items}, nil
}
@@ -108,34 +127,34 @@ func buildListQuery(filter cardorder.ListFilter) (whereClause string, args []int
if filter.Status != nil {
args = append(args, *filter.Status)
conditions = append(conditions, fmt.Sprintf("status = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.status = $%d", len(args)))
}
if len(filter.BranchIds) > 0 {
conditions, args = rbac.AppendBranchIDs(conditions, args, "branch_id", filter.BranchIds)
conditions, args = rbac.AppendBranchIDs(conditions, args, "co.branch_id", filter.BranchIds)
} else if filter.BranchId != nil {
args = append(args, *filter.BranchId)
conditions = append(conditions, fmt.Sprintf("branch_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.branch_id = $%d", len(args)))
}
if filter.UserId != nil {
args = append(args, *filter.UserId)
conditions = append(conditions, fmt.Sprintf("user_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.user_id = $%d", len(args)))
}
if filter.CardStateId != nil {
args = append(args, *filter.CardStateId)
conditions = append(conditions, fmt.Sprintf("card_state_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.card_state_id = $%d", len(args)))
}
if filter.CardTypeId != nil {
args = append(args, *filter.CardTypeId)
conditions = append(conditions, fmt.Sprintf("card_type_id = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.card_type_id = $%d", len(args)))
}
if filter.Region != "" {
args = append(args, filter.Region)
conditions = append(conditions, fmt.Sprintf("region = $%d", len(args)))
conditions = append(conditions, fmt.Sprintf("co.region = $%d", len(args)))
}
if len(conditions) == 0 {
@@ -146,7 +165,8 @@ func buildListQuery(filter cardorder.ListFilter) (whereClause string, args []int
}
func (r *repository) GetByID(ctx context.Context, id int64) (cardorder.CardOrderDTO, error) {
return r.generic.GetByID(ctx, id)
query := `SELECT ` + cardOrderSelectColumns + cardOrderFromClause + ` WHERE co.id = $1`
return scanCardOrderWithJoins(r.client.QueryRow(ctx, query, id))
}
func (r *repository) Create(ctx context.Context, dto cardorder.CreateCardOrderDTO) (cardorder.CardOrderDTO, error) {
@@ -157,12 +177,22 @@ func (r *repository) Create(ctx context.Context, dto cardorder.CreateCardOrderDT
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
query := fmt.Sprintf(
`INSERT INTO card_orders (%s) VALUES (%s) RETURNING %s`,
cardOrderWritableColumnsSQL, strings.Join(placeholders, ", "), cardOrderColumnsSQL,
query := fmt.Sprintf(`
WITH inserted AS (
INSERT INTO card_orders (%s) VALUES (%s)
RETURNING %s
)
SELECT %s
FROM inserted co
LEFT JOIN card_states cs ON cs.id = co.card_state_id
LEFT JOIN card_types ct ON ct.id = co.card_type_id
LEFT JOIN branches b ON b.id = co.branch_id
LEFT JOIN users u ON u.id = co.user_id`,
cardOrderWritableColumnsSQL, strings.Join(placeholders, ", "),
cardOrderColumnsSQL, cardOrderSelectColumns,
)
return scanCardOrder(r.client.QueryRow(ctx, query, args...))
return scanCardOrderWithJoins(r.client.QueryRow(ctx, query, args...))
}
func (r *repository) Update(ctx context.Context, id int64, dto cardorder.UpdateCardOrderDTO) (cardorder.CardOrderDTO, error) {
@@ -174,12 +204,22 @@ func (r *repository) Update(ctx context.Context, id int64, dto cardorder.UpdateC
}
args = append(args, id)
query := fmt.Sprintf(
`UPDATE card_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $%d RETURNING %s`,
strings.Join(assignments, ", "), len(args), cardOrderColumnsSQL,
query := fmt.Sprintf(`
WITH updated AS (
UPDATE card_orders SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $%d
RETURNING %s
)
SELECT %s
FROM updated co
LEFT JOIN card_states cs ON cs.id = co.card_state_id
LEFT JOIN card_types ct ON ct.id = co.card_type_id
LEFT JOIN branches b ON b.id = co.branch_id
LEFT JOIN users u ON u.id = co.user_id`,
strings.Join(assignments, ", "), len(args), cardOrderColumnsSQL, cardOrderSelectColumns,
)
item, err := scanCardOrder(r.client.QueryRow(ctx, query, args...))
item, err := scanCardOrderWithJoins(r.client.QueryRow(ctx, query, args...))
if err != nil {
return cardorder.CardOrderDTO{}, err
}
@@ -191,17 +231,67 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
}
func scanCardOrder(row genericrepo.Scanner) (cardorder.CardOrderDTO, error) {
return scanCardOrderRow(row, false)
}
func scanCardOrderWithJoins(row genericrepo.Scanner) (cardorder.CardOrderDTO, error) {
return scanCardOrderRow(row, true)
}
func scanCardOrderRow(row genericrepo.Scanner, withJoins bool) (cardorder.CardOrderDTO, error) {
var item cardorder.CardOrderDTO
err := row.Scan(
dest := []interface{}{
&item.Id, &item.UniqueId, &item.CardStateId, &item.CardTypeId, &item.Region, &item.BranchId,
&item.CustomerName, &item.CustomerSurname, &item.CustomerPatronicName, &item.BornAt, &item.OldSurname, &item.Citizenship,
&item.PassportSerie, &item.PassportId, &item.PassportGivenAt, &item.PassportGivenBy, &item.BornPlace,
&item.JobLocation, &item.PassportAddress, &item.RealAddress, &item.Phone, &item.PhoneAdditional,
&item.Status, &item.PassportOne, &item.PassportTwo, &item.PassportThree, &item.PassportFour, &item.Notes, &item.UserId, &item.Paid,
&item.CreatedAt, &item.UpdatedAt,
)
if err != nil {
}
var cardStateNameJSON, cardTypeNameJSON, branchNameJSON []byte
var userName, userUsername, userPhone, userEmail string
if withJoins {
dest = append(dest,
&cardStateNameJSON, &cardTypeNameJSON, &branchNameJSON,
&userName, &userUsername, &userPhone, &userEmail,
)
}
if err := row.Scan(dest...); err != nil {
return cardorder.CardOrderDTO{}, err
}
if !withJoins {
return item, nil
}
var cardStateName, cardTypeName, branchName shared.LocalizedText
if err := shared.UnmarshalLocalized(cardStateNameJSON, &cardStateName); err != nil {
return cardorder.CardOrderDTO{}, err
}
if err := shared.UnmarshalLocalized(cardTypeNameJSON, &cardTypeName); err != nil {
return cardorder.CardOrderDTO{}, err
}
if err := shared.UnmarshalLocalized(branchNameJSON, &branchName); err != nil {
return cardorder.CardOrderDTO{}, err
}
if item.CardStateId != 0 {
item.CardState = &shared.NamedInfo{Id: item.CardStateId, Name: cardStateName}
}
if item.CardTypeId != 0 {
item.CardType = &shared.NamedInfo{Id: item.CardTypeId, Name: cardTypeName}
}
if item.BranchId != 0 {
item.Branch = &shared.NamedInfo{Id: item.BranchId, Name: branchName}
}
if item.UserId != nil {
item.User = &shared.UserInfo{
Id: *item.UserId,
Name: userName,
Username: userUsername,
Phone: userPhone,
Email: userEmail,
}
}
return item, nil
}
+96 -196
View File
@@ -1,212 +1,112 @@
package cardorder
import (
"encoding/json"
"strconv"
"strings"
"time"
"online-back/internal/cards/shared"
)
type FlexibleTime time.Time
func (t FlexibleTime) Time() time.Time { return time.Time(t) }
func (t FlexibleTime) IsZero() bool { return time.Time(t).IsZero() }
func (t FlexibleTime) MarshalJSON() ([]byte, error) {
return time.Time(t).MarshalJSON()
}
func (t *FlexibleTime) UnmarshalJSON(b []byte) error {
*t = FlexibleTime{}
raw := strings.TrimSpace(string(b))
if raw == "" || raw == "null" {
return nil
}
var n json.Number
if err := json.Unmarshal(b, &n); err == nil {
s := n.String()
if parsed, ok := parseFlexibleTime(s); ok {
*t = parsed
return nil
}
if unix, err := n.Int64(); err == nil && unix > 0 {
*t = FlexibleTime(time.Unix(unix, 0).UTC())
}
return nil
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return nil
}
if parsed, ok := parseFlexibleTime(s); ok {
*t = parsed
}
return nil
}
func parseFlexibleTime(s string) (FlexibleTime, bool) {
s = strings.TrimSpace(s)
if s == "" {
return FlexibleTime{}, false
}
switch strings.ToLower(s) {
case "string", "undefined", "null", "none", "invalid", "nan", "-":
return FlexibleTime{}, false
}
for _, layout := range []string{
time.RFC3339Nano,
time.RFC3339,
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05.000Z",
"2006-01-02T15:04:05",
"2006-01-02 15:04:05",
"2006-01-02",
"02.01.2006",
"02/01/2006",
"02-01-2006",
"2006/01/02",
"20060102",
} {
if parsed, err := time.Parse(layout, s); err == nil {
return FlexibleTime(parsed), true
}
}
return FlexibleTime{}, false
}
type FlexibleInt64 int64
func (n FlexibleInt64) Int64() int64 { return int64(n) }
func (n *FlexibleInt64) UnmarshalJSON(b []byte) error {
if string(b) == "null" {
*n = 0
return nil
}
var v int64
if err := json.Unmarshal(b, &v); err == nil {
*n = FlexibleInt64(v)
return nil
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
s = strings.TrimSpace(s)
if s == "" {
*n = 0
return nil
}
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
*n = FlexibleInt64(v)
return nil
}
type CardOrderDTO struct {
Id int64 `json:"id"`
UniqueId *string `json:"uniqueId"`
CardStateId int64 `json:"cardStateId"`
CardTypeId int64 `json:"cardTypeId"`
Region string `json:"region"`
BranchId int64 `json:"branchId"`
CustomerName string `json:"customerName"`
CustomerSurname string `json:"customerSurname"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *time.Time `json:"bornAt"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship"`
PassportSerie string `json:"passportSerie"`
PassportId string `json:"passportId"`
PassportGivenAt time.Time `json:"passportGivenAt"`
PassportGivenBy string `json:"passportGivenBy"`
BornPlace string `json:"bornPlace"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne"`
PassportTwo string `json:"passportTwo"`
PassportThree string `json:"passportThree"`
PassportFour string `json:"passportFour"`
Notes *string `json:"notes"`
UserId *int64 `json:"userId"`
Paid bool `json:"paid"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Id int64 `json:"id"`
UniqueId *string `json:"uniqueId"`
CardStateId int64 `json:"-"`
CardState *shared.NamedInfo `json:"cardState,omitempty"`
CardTypeId int64 `json:"-"`
CardType *shared.NamedInfo `json:"cardType,omitempty"`
Region string `json:"region"`
BranchId int64 `json:"-"`
Branch *shared.NamedInfo `json:"branch,omitempty"`
CustomerName string `json:"customerName"`
CustomerSurname string `json:"customerSurname"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *time.Time `json:"bornAt"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship"`
PassportSerie string `json:"passportSerie"`
PassportId string `json:"passportId"`
PassportGivenAt time.Time `json:"passportGivenAt"`
PassportGivenBy string `json:"passportGivenBy"`
BornPlace string `json:"bornPlace"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne"`
PassportTwo string `json:"passportTwo"`
PassportThree string `json:"passportThree"`
PassportFour string `json:"passportFour"`
Notes *string `json:"notes"`
UserId *int64 `json:"-"`
User *shared.UserInfo `json:"user,omitempty"`
Paid bool `json:"paid"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type CreateCardOrderDTO struct {
UniqueId *string `json:"uniqueId"`
CardStateId FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
CardTypeId FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
Region string `json:"region" binding:"required"`
BranchId FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
CustomerName string `json:"customerName" binding:"required"`
CustomerSurname string `json:"customerSurname" binding:"required"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship" binding:"required"`
PassportSerie string `json:"passportSerie" binding:"required"`
PassportId string `json:"passportId" binding:"required"`
PassportGivenAt FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
BornPlace string `json:"bornPlace" binding:"required"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne" binding:"required"`
PassportTwo string `json:"passportTwo" binding:"required"`
PassportThree string `json:"passportThree" binding:"required"`
PassportFour string `json:"passportFour" binding:"required"`
Notes *string `json:"notes"`
UserId *FlexibleInt64 `json:"userId" swaggertype:"integer"`
Paid *bool `json:"paid"`
UniqueId *string `json:"uniqueId"`
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
Region string `json:"region" binding:"required"`
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
CustomerName string `json:"customerName" binding:"required"`
CustomerSurname string `json:"customerSurname" binding:"required"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship" binding:"required"`
PassportSerie string `json:"passportSerie" binding:"required"`
PassportId string `json:"passportId" binding:"required"`
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
BornPlace string `json:"bornPlace" binding:"required"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne" binding:"required"`
PassportTwo string `json:"passportTwo" binding:"required"`
PassportThree string `json:"passportThree" binding:"required"`
PassportFour string `json:"passportFour" binding:"required"`
Notes *string `json:"notes"`
UserId *shared.FlexibleInt64 `json:"userId" swaggertype:"integer"`
Paid *bool `json:"paid"`
}
type UpdateCardOrderDTO struct {
UniqueId *string `json:"uniqueId"`
CardStateId FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
CardTypeId FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
Region string `json:"region" binding:"required"`
BranchId FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
CustomerName string `json:"customerName" binding:"required"`
CustomerSurname string `json:"customerSurname" binding:"required"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship" binding:"required"`
PassportSerie string `json:"passportSerie" binding:"required"`
PassportId string `json:"passportId" binding:"required"`
PassportGivenAt FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
BornPlace string `json:"bornPlace" binding:"required"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne" binding:"required"`
PassportTwo string `json:"passportTwo" binding:"required"`
PassportThree string `json:"passportThree" binding:"required"`
PassportFour string `json:"passportFour" binding:"required"`
Notes *string `json:"notes"`
UserId *FlexibleInt64 `json:"userId" swaggertype:"integer"`
Paid bool `json:"paid"`
UniqueId *string `json:"uniqueId"`
CardStateId shared.FlexibleInt64 `json:"cardStateId" binding:"required" swaggertype:"integer"`
CardTypeId shared.FlexibleInt64 `json:"cardTypeId" binding:"required" swaggertype:"integer"`
Region string `json:"region" binding:"required"`
BranchId shared.FlexibleInt64 `json:"branchId" binding:"required" swaggertype:"integer"`
CustomerName string `json:"customerName" binding:"required"`
CustomerSurname string `json:"customerSurname" binding:"required"`
CustomerPatronicName *string `json:"customerPatronicName"`
BornAt *shared.FlexibleTime `json:"bornAt" swaggertype:"string" format:"date"`
OldSurname *string `json:"oldSurname"`
Citizenship string `json:"citizenship" binding:"required"`
PassportSerie string `json:"passportSerie" binding:"required"`
PassportId string `json:"passportId" binding:"required"`
PassportGivenAt shared.FlexibleTime `json:"passportGivenAt" swaggertype:"string" format:"date"`
PassportGivenBy string `json:"passportGivenBy" binding:"required"`
BornPlace string `json:"bornPlace" binding:"required"`
JobLocation *string `json:"jobLocation"`
PassportAddress *string `json:"passportAddress"`
RealAddress *string `json:"realAddress"`
Phone *string `json:"phone"`
PhoneAdditional *string `json:"phoneAdditional"`
Status *string `json:"status"`
PassportOne string `json:"passportOne" binding:"required"`
PassportTwo string `json:"passportTwo" binding:"required"`
PassportThree string `json:"passportThree" binding:"required"`
PassportFour string `json:"passportFour" binding:"required"`
Notes *string `json:"notes"`
UserId *shared.FlexibleInt64 `json:"userId" swaggertype:"integer"`
Paid bool `json:"paid"`
}
type ListFilter struct {
+14 -68
View File
@@ -3,10 +3,9 @@ package cardorder
import (
"errors"
"net/http"
"strconv"
"strings"
"online-back/internal/appresult"
"online-back/internal/cards/shared"
"online-back/internal/handlers"
"online-back/internal/identity/rbac"
"online-back/pkg/logging"
@@ -83,7 +82,7 @@ func (h *handler) list(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /card-orders/{id} [get]
func (h *handler) getByID(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -151,7 +150,7 @@ func (h *handler) create(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /card-orders/{id} [put]
func (h *handler) update(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -195,7 +194,7 @@ func (h *handler) update(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /card-orders/{id} [delete]
func (h *handler) delete(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -217,74 +216,42 @@ func (h *handler) delete(c *gin.Context) {
c.JSON(http.StatusOK, appresult.SuccessWithData("card order removed"))
}
func parseID(raw string) (int64, bool) {
id, err := strconv.ParseInt(raw, 10, 64)
if err != nil || id <= 0 {
return 0, false
}
return id, true
}
const (
defaultPage = 1
defaultLimit = 10
maxLimit = 100
)
func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter := ListFilter{
Page: defaultPage,
Limit: defaultLimit,
page, limit, ok := shared.ParsePageLimit(c)
if !ok {
return ListFilter{}, false
}
filter := ListFilter{Page: page, Limit: limit}
if id, ok := optionalQueryID(c, "branch_id", "branchId"); !ok {
if id, ok := shared.OptionalQueryID(c, "branch_id", "branchId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.BranchId = id
}
if id, ok := optionalQueryID(c, "user_id", "userId"); !ok {
if id, ok := shared.OptionalQueryID(c, "user_id", "userId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.UserId = id
}
if id, ok := optionalQueryID(c, "card_state_id", "cardStateId"); !ok {
if id, ok := shared.OptionalQueryID(c, "card_state_id", "cardStateId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.CardStateId = id
}
if id, ok := optionalQueryID(c, "card_type_id", "cardTypeId"); !ok {
if id, ok := shared.OptionalQueryID(c, "card_type_id", "cardTypeId"); !ok {
return ListFilter{}, false
} else if id != nil {
filter.CardTypeId = id
}
if raw := queryValue(c, "page"); raw != "" {
page, err := strconv.Atoi(raw)
if err != nil || page < 1 {
return ListFilter{}, false
}
filter.Page = page
}
if raw := queryValue(c, "limit"); raw != "" {
limit, err := strconv.Atoi(raw)
if err != nil || limit < 1 {
return ListFilter{}, false
}
if limit > maxLimit {
limit = maxLimit
}
filter.Limit = limit
}
if raw := queryValue(c, "status"); raw != "" {
if raw := shared.QueryValue(c, "status"); raw != "" {
filter.Status = &raw
}
if raw := queryValue(c, "region"); raw != "" {
if raw := shared.QueryValue(c, "region"); raw != "" {
filter.Region = raw
}
@@ -307,24 +274,3 @@ func (h *handler) guardBranchByID(c *gin.Context, id int64) bool {
}
return rbac.GuardBranch(c, item.BranchId)
}
func queryValue(c *gin.Context, keys ...string) string {
for _, key := range keys {
if raw := strings.TrimSpace(c.Query(key)); raw != "" && raw != "undefined" && raw != "null" {
return raw
}
}
return ""
}
func optionalQueryID(c *gin.Context, keys ...string) (*int64, bool) {
raw := queryValue(c, keys...)
if raw == "" {
return nil, true
}
id, ok := parseID(raw)
if !ok {
return nil, false
}
return &id, true
}