147 lines
4.5 KiB
Go
147 lines
4.5 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/admin/loansrepayment"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) loansrepayment.Repository {
|
|
return &repository{client: client, 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 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) {
|
|
fromClause, whereClause, args := buildListQuery(filter)
|
|
|
|
var count int64
|
|
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) `+fromClause+whereClause, args...).Scan(&count); err != nil {
|
|
return loansrepayment.ListLoanRepaymentDTO{}, err
|
|
}
|
|
|
|
offset := (filter.Page - 1) * filter.Limit
|
|
limitPlaceholder := len(args) + 1
|
|
offsetPlaceholder := len(args) + 2
|
|
listQuery := fmt.Sprintf(
|
|
`SELECT %s%s%s ORDER BY l.id LIMIT $%d OFFSET $%d`,
|
|
loanColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
|
)
|
|
|
|
rows, err := r.client.Query(ctx, listQuery, 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
|
|
}
|
|
|
|
func buildListQuery(filter loansrepayment.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
|
fromClause = ` FROM loans l`
|
|
|
|
var conditions []string
|
|
if filter.PassportId != "" {
|
|
args = append(args, likePattern(filter.PassportId))
|
|
conditions = append(conditions, fmt.Sprintf("l.passport_id ILIKE $%d", len(args)))
|
|
}
|
|
|
|
if len(conditions) == 0 {
|
|
return fromClause, "", args
|
|
}
|
|
|
|
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args
|
|
}
|
|
|
|
func likePattern(term string) string {
|
|
term = strings.ReplaceAll(term, `\`, `\\`)
|
|
term = strings.ReplaceAll(term, `%`, `\%`)
|
|
term = strings.ReplaceAll(term, `_`, `\_`)
|
|
return "%" + term + "%"
|
|
}
|
|
|
|
func (r *repository) GetByID(ctx context.Context, id int64) (loansrepayment.LoanRepaymentDTO, error) {
|
|
query := fmt.Sprintf(`SELECT %s FROM loans l WHERE l.id = $1`, loanColumns)
|
|
item, err := scanLoanRepayment(r.client.QueryRow(ctx, query, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return loansrepayment.LoanRepaymentDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
item, err := scanLoanRepayment(r.client.QueryRow(ctx, query, dto.UserId, dto.PassportSerie, dto.PassportId, dto.AccountNumber, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return loansrepayment.LoanRepaymentDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM loans WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return pgx.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type scannable interface {
|
|
Scan(dest ...interface{}) error
|
|
}
|
|
|
|
func scanLoanRepayment(row scannable) (loansrepayment.LoanRepaymentDTO, error) {
|
|
var item loansrepayment.LoanRepaymentDTO
|
|
|
|
if err := row.Scan(
|
|
&item.Id, &item.UserId, &item.PassportSerie, &item.PassportId, &item.AccountNumber, &item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return loansrepayment.LoanRepaymentDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|