feat: Контроллер и сервис пользователей
This commit is contained in:
83
apps/backend/src/controllers/users.controller.ts
Normal file
83
apps/backend/src/controllers/users.controller.ts
Normal file
@ -0,0 +1,83 @@
|
||||
import { Elysia, t } from 'elysia';
|
||||
import { usersService } from '@/services/users.service';
|
||||
|
||||
export const usersController = new Elysia({ prefix: '/users' })
|
||||
.get(
|
||||
'/by-name',
|
||||
async ({ query, set }) => {
|
||||
try {
|
||||
const name = query.name?.trim();
|
||||
|
||||
const foundUser = await usersService.getByName(name);
|
||||
|
||||
if (!foundUser) {
|
||||
set.status = 404;
|
||||
return { error: 'User not found' };
|
||||
}
|
||||
|
||||
return {
|
||||
id: foundUser.id,
|
||||
name: foundUser.name,
|
||||
email: foundUser.email,
|
||||
image: foundUser.image,
|
||||
emailVerified: foundUser.emailVerified,
|
||||
createdAt: foundUser.createdAt.toISOString(),
|
||||
updatedAt: foundUser.updatedAt.toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return {
|
||||
error: error instanceof Error ? error.message : 'Failed to get user',
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
query: t.Object({
|
||||
name: t.String({ minLength: 1 }),
|
||||
}),
|
||||
detail: {
|
||||
tags: ['Users'],
|
||||
summary: 'Get user by name',
|
||||
description: 'Returns user information by name',
|
||||
},
|
||||
}
|
||||
)
|
||||
.get(
|
||||
'/:id',
|
||||
async ({ params: { id }, set }) => {
|
||||
try {
|
||||
const foundUser = await usersService.getById(id);
|
||||
|
||||
if (!foundUser) {
|
||||
set.status = 404;
|
||||
return { error: 'User not found' };
|
||||
}
|
||||
|
||||
return {
|
||||
id: foundUser.id,
|
||||
name: foundUser.name,
|
||||
email: foundUser.email,
|
||||
image: foundUser.image,
|
||||
emailVerified: foundUser.emailVerified,
|
||||
createdAt: foundUser.createdAt.toISOString(),
|
||||
updatedAt: foundUser.updatedAt.toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
set.status = 500;
|
||||
return {
|
||||
error: error instanceof Error ? error.message : 'Failed to get user',
|
||||
};
|
||||
}
|
||||
},
|
||||
{
|
||||
params: t.Object({
|
||||
id: t.String(),
|
||||
}),
|
||||
detail: {
|
||||
tags: ['Users'],
|
||||
summary: 'Get user by ID',
|
||||
description: 'Returns user information by ID',
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user