rm -rf permission module
This commit is contained in:
1 parent
c6b9cb7462
commit
848a07324f
10 files changed
+193
-119
No files matched your search
@@ -21,22 +21,64 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) permissionr
|
||||
return &repository{client: client, logger: logger}
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto permissionrole.CreatePermissionRoleDTO) (permissionrole.PermissionRoleDTO, error) {
|
||||
query := `
|
||||
WITH upserted AS (
|
||||
INSERT INTO permission_role (permission_id, role_id)
|
||||
VALUES ($1, $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`
|
||||
// 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)
|
||||
|
||||
row := r.client.QueryRow(ctx, query, dto.PermissionId, dto.RoleId)
|
||||
return scanPermissionRole(row)
|
||||
_, 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 {
|
||||
|
||||
Reference in new issue
Block a user