add payment text
This commit is contained in:
114
internal/payment/paymenttexts/db/postgresql.go
Normal file
114
internal/payment/paymenttexts/db/postgresql.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/payment/paymenttexts"
|
||||
"online-back/internal/shared"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) paymenttexts.Repository {
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
const textColumnsSQL = `t.id, t.code, t.name, t.text, t.is_active, t.updated_by, ` + shared.UserDisplaySQL + `, t.updated_at`
|
||||
|
||||
func (r *repository) List(ctx context.Context, filter paymenttexts.ListFilter) (paymenttexts.ListPaymentTextDTO, error) {
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
if filter.ActiveOnly {
|
||||
conditions = append(conditions, "t.is_active")
|
||||
}
|
||||
if filter.Search != "" {
|
||||
args = append(args, shared.LikePattern(filter.Search))
|
||||
n := strconv.Itoa(len(args))
|
||||
conditions = append(conditions, "(t.code ILIKE $"+n+" OR t.name ILIKE $"+n+" OR t.text::text ILIKE $"+n+")")
|
||||
}
|
||||
where := ""
|
||||
if len(conditions) > 0 {
|
||||
where = " WHERE " + strings.Join(conditions, " AND ")
|
||||
}
|
||||
|
||||
rows, err := r.client.Query(ctx,
|
||||
`SELECT `+textColumnsSQL+` FROM payment_texts t LEFT JOIN users u ON u.id = t.updated_by`+where+` ORDER BY t.id ASC`,
|
||||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return paymenttexts.ListPaymentTextDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items := []paymenttexts.PaymentTextDTO{}
|
||||
for rows.Next() {
|
||||
item, err := scanText(rows)
|
||||
if err != nil {
|
||||
return paymenttexts.ListPaymentTextDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return paymenttexts.ListPaymentTextDTO{}, err
|
||||
}
|
||||
|
||||
return paymenttexts.ListPaymentTextDTO{Count: len(items), Items: items}, nil
|
||||
}
|
||||
|
||||
func (r *repository) GetByCode(ctx context.Context, code string) (paymenttexts.PaymentTextDTO, error) {
|
||||
return scanText(r.client.QueryRow(ctx,
|
||||
`SELECT `+textColumnsSQL+` FROM payment_texts t LEFT JOIN users u ON u.id = t.updated_by WHERE t.code = $1`,
|
||||
code,
|
||||
))
|
||||
}
|
||||
|
||||
func (r *repository) Update(ctx context.Context, code string, dto paymenttexts.UpdatePaymentTextDTO) (paymenttexts.PaymentTextDTO, error) {
|
||||
text, err := json.Marshal(dto.Text)
|
||||
if err != nil {
|
||||
return paymenttexts.PaymentTextDTO{}, err
|
||||
}
|
||||
|
||||
query := `
|
||||
WITH t AS (
|
||||
UPDATE payment_texts SET
|
||||
name = $2, text = $3, is_active = $4, updated_by = $5,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE code = $1
|
||||
RETURNING *
|
||||
)
|
||||
SELECT ` + textColumnsSQL + ` FROM t LEFT JOIN users u ON u.id = t.updated_by`
|
||||
|
||||
return scanText(r.client.QueryRow(ctx, query, code, dto.Name, text, *dto.IsActive, dto.UpdatedBy))
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, code string) (bool, error) {
|
||||
tag, err := r.client.Exec(ctx, `DELETE FROM payment_texts WHERE code = $1`, code)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
func scanText(row interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}) (paymenttexts.PaymentTextDTO, error) {
|
||||
var item paymenttexts.PaymentTextDTO
|
||||
var text []byte
|
||||
if err := row.Scan(
|
||||
&item.Id, &item.Code, &item.Name, &text, &item.IsActive, &item.UpdatedById, &item.UpdatedByName, &item.UpdatedAt,
|
||||
); err != nil {
|
||||
return paymenttexts.PaymentTextDTO{}, err
|
||||
}
|
||||
if err := shared.UnmarshalLocalized(text, &item.Text); err != nil {
|
||||
return paymenttexts.PaymentTextDTO{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
Reference in New Issue
Block a user