109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/identity/role"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
"online-back/pkg/repository/genericrepo"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
generic *genericrepo.Repository[role.RoleDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) role.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "roles r", roleColumns, scanRole),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
const roleColumns = `r.id, r.code, r.name, r.guard_name, r.created_at, r.updated_at`
|
|
|
|
const roleReturningColumns = `id, code, name, guard_name, created_at, updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter role.ListFilter) (role.ListRoleDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
|
|
if err != nil {
|
|
return role.ListRoleDTO{}, err
|
|
}
|
|
|
|
return role.ListRoleDTO{Count: count, Roles: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter role.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
if filter.RoleName != "" {
|
|
args = append(args, likePattern(filter.RoleName))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`r.name ILIKE $%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) GetByID(ctx context.Context, id int64) (role.RoleDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto role.CreateRoleDTO) (role.RoleDTO, error) {
|
|
query := fmt.Sprintf(`
|
|
INSERT INTO roles (code, name, guard_name)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING %s`, roleReturningColumns)
|
|
|
|
return scanRole(r.client.QueryRow(ctx, query, dto.Code, dto.Name, dto.GuardName))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto role.UpdateRoleDTO) (role.RoleDTO, error) {
|
|
query := fmt.Sprintf(`
|
|
UPDATE roles SET
|
|
code = $1, name = $2, guard_name = $3,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = $4
|
|
RETURNING %s`, roleReturningColumns)
|
|
|
|
item, err := scanRole(r.client.QueryRow(ctx, query, dto.Code, dto.Name, dto.GuardName, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return role.RoleDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return r.generic.Delete(ctx, id)
|
|
}
|
|
|
|
func scanRole(row genericrepo.Scanner) (role.RoleDTO, error) {
|
|
var item role.RoleDTO
|
|
if err := row.Scan(
|
|
&item.Id, &item.Code, &item.Name, &item.GuardName, &item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return role.RoleDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|