Added structured logging across services and repositories. Updated SQL queries to use parameterized placeholders for better readability and security. Enhanced error handling for external service communication.

This commit is contained in:
2026-05-15 22:07:03 +03:00
parent c3f90b57c2
commit 8462b16305
11 changed files with 296 additions and 63 deletions
+91 -32
View File
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"log"
"FamilyHub/src/domain"
)
@@ -25,29 +26,63 @@ func NewReceiptsSQLRepository(db *sql.DB) *ReceiptsSQLRepository {
}
func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Receipt) (int64, error) {
log.Printf("%+v\n", receipt)
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
if receipt.ReceiptNumber != receipt.UI {
receipt.ReceiptNumber = receipt.UI
}
res, err := tx.ExecContext(ctx, `
log.Println("First query")
query := `
INSERT INTO receipts (
transaction_id, receipt_number, ui, status, issued_at,
total_amount, payment_amount, cash_amount,
another_amount, clearing_amount, margin,
currency, payment_type,
cashbox_number, cashier,
name_spd, name_to, name_np, type_np,
street_to, house_to,
kod_soato, oblast_soato, rayon_soato, selsovet_soato,
doc_num, skno_number, unp,
transaction_id,
receipt_number,
ui,
status,
issued_at,
total_amount,
payment_amount,
cash_amount,
another_amount,
clearing_amount,
margin,
currency,
payment_type,
cashbox_number,
cashier,
name_spd,
name_to,
name_np,
type_np,
street_to,
house_to,
kod_soato,
oblast_soato,
rayon_soato,
selsovet_soato,
doc_num,
skno_number,
unp,
success
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9, $10,
$11, $12, $13, $14, $15,
$16, $17, $18, $19, $20,
$21, $22, $23, $24, $25,
$26, $27, $28, $29
)
RETURNING id;
`
args := []any{
receipt.TransactionID,
receipt.ReceiptNumber,
receipt.UI,
@@ -85,16 +120,19 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
receipt.UNP,
receipt.Success,
)
}
log.Printf("SQL: %s", query)
log.Printf("ARGS: %+v", args)
var receiptID int64
err = tx.QueryRowContext(ctx, query, args...).Scan(&receiptID)
if err != nil {
return 0, err
}
receiptID, err := res.LastInsertId()
if err != nil {
return 0, err
}
log.Println("Second query")
stmt, err := tx.PrepareContext(ctx, `
INSERT INTO positions (
@@ -109,7 +147,11 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
tag,
marking_code,
ukz_code
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9, $10, $11
)
`)
if err != nil {
return 0, err
@@ -117,7 +159,8 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
defer stmt.Close()
for _, p := range receipt.Positions {
_, err = stmt.ExecContext(ctx,
_, err = stmt.ExecContext(
ctx,
receiptID,
p.SectionNumber,
p.GTINCode,
@@ -135,7 +178,11 @@ func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Rece
}
}
return receiptID, tx.Commit()
if err = tx.Commit(); err != nil {
return 0, err
}
return receiptID, nil
}
func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.Receipt, error) {
@@ -157,7 +204,7 @@ func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.
doc_num, skno_number, unp,
success
FROM receipts
WHERE id = ?
WHERE id = $1
`, id).Scan(
&receipt.ID,
&receipt.TransactionID,
@@ -213,7 +260,7 @@ func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.
product_count, amount,
discount, surcharge,
tag, marking_code, ukz_code
FROM positions WHERE receipt_id = ?
FROM positions WHERE receipt_id = $1
`, id)
if err != nil {
return nil, err
@@ -247,10 +294,16 @@ func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.
func (r *ReceiptsSQLRepository) GetAll(ctx context.Context, limit, offset int) ([]*domain.Receipt, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id, transaction_id, receipt_number, issued_at, total_amount, currency
SELECT
id,
transaction_id,
receipt_number,
issued_at,
total_amount,
currency
FROM receipts
ORDER BY issued_at DESC
LIMIT ? OFFSET ?
LIMIT $1 OFFSET $2
`, limit, offset)
if err != nil {
return nil, err
@@ -261,6 +314,7 @@ func (r *ReceiptsSQLRepository) GetAll(ctx context.Context, limit, offset int) (
for rows.Next() {
var rct domain.Receipt
if err := rows.Scan(
&rct.ID,
&rct.TransactionID,
@@ -271,9 +325,14 @@ func (r *ReceiptsSQLRepository) GetAll(ctx context.Context, limit, offset int) (
); err != nil {
return nil, err
}
receipts = append(receipts, &rct)
}
if err := rows.Err(); err != nil {
return nil, err
}
return receipts, nil
}
@@ -287,11 +346,11 @@ func (r *ReceiptsSQLRepository) Update(ctx context.Context, receipt *domain.Rece
_, err = tx.ExecContext(ctx, `
UPDATE receipts SET
transaction_id = ?,
issued_at = ?,
total_amount = ?,
currency = ?
WHERE id = ?
transaction_id = $1,
issued_at = $2,
total_amount = $3,
currency = $4
WHERE id = $5
`,
receipt.TransactionID,
receipt.IssuedAt,
@@ -303,7 +362,7 @@ func (r *ReceiptsSQLRepository) Update(ctx context.Context, receipt *domain.Rece
return err
}
_, err = tx.ExecContext(ctx, `DELETE FROM positions WHERE receipt_id = ?`, receipt.ID)
_, err = tx.ExecContext(ctx, `DELETE FROM positions WHERE receipt_id = $1`, receipt.ID)
if err != nil {
return err
}
@@ -312,7 +371,7 @@ func (r *ReceiptsSQLRepository) Update(ctx context.Context, receipt *domain.Rece
_, err = tx.ExecContext(ctx, `
INSERT INTO positions (
receipt_id, product_name, product_count, amount
) VALUES (?, ?, ?, ?)
) VALUES ($1, $2, $3, $4)
`, receipt.ID, p.ProductName, p.ProductCount, p.Amount)
if err != nil {
return err
@@ -324,7 +383,7 @@ func (r *ReceiptsSQLRepository) Update(ctx context.Context, receipt *domain.Rece
func (r *ReceiptsSQLRepository) Delete(ctx context.Context, id int64) error {
_, err := r.db.ExecContext(ctx,
`DELETE FROM receipts WHERE id = ?`,
`DELETE FROM receipts WHERE id = $1`,
id,
)
return err