changed arhitecture
This commit is contained in:
108
internal/lending/loansrepayment/db/postgresql.go
Normal file
108
internal/lending/loansrepayment/db/postgresql.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/lending/loansrepayment"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
generic *genericrepo.Repository[loansrepayment.LoanRepaymentDTO]
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) loansrepayment.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
generic: genericrepo.New(client, "loans l", loanColumns, 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 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")
|
||||
if err != nil {
|
||||
return loansrepayment.ListLoanRepaymentDTO{}, err
|
||||
}
|
||||
|
||||
return loansrepayment.ListLoanRepaymentDTO{Count: count, Loans: items}, nil
|
||||
}
|
||||
|
||||
func buildListQuery(filter loansrepayment.ListFilter) (whereClause string, args []interface{}) {
|
||||
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 "", args
|
||||
}
|
||||
|
||||
return " 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) {
|
||||
return r.generic.GetByID(ctx, 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)
|
||||
|
||||
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 {
|
||||
return r.generic.Delete(ctx, id)
|
||||
}
|
||||
|
||||
func scanLoanRepayment(row genericrepo.Scanner) (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
|
||||
}
|
||||
Reference in New Issue
Block a user