-
Notifications
You must be signed in to change notification settings - Fork 0
feat: user management api #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e655b83
feat: user management api
lolakk05 28a1d81
refactor: user contoller to custom one, transactions"
lolakk05 e795074
fix: lint
lolakk05 438603b
fix: fix: lint
lolakk05 29d8362
refactor: changes
lolakk05 684eeaa
fix: delete endpoint wrap with transactions and arrow function error …
lolakk05 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
| import vine from "@vinejs/vine"; | ||
|
|
||
| import type { HttpContext } from "@adonisjs/core/http"; | ||
| import router from "@adonisjs/core/services/router"; | ||
| import type { Constructor, LazyImport } from "@adonisjs/core/types/http"; | ||
| import db from "@adonisjs/lucid/services/db"; | ||
|
|
||
| import User from "#app/models/user"; | ||
|
|
||
| import BaseController from "../base_controller.js"; | ||
|
|
||
| //should check if email is unique and more conditions on password | ||
| const createUserValidator = vine.compile( | ||
| vine.object({ | ||
| fullName: vine.string().optional(), | ||
| email: vine.string().email(), | ||
| password: vine.string().minLength(8), | ||
| }), | ||
| ); | ||
|
|
||
| const updateUserValidator = vine.compile( | ||
| vine.object({ | ||
| fullName: vine.string().optional(), | ||
| email: vine.string().email().optional(), | ||
| password: vine.string().minLength(8).optional(), | ||
| }), | ||
| ); | ||
|
|
||
| const paginationValidator = vine.compile( | ||
| vine.object({ | ||
| page: vine.number().min(1).optional(), | ||
| limit: vine.number().min(1).max(100).optional(), | ||
| }), | ||
| ); | ||
|
|
||
| const userIdParamValidator = vine.compile( | ||
| vine.object({ | ||
| id: vine.number(), | ||
| }), | ||
| ); | ||
|
|
||
| export default class UsersController extends BaseController { | ||
| $configureRoutes(controller: LazyImport<Constructor<UsersController>>) { | ||
| router.get("/", [controller, "findAll"]).as("users.list"); | ||
| router.get("/:id", [controller, "findOne"]).as("users.show"); | ||
| router.delete("/:id", [controller, "delete"]).as("users.delete"); | ||
| router.patch("/:id", [controller, "update"]).as("users.update"); | ||
| router.post("/", [controller, "create"]).as("users.create"); | ||
| } | ||
|
|
||
| async findAll({ request, auth }: HttpContext) { | ||
| await this.requireSuperUser(auth); | ||
|
|
||
| const { page = 1, limit = 10 } = | ||
| await request.validateUsing(paginationValidator); | ||
|
|
||
| const users = await User.query() | ||
| .select("id", "fullName", "email") | ||
| .paginate(page, limit); | ||
|
|
||
| return { data: users }; | ||
| } | ||
|
|
||
| async findOne({ request, auth }: HttpContext) { | ||
| const user = await request.validateUsing(userIdParamValidator); | ||
|
|
||
| await this.requireSuperUser(auth); | ||
|
|
||
| const targetUser = await User.query() | ||
| .select("id", "fullName", "email") | ||
| .where("id", user.id) | ||
| .firstOrFail() | ||
| .addErrorContext(() => `User with id ${user.id} not found`); | ||
|
|
||
| return { data: targetUser }; | ||
| } | ||
|
|
||
| async delete({ request, auth }: HttpContext) { | ||
| const user = await request.validateUsing(userIdParamValidator); | ||
|
|
||
| await this.requireSuperUser(auth); | ||
| await db.transaction(async (trx) => { | ||
| const targetUser = await User.findOrFail(user.id, { | ||
| client: trx, | ||
| }).addErrorContext(() => `User with id ${user.id} not found`); | ||
|
|
||
| await targetUser.delete(); | ||
| }); | ||
|
|
||
| return { success: true }; | ||
| } | ||
|
|
||
| async update({ request, auth }: HttpContext) { | ||
| const user = await request.validateUsing(userIdParamValidator); | ||
|
|
||
| await this.requireSuperUser(auth); | ||
|
|
||
| const payload = await request.validateUsing(updateUserValidator); | ||
|
|
||
| const updatedUser = await db.transaction(async (trx) => { | ||
| const targetUser = await User.query({ client: trx }) | ||
| .where("id", user.id) | ||
| .firstOrFail() | ||
| .addErrorContext(() => `User with id ${user.id} not found`); | ||
|
|
||
| targetUser.merge(payload); | ||
| await targetUser.save(); | ||
|
|
||
| return targetUser; | ||
| }); | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| id: updatedUser.id, | ||
| fullName: updatedUser.fullName, | ||
| email: updatedUser.email, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| async create({ request, auth }: HttpContext) { | ||
| await this.requireSuperUser(auth); | ||
|
|
||
| const payload = await request.validateUsing(createUserValidator); | ||
|
|
||
| const newUser = await db.transaction(async (trx) => { | ||
| const user = await User.create(payload, { client: trx }); | ||
| return user; | ||
| }); | ||
|
|
||
| return { | ||
| success: true, | ||
| data: { | ||
| id: newUser.id, | ||
| fullName: newUser.fullName, | ||
| email: newUser.email, | ||
| }, | ||
| }; | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.