141 lines
3.8 KiB
Go
141 lines
3.8 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/identity/userbranch"
|
|
"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) userbranch.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
const userBranchFromClause = ` FROM branch_user bu
|
|
JOIN branches b ON b.id = bu.branch_id
|
|
JOIN users u ON u.id = bu.user_id`
|
|
|
|
const userBranchSelectColumns = `bu.id, bu.branch_id, b.name, bu.user_id, u.name, bu.created_at, bu.updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter userbranch.ListFilter) (userbranch.ListUserBranchDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
var count int64
|
|
if err := r.client.QueryRow(ctx, `SELECT COUNT(*)`+userBranchFromClause+whereClause, args...).Scan(&count); err != nil {
|
|
return userbranch.ListUserBranchDTO{}, err
|
|
}
|
|
|
|
query := `SELECT ` + userBranchSelectColumns + userBranchFromClause + whereClause + ` ORDER BY bu.id DESC`
|
|
rows, err := r.client.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return userbranch.ListUserBranchDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []userbranch.UserBranchDTO
|
|
for rows.Next() {
|
|
item, err := scanUserBranch(rows)
|
|
if err != nil {
|
|
return userbranch.ListUserBranchDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return userbranch.ListUserBranchDTO{}, err
|
|
}
|
|
|
|
return userbranch.ListUserBranchDTO{Count: count, Items: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter userbranch.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
|
|
if filter.UserId != nil {
|
|
args = append(args, *filter.UserId)
|
|
conditions = append(conditions, fmt.Sprintf("bu.user_id = $%d", len(args)))
|
|
}
|
|
|
|
if filter.BranchName != "" {
|
|
args = append(args, likePattern(filter.BranchName))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
b.name->>'en' ILIKE $%[1]d OR b.name->>'ru' ILIKE $%[1]d OR b.name->>'tm' ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if len(conditions) == 0 {
|
|
return "", args
|
|
}
|
|
|
|
return " 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) Create(ctx context.Context, dto userbranch.CreateUserBranchDTO) (userbranch.UserBranchDTO, error) {
|
|
query := `
|
|
WITH inserted AS (
|
|
INSERT INTO branch_user (branch_id, user_id)
|
|
VALUES ($1, $2)
|
|
RETURNING id, branch_id, user_id, created_at, updated_at
|
|
)
|
|
SELECT inserted.id, inserted.branch_id, b.name, inserted.user_id, u.name, inserted.created_at, inserted.updated_at
|
|
FROM inserted
|
|
JOIN branches b ON b.id = inserted.branch_id
|
|
JOIN users u ON u.id = inserted.user_id`
|
|
|
|
row := r.client.QueryRow(ctx, query, dto.BranchId, dto.UserId)
|
|
return scanUserBranch(row)
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM branch_user 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 scanUserBranch(row scannable) (userbranch.UserBranchDTO, error) {
|
|
var item userbranch.UserBranchDTO
|
|
var branchNameJSON []byte
|
|
|
|
err := row.Scan(
|
|
&item.Id, &item.BranchId, &branchNameJSON, &item.UserId, &item.UserName, &item.CreatedAt, &item.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
if err != nil {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(branchNameJSON, &item.BranchName); err != nil {
|
|
return userbranch.UserBranchDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|