Files
online-backend/internal/lending/loansdocument/db/postgresql.go
GuwandykA e0d03730de
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
added soft deleted
2026-09-05 17:58:23 +05:00

130 lines
3.8 KiB
Go

package db
import (
"context"
"errors"
"fmt"
"strings"
"online-back/internal/lending/loansdocument"
"online-back/internal/shared"
"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[loansdocument.LoanDocumentDTO]
logger *logging.Logger
}
func NewRepository(client postgresql.Client, logger *logging.Logger) loansdocument.Repository {
return &repository{
client: client,
generic: genericrepo.New(client, "loan_order_required_docs d", documentColumns, scanLoanDocument),
logger: logger,
}
}
const documentColumns = `d.id, d.name, d.value, d.active, d.created_at, d.updated_at`
const documentReturningColumns = `id, name, value, active, created_at, updated_at`
func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter) (loansdocument.ListLoanDocumentDTO, error) {
whereClause, args := buildListQuery(filter)
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
if err != nil {
return loansdocument.ListLoanDocumentDTO{}, err
}
return loansdocument.ListLoanDocumentDTO{Count: count, LoansDocuments: items}, nil
}
func buildListQuery(filter loansdocument.ListFilter) (whereClause string, args []interface{}) {
var conditions []string
if filter.DocumentName != "" {
args = append(args, shared.LikePattern(filter.DocumentName))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`d.name ILIKE $%d`, n))
}
if filter.Active != nil {
args = append(args, *filter.Active)
conditions = append(conditions, fmt.Sprintf("d.active = $%d", len(args)))
}
if len(conditions) == 0 {
return "", args
}
return " WHERE " + strings.Join(conditions, " AND "), args
}
func (r *repository) GetByID(ctx context.Context, id int64) (loansdocument.LoanDocumentDTO, error) {
return r.generic.GetByID(ctx, id)
}
func (r *repository) Create(ctx context.Context, dto loansdocument.CreateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
active := true
if dto.Active != nil {
active = *dto.Active
}
query := fmt.Sprintf(`
INSERT INTO loan_order_required_docs (name, value, active)
VALUES ($1, $2, $3)
RETURNING %s`, documentReturningColumns)
return scanLoanDocument(r.client.QueryRow(ctx, query, dto.Name, dto.Value, active))
}
func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.UpdateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
query := fmt.Sprintf(`
UPDATE loan_order_required_docs SET
name = $1, value = $2, active = $3,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $4 AND deleted_at IS NULL
RETURNING %s`, documentReturningColumns)
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, dto.Name, dto.Value, dto.Active, id))
if errors.Is(err, pgx.ErrNoRows) {
return loansdocument.LoanDocumentDTO{}, err
}
return item, err
}
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (loansdocument.LoanDocumentDTO, error) {
query := fmt.Sprintf(`
UPDATE loan_order_required_docs SET
active = $1,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $2 AND deleted_at IS NULL
RETURNING %s`, documentReturningColumns)
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, active, id))
if errors.Is(err, pgx.ErrNoRows) {
return loansdocument.LoanDocumentDTO{}, err
}
return item, err
}
func (r *repository) Delete(ctx context.Context, id int64) error {
return r.generic.Delete(ctx, id)
}
func scanLoanDocument(row genericrepo.Scanner) (loansdocument.LoanDocumentDTO, error) {
var item loansdocument.LoanDocumentDTO
if err := row.Scan(
&item.Id, &item.Name, &item.Value, &item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil {
return loansdocument.LoanDocumentDTO{}, err
}
return item, nil
}