102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"online-back/internal/identity/permissionrole"
|
|
"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
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) permissionrole.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
// SetPermissions replaces dto.RoleId's whole permission set with
|
|
// dto.PermissionIds inside one transaction: pairs not in the list are
|
|
// soft-deleted, pairs in the list are upserted. Both statements run on the
|
|
// same tx so a role never ends up with permissions only half-updated.
|
|
func (r *repository) SetPermissions(ctx context.Context, dto permissionrole.SetRolePermissionsDTO) ([]permissionrole.PermissionRoleDTO, error) {
|
|
tx, err := r.client.Begin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
_, err = tx.Exec(ctx,
|
|
`UPDATE permission_role SET deleted_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE role_id = $1 AND deleted_at IS NULL AND NOT (permission_id = ANY($2::bigint[]))`,
|
|
dto.RoleId, dto.PermissionIds,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var items []permissionrole.PermissionRoleDTO
|
|
if len(dto.PermissionIds) > 0 {
|
|
query := `
|
|
WITH upserted AS (
|
|
INSERT INTO permission_role (permission_id, role_id)
|
|
SELECT unnest($1::bigint[]), $2
|
|
ON CONFLICT (permission_id, role_id) WHERE deleted_at IS NULL DO UPDATE SET
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
RETURNING id, permission_id, role_id, created_at, updated_at
|
|
)
|
|
SELECT upserted.id, upserted.permission_id, p.name, upserted.role_id, r.name, upserted.created_at, upserted.updated_at
|
|
FROM upserted
|
|
JOIN permissions p ON p.id = upserted.permission_id
|
|
JOIN roles r ON r.id = upserted.role_id`
|
|
|
|
rows, err := tx.Query(ctx, query, dto.PermissionIds, dto.RoleId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for rows.Next() {
|
|
item, err := scanPermissionRole(rows)
|
|
if err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
rowsErr := rows.Err()
|
|
rows.Close()
|
|
if rowsErr != nil {
|
|
return nil, rowsErr
|
|
}
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return genericrepo.Delete(ctx, r.client, "permission_role", id)
|
|
}
|
|
|
|
func scanPermissionRole(row genericrepo.Scanner) (permissionrole.PermissionRoleDTO, error) {
|
|
var item permissionrole.PermissionRoleDTO
|
|
|
|
err := row.Scan(
|
|
&item.Id, &item.PermissionId, &item.PermissionName, &item.RoleId, &item.RoleName, &item.CreatedAt, &item.UpdatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return permissionrole.PermissionRoleDTO{}, err
|
|
}
|
|
if err != nil {
|
|
return permissionrole.PermissionRoleDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|