full project: admin panel, backend modules, docs
This commit is contained in:
@@ -2,6 +2,7 @@ import { Controller, Get, Patch, Param, Body, UseGuards, Req } from '@nestjs/com
|
||||
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { UsersService } from './users.service';
|
||||
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
|
||||
import { UpdateUserDto, FcmTokenDto } from '../auth/dto/auth.dto';
|
||||
|
||||
@ApiTags('Users')
|
||||
@Controller('users')
|
||||
@@ -9,30 +10,35 @@ export class UsersController {
|
||||
constructor(private users: UsersService) {}
|
||||
|
||||
@Get()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findAll() {
|
||||
return this.users.findAll();
|
||||
findAll(@Req() req) {
|
||||
const page = +(req.query.page || 1);
|
||||
const limit = +(req.query.limit || 20);
|
||||
return this.users.findAll(page, limit);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@Get('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
findById(@Param('id') id: string) {
|
||||
return this.users.findById(id);
|
||||
findMe(@Req() req) {
|
||||
return this.users.findById(req.user.sub);
|
||||
}
|
||||
|
||||
@Patch('me')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
update(@Req() req, @Body() data: any) {
|
||||
return this.users.update(req.user.sub, data);
|
||||
update(@Req() req, @Body() dto: UpdateUserDto) {
|
||||
return this.users.update(req.user.sub, dto);
|
||||
}
|
||||
|
||||
@Patch('fcm-token')
|
||||
@Patch('me/fcm-token')
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ApiBearerAuth()
|
||||
updateFcmToken(@Req() req, @Body() dto: { token: string }) {
|
||||
updateFcmToken(@Req() req, @Body() dto: FcmTokenDto) {
|
||||
return this.users.updateFcmToken(req.user.sub, dto.token);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findById(@Param('id') id: string) {
|
||||
return this.users.findById(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,13 @@ import { PrismaService } from '../prisma/prisma.service';
|
||||
export class UsersService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
findAll() {
|
||||
return this.prisma.users.findMany({ orderBy: { created_at: 'desc' } });
|
||||
async findAll(page = 1, limit = 20) {
|
||||
const skip = (page - 1) * limit;
|
||||
const [data, total] = await Promise.all([
|
||||
this.prisma.users.findMany({ skip, take: limit, orderBy: { created_at: 'desc' } }),
|
||||
this.prisma.users.count(),
|
||||
]);
|
||||
return { data, meta: { total, page, limit, totalPages: Math.ceil(total / limit) || 1 } };
|
||||
}
|
||||
|
||||
findById(id: string) {
|
||||
|
||||
Reference in New Issue
Block a user