Skip to content

Commit

Permalink
Create user delete route
Browse files Browse the repository at this point in the history
  • Loading branch information
kauefraga committed Nov 9, 2024
1 parent 77942db commit d5f85bb
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion apps/backend/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const AuthUserSchema = z.object({
password: z.string(),
});

const DeleteUserSchema = z.object({
id: z.string().uuid(),
});

export const UserController = defineController(http => {
http.post('/v1/user/create', async (request, reply) => {
const { name, email, password } = CreateUserSchema.parse(request.body);
Expand Down Expand Up @@ -61,7 +65,7 @@ export const UserController = defineController(http => {
.limit(1);

if (!existingUser) {
return reply.status(400).send({
return reply.status(409).send({
message: 'The user does not exist.',
});
}
Expand All @@ -80,4 +84,26 @@ export const UserController = defineController(http => {
token,
});
});

http.delete('/v1/user/delete', async (request, reply) => {
const { id } = DeleteUserSchema.parse(request.body);

const [existingUser] = await db.select().from(user).where(eq(user.id, id)).limit(1);

if (!existingUser) {
return reply.status(409).send({
message: 'The user does not exist.',
});
}

const { rowCount } = await db.delete(user).where(eq(user.id, id));

if (rowCount === 0) {
return reply.send(500).send({
message: 'Failed to delete user.',
});
}

return reply.status(204).send();
});
});

0 comments on commit d5f85bb

Please sign in to comment.