-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement roles services and controllers
- Loading branch information
1 parent
d98ae34
commit 1e04f1e
Showing
13 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
import { inject } from '@adonisjs/core' | ||
import { HttpContext } from '@adonisjs/core/http' | ||
import app from '@adonisjs/core/services/app' | ||
|
||
import ListRolesService from '#modules/role/services/list_roles/list_roles_service' | ||
import SyncRolesService from '#modules/role/services/attach_roles/sync_roles_service' | ||
|
||
import { attachRoleValidator } from '#modules/role/validation/roles_validator' | ||
|
||
@inject() | ||
export default class RolesController {} | ||
export default class RolesController { | ||
async list({ response }: HttpContext) { | ||
const service = await app.container.make(ListRolesService) | ||
|
||
const roles = await service.run() | ||
return response.json(roles) | ||
} | ||
|
||
async attach({ request, response, i18n }: HttpContext) { | ||
const data = request.body() | ||
const { user_id: userId, role_ids: roleIds } = await attachRoleValidator.validate(data) | ||
|
||
const syncRolesService = await app.container.make(SyncRolesService) | ||
await syncRolesService.run({ userId, roleIds }) | ||
|
||
return response.json({ | ||
message: i18n.t('messages.successfully_attached', { | ||
resource: i18n.t('models.role'), | ||
}), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import router from '@adonisjs/core/services/router' | ||
import { middleware } from '#start/kernel' | ||
import IRole from '#modules/role/interfaces/role_interface' | ||
|
||
const RolesController = () => import('#modules/role/controllers/roles_controller') | ||
|
||
router | ||
.group(() => { | ||
router.get('/', [RolesController, 'list']).as('role.list') | ||
router.put('/attach', [RolesController, 'attach']).as('role.attach') | ||
}) | ||
.use([ | ||
middleware.auth(), | ||
middleware.acl({ | ||
role_slugs: [IRole.Slugs.ROOT, IRole.Slugs.ADMIN], | ||
}), | ||
]) | ||
.prefix('admin/roles') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '#modules/role/routes/admin_routes' |
20 changes: 20 additions & 0 deletions
20
app/modules/role/services/attach_roles/sync_roles_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { inject } from '@adonisjs/core' | ||
import app from '@adonisjs/core/services/app' | ||
import GetUserService from '#modules/user/services/read_user/get_user_service' | ||
|
||
type SyncRolesRequest = { | ||
userId: number | ||
roleIds: number[] | ||
} | ||
|
||
@inject() | ||
export default class SyncRolesService { | ||
constructor() {} | ||
|
||
async run({ userId, roleIds }: SyncRolesRequest) { | ||
const getUserService = await app.container.make(GetUserService) | ||
const user = await getUserService.run(userId) | ||
|
||
await user.related('roles').sync(roleIds) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import vine from '@vinejs/vine' | ||
|
||
export const attachRoleValidator = vine.compile( | ||
vine.object({ | ||
user_id: vine.number(), | ||
role_ids: vine.array( | ||
vine.number().exists(async (db, value) => { | ||
const roles = await db.from('roles').where('id', +value) | ||
return roles.length === value.length | ||
}) | ||
), | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
successfully_deleted: '{resource} successfully deleted.' | ||
successfully_updated: '{resource} successfully updated.' | ||
successfully_created: '{resource} successfully created.' | ||
successfully_attached: '{resource} successfully attached.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
user: 'User' | ||
role: 'Role' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
successfully_deleted: '{resource} deletado com sucesso.' | ||
successfully_updated: '{resource} atualizado com sucesso.' | ||
successfully_created: '{resource} criado com sucesso.' | ||
successfully_attached: '{resource} anexado com sucesso.' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
user: 'Usuário' | ||
role: 'Função' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters