140 lines
4.2 KiB
Go
140 lines
4.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/identity/permission"
|
|
"online-back/internal/shared"
|
|
"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[permission.PermissionDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) permission.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "permissions p", permissionColumns, scanPermission),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
const permissionColumns = `p.id, p.code, p.name, p.guard_name, p.module, p.created_at, p.updated_at`
|
|
|
|
const permissionReturningColumns = `id, code, name, guard_name, module, created_at, updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter permission.ListFilter) (permission.ListPermissionDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
|
|
if err != nil {
|
|
return permission.ListPermissionDTO{}, err
|
|
}
|
|
|
|
return permission.ListPermissionDTO{Count: count, Permissions: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter permission.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
if filter.PermissionName != "" {
|
|
args = append(args, shared.LikePattern(filter.PermissionName))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`p.name ILIKE $%d`, n))
|
|
}
|
|
if filter.GuardName != "" {
|
|
args = append(args, filter.GuardName)
|
|
conditions = append(conditions, fmt.Sprintf(`p.guard_name = $%d`, len(args)))
|
|
}
|
|
if filter.Module != "" {
|
|
args = append(args, filter.Module)
|
|
conditions = append(conditions, fmt.Sprintf(`p.module = $%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) (permission.PermissionDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Tree(ctx context.Context, guardName string) (permission.PermissionTreeDTO, error) {
|
|
query := `SELECT ` + permissionColumns + ` FROM permissions p`
|
|
var args []interface{}
|
|
if guardName != "" {
|
|
query += ` WHERE p.guard_name = $1`
|
|
args = append(args, guardName)
|
|
}
|
|
query += ` ORDER BY p.guard_name, p.module, p.id`
|
|
|
|
rows, err := r.client.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return permission.PermissionTreeDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []permission.PermissionDTO
|
|
for rows.Next() {
|
|
item, err := scanPermission(rows)
|
|
if err != nil {
|
|
return permission.PermissionTreeDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return permission.PermissionTreeDTO{}, err
|
|
}
|
|
return permission.BuildTree(items), nil
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto permission.CreatePermissionDTO) (permission.PermissionDTO, error) {
|
|
query := fmt.Sprintf(`
|
|
INSERT INTO permissions (code, name, guard_name, module)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING %s`, permissionReturningColumns)
|
|
|
|
return scanPermission(r.client.QueryRow(ctx, query, dto.Code, dto.Name, dto.GuardName, dto.Module))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto permission.UpdatePermissionDTO) (permission.PermissionDTO, error) {
|
|
query := fmt.Sprintf(`
|
|
UPDATE permissions SET
|
|
code = $1, name = $2, guard_name = $3, module = $4,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = $5 AND deleted_at IS NULL
|
|
RETURNING %s`, permissionReturningColumns)
|
|
|
|
item, err := scanPermission(r.client.QueryRow(ctx, query, dto.Code, dto.Name, dto.GuardName, dto.Module, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return permission.PermissionDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return r.generic.Delete(ctx, id)
|
|
}
|
|
|
|
func scanPermission(row genericrepo.Scanner) (permission.PermissionDTO, error) {
|
|
var item permission.PermissionDTO
|
|
if err := row.Scan(
|
|
&item.Id, &item.Code, &item.Name, &item.GuardName, &item.Module, &item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return permission.PermissionDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|