Rename users table fields and loan documents table, expand branch billing credentials, switch to /api/admin prefix, refactor auth endpoints, replace PATCH with PUT for status updates, remove file upload APIs, and regenerate Swagger with ordered tags. Co-authored-by: Cursor <cursoragent@cursor.com>
190 lines
5.6 KiB
Go
190 lines
5.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/admin/loansdocument"
|
|
"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) loansdocument.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
const documentColumns = `d.id, d.title, d.content_en, d.content_ru, d.content_tm, d.status, d.created_at, d.updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter) (loansdocument.ListLoanDocumentDTO, 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 loansdocument.ListLoanDocumentDTO{}, err
|
|
}
|
|
|
|
offset := (filter.Page - 1) * filter.Limit
|
|
limitPlaceholder := len(args) + 1
|
|
offsetPlaceholder := len(args) + 2
|
|
listQuery := fmt.Sprintf(
|
|
`SELECT %s%s%s ORDER BY d.id LIMIT $%d OFFSET $%d`,
|
|
documentColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
|
)
|
|
|
|
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
|
if err != nil {
|
|
return loansdocument.ListLoanDocumentDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []loansdocument.LoanDocumentDTO
|
|
for rows.Next() {
|
|
item, err := scanLoanDocument(rows)
|
|
if err != nil {
|
|
return loansdocument.ListLoanDocumentDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return loansdocument.ListLoanDocumentDTO{}, err
|
|
}
|
|
|
|
return loansdocument.ListLoanDocumentDTO{Count: count, LoansDocuments: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter loansdocument.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
|
fromClause = ` FROM loan_required_documents d`
|
|
|
|
var conditions []string
|
|
|
|
if filter.DocumentName != "" {
|
|
args = append(args, likePattern(filter.DocumentName))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
d.title->>'en' ILIKE $%[1]d OR d.title->>'ru' ILIKE $%[1]d OR d.title->>'tm' ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if filter.Status != nil {
|
|
args = append(args, *filter.Status)
|
|
conditions = append(conditions, fmt.Sprintf("d.status = $%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) (loansdocument.LoanDocumentDTO, error) {
|
|
query := fmt.Sprintf(`SELECT %s FROM loan_required_documents d WHERE d.id = $1`, documentColumns)
|
|
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto loansdocument.CreateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
|
|
title, err := json.Marshal(dto.Title)
|
|
if err != nil {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
|
|
status := true
|
|
if dto.Status != nil {
|
|
status = *dto.Status
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, status)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
RETURNING %s`, documentColumns)
|
|
|
|
return scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, status))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.UpdateLoanDocumentDTO) (loansdocument.LoanDocumentDTO, error) {
|
|
title, err := json.Marshal(dto.Title)
|
|
if err != nil {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
UPDATE loan_required_documents SET
|
|
title = $1, content_en = $2, content_ru = $3, content_tm = $4, status = $5,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = $6
|
|
RETURNING %s`, documentColumns)
|
|
|
|
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, dto.Status, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) UpdateStatus(ctx context.Context, id int64, status bool) (loansdocument.LoanDocumentDTO, error) {
|
|
query := fmt.Sprintf(`
|
|
UPDATE loan_required_documents SET
|
|
status = $1,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = $2
|
|
RETURNING %s`, documentColumns)
|
|
|
|
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, status, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM loan_required_documents 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 scanLoanDocument(row scannable) (loansdocument.LoanDocumentDTO, error) {
|
|
var item loansdocument.LoanDocumentDTO
|
|
var titleJSON []byte
|
|
|
|
if err := row.Scan(
|
|
&item.Id, &titleJSON, &item.ContentEn, &item.ContentRu, &item.ContentTm,
|
|
&item.Status, &item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
|
return loansdocument.LoanDocumentDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|