Return role IDs on users and related names on loan APIs.
Loan orders, paid-off letters, and repayments now include user, branch, type, and document names alongside foreign-key IDs. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -23,22 +23,53 @@ type repository struct {
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) loansrepayment.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
generic: genericrepo.New(client, "loans l", loanColumns, scanLoanRepayment),
|
||||
generic: genericrepo.New(client, "loans", loanReturningColumns, scanLoanRepayment),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
const loanColumns = `l.id, l.user_id, l.passport_serie, l.passport_id, l.account_number, l.created_at, l.updated_at`
|
||||
const userDisplaySQL = `COALESCE(NULLIF(TRIM(CONCAT_WS(' ', u.first_name, u.last_name)), ''), NULLIF(u.name, ''), u.username, '')`
|
||||
|
||||
const loanFromClause = ` FROM loans l
|
||||
LEFT JOIN users u ON u.id = l.user_id`
|
||||
|
||||
const loanColumns = `l.id, l.user_id, l.passport_serie, l.passport_id, l.account_number, l.created_at, l.updated_at, ` + userDisplaySQL
|
||||
|
||||
const loanReturningColumns = `id, user_id, passport_serie, passport_id, account_number, created_at, updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter loansrepayment.ListFilter) (loansrepayment.ListLoanRepaymentDTO, 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 loans l`+whereClause, args...).Scan(&count); err != nil {
|
||||
return loansrepayment.ListLoanRepaymentDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
query := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY l.id LIMIT $%d OFFSET $%d`,
|
||||
loanColumns, loanFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
|
||||
rows, err := r.client.Query(ctx, query, append(args, filter.Limit, offset)...)
|
||||
if err != nil {
|
||||
return loansrepayment.ListLoanRepaymentDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []loansrepayment.LoanRepaymentDTO
|
||||
for rows.Next() {
|
||||
item, err := scanLoanRepayment(rows)
|
||||
if err != nil {
|
||||
return loansrepayment.ListLoanRepaymentDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return loansrepayment.ListLoanRepaymentDTO{}, err
|
||||
}
|
||||
|
||||
return loansrepayment.ListLoanRepaymentDTO{Count: count, Loans: items}, nil
|
||||
}
|
||||
@@ -65,25 +96,36 @@ func likePattern(term string) string {
|
||||
}
|
||||
|
||||
func (r *repository) GetByID(ctx context.Context, id int64) (loansrepayment.LoanRepaymentDTO, error) {
|
||||
return r.generic.GetByID(ctx, id)
|
||||
query := `SELECT ` + loanColumns + loanFromClause + ` WHERE l.id = $1`
|
||||
return scanLoanRepayment(r.client.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto loansrepayment.CreateLoanRepaymentDTO) (loansrepayment.LoanRepaymentDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO loans (user_id, passport_serie, passport_id, account_number)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING %s`, loanReturningColumns)
|
||||
query := `
|
||||
WITH inserted AS (
|
||||
INSERT INTO loans (user_id, passport_serie, passport_id, account_number)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING ` + loanReturningColumns + `
|
||||
)
|
||||
SELECT ` + loanColumns + `
|
||||
FROM inserted l
|
||||
LEFT JOIN users u ON u.id = l.user_id`
|
||||
|
||||
return scanLoanRepayment(r.client.QueryRow(ctx, query, dto.UserId, dto.PassportSerie, dto.PassportId, dto.AccountNumber))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, id int64, dto loansrepayment.UpdateLoanRepaymentDTO) (loansrepayment.LoanRepaymentDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE loans SET
|
||||
user_id = $1, passport_serie = $2, passport_id = $3, account_number = $4,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $5
|
||||
RETURNING %s`, loanReturningColumns)
|
||||
query := `
|
||||
WITH updated AS (
|
||||
UPDATE loans SET
|
||||
user_id = $1, passport_serie = $2, passport_id = $3, account_number = $4,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $5
|
||||
RETURNING ` + loanReturningColumns + `
|
||||
)
|
||||
SELECT ` + loanColumns + `
|
||||
FROM updated l
|
||||
LEFT JOIN users u ON u.id = l.user_id`
|
||||
|
||||
item, err := scanLoanRepayment(r.client.QueryRow(ctx, query, dto.UserId, dto.PassportSerie, dto.PassportId, dto.AccountNumber, id))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
@@ -100,7 +142,7 @@ func scanLoanRepayment(row genericrepo.Scanner) (loansrepayment.LoanRepaymentDTO
|
||||
var item loansrepayment.LoanRepaymentDTO
|
||||
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.UserId, &item.PassportSerie, &item.PassportId, &item.AccountNumber, &item.CreatedAt, &item.UpdatedAt,
|
||||
&item.Id, &item.UserId, &item.PassportSerie, &item.PassportId, &item.AccountNumber, &item.CreatedAt, &item.UpdatedAt, &item.UserName,
|
||||
); err != nil {
|
||||
return loansrepayment.LoanRepaymentDTO{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user