Align loan type and required document schema with updated column names.
Rename loan_required_documents to loan_order_required_docs, simplify document fields to name/value, and map loan_types columns to name, maturity, notes, and active. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -23,9 +22,9 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) loansdocume
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const documentColumns = `d.id, d.title, d.content_en, d.content_ru, d.content_tm, d.active, d.created_at, d.updated_at`
|
||||
const documentColumns = `d.id, d.name, d.value, d.active, d.created_at, d.updated_at`
|
||||
|
||||
const documentReturningColumns = `id, title, content_en, content_ru, content_tm, active, created_at, 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) {
|
||||
fromClause, whereClause, args := buildListQuery(filter)
|
||||
@@ -65,16 +64,14 @@ func (r *repository) List(ctx context.Context, filter loansdocument.ListFilter)
|
||||
}
|
||||
|
||||
func buildListQuery(filter loansdocument.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
||||
fromClause = ` FROM loan_required_documents d`
|
||||
fromClause = ` FROM loan_order_required_docs 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))
|
||||
conditions = append(conditions, fmt.Sprintf(`d.name ILIKE $%d`, n))
|
||||
}
|
||||
|
||||
if filter.Active != nil {
|
||||
@@ -97,7 +94,7 @@ func likePattern(term string) string {
|
||||
}
|
||||
|
||||
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)
|
||||
query := fmt.Sprintf(`SELECT %s FROM loan_order_required_docs 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
|
||||
@@ -106,38 +103,28 @@ func (r *repository) GetByID(ctx context.Context, id int64) (loansdocument.LoanD
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
active := true
|
||||
if dto.Active != nil {
|
||||
active = *dto.Active
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
INSERT INTO loan_required_documents (title, content_en, content_ru, content_tm, active)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
INSERT INTO loan_order_required_docs (name, value, active)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING %s`, documentReturningColumns)
|
||||
|
||||
return scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, active))
|
||||
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) {
|
||||
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, active = $5,
|
||||
UPDATE loan_order_required_docs SET
|
||||
name = $1, value = $2, active = $3,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $6
|
||||
WHERE id = $4
|
||||
RETURNING %s`, documentReturningColumns)
|
||||
|
||||
item, err := scanLoanDocument(r.client.QueryRow(ctx, query, title, dto.ContentEn, dto.ContentRu, dto.ContentTm, dto.Active, id))
|
||||
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
|
||||
}
|
||||
@@ -146,7 +133,7 @@ func (r *repository) Update(ctx context.Context, id int64, dto loansdocument.Upd
|
||||
|
||||
func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (loansdocument.LoanDocumentDTO, error) {
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE loan_required_documents SET
|
||||
UPDATE loan_order_required_docs SET
|
||||
active = $1,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = $2
|
||||
@@ -160,7 +147,7 @@ func (r *repository) UpdateActive(ctx context.Context, id int64, active bool) (l
|
||||
}
|
||||
|
||||
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)
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM loan_order_required_docs WHERE id = $1`, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -176,16 +163,11 @@ type scannable interface {
|
||||
|
||||
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.Active, &item.CreatedAt, &item.UpdatedAt,
|
||||
&item.Id, &item.Name, &item.Value, &item.Active, &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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user