139 lines
3.7 KiB
Go
139 lines
3.7 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/cards/cards"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
"online-back/pkg/repository/genericrepo"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
generic *genericrepo.Repository[cards.CardDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) cards.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "cards", cardColumnsSQL, scanCard),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// cardWritableColumns lists every cards column except id, created_at and
|
|
// updated_at, in the exact order CreateCardDTO/UpdateCardDTO fields are read.
|
|
var cardWritableColumns = []string{
|
|
"user_id", "number", "name", "month", "year", "type",
|
|
}
|
|
|
|
var cardColumns = append(append([]string{"id"}, cardWritableColumns...), "created_at", "updated_at")
|
|
|
|
var cardColumnsSQL = strings.Join(cardColumns, ", ")
|
|
|
|
var cardWritableColumnsSQL = strings.Join(cardWritableColumns, ", ")
|
|
|
|
func createArgs(dto cards.CreateCardDTO) []interface{} {
|
|
return []interface{}{
|
|
dto.UserId, dto.Number, dto.Name, dto.Month, dto.Year, dto.Type,
|
|
}
|
|
}
|
|
|
|
func updateArgs(dto cards.UpdateCardDTO) []interface{} {
|
|
return []interface{}{
|
|
dto.UserId, dto.Number, dto.Name, dto.Month, dto.Year, dto.Type,
|
|
}
|
|
}
|
|
|
|
func (r *repository) List(ctx context.Context, filter cards.ListFilter) (cards.ListCardDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
|
|
if err != nil {
|
|
return cards.ListCardDTO{}, err
|
|
}
|
|
|
|
return cards.ListCardDTO{Count: count, Cards: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter cards.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
|
|
if filter.UserId != nil {
|
|
args = append(args, *filter.UserId)
|
|
conditions = append(conditions, fmt.Sprintf("user_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.Type != nil {
|
|
args = append(args, *filter.Type)
|
|
conditions = append(conditions, fmt.Sprintf("type = $%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) (cards.CardDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto cards.CreateCardDTO) (cards.CardDTO, error) {
|
|
args := createArgs(dto)
|
|
|
|
placeholders := make([]string, len(args))
|
|
for i := range args {
|
|
placeholders[i] = fmt.Sprintf("$%d", i+1)
|
|
}
|
|
|
|
query := fmt.Sprintf(
|
|
`INSERT INTO cards (%s) VALUES (%s) RETURNING %s`,
|
|
cardWritableColumnsSQL, strings.Join(placeholders, ", "), cardColumnsSQL,
|
|
)
|
|
|
|
return scanCard(r.client.QueryRow(ctx, query, args...))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto cards.UpdateCardDTO) (cards.CardDTO, error) {
|
|
args := updateArgs(dto)
|
|
|
|
assignments := make([]string, len(cardWritableColumns))
|
|
for i, column := range cardWritableColumns {
|
|
assignments[i] = fmt.Sprintf("%s = $%d", column, i+1)
|
|
}
|
|
args = append(args, id)
|
|
|
|
query := fmt.Sprintf(
|
|
`UPDATE cards SET %s, updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC') WHERE id = $%d RETURNING %s`,
|
|
strings.Join(assignments, ", "), len(args), cardColumnsSQL,
|
|
)
|
|
|
|
item, err := scanCard(r.client.QueryRow(ctx, query, args...))
|
|
if err != nil {
|
|
return cards.CardDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return r.generic.Delete(ctx, id)
|
|
}
|
|
|
|
func scanCard(row genericrepo.Scanner) (cards.CardDTO, error) {
|
|
var item cards.CardDTO
|
|
err := row.Scan(
|
|
&item.Id, &item.UserId, &item.Number, &item.Name, &item.Month, &item.Year, &item.Type,
|
|
&item.CreatedAt, &item.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return cards.CardDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|