Files
ShaFaFanXin/后端/src/user/user.controller.ts

109 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, HttpCode } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { UserService } from './user.service';
import { CreateUserDto, UpdateUserDto } from './dto/user.dto';
import type { CurrentUserData } from '../auth/decorators/current-user.decorator';
import { CurrentUser } from '../auth/decorators/current-user.decorator';
import { Roles } from '../auth/guards/roles.decorator';
@ApiTags('用户管理')
@ApiBearerAuth()
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Post()
@HttpCode(200)
@Roles('admin')
@ApiOperation({ summary: '创建用户(管理员)' })
@ApiResponse({ status: 200, description: '创建成功' })
async create(@Body() createUserDto: CreateUserDto) {
const data = await this.userService.create(createUserDto);
return {
code: 0,
message: '创建成功',
data,
};
}
@Get()
@Roles('admin')
@ApiOperation({ summary: '获取所有用户(管理员)' })
@ApiResponse({ status: 200, description: '获取成功' })
async findAll() {
const list = await this.userService.findAll();
return {
code: 0,
message: '获取成功',
data: {
list,
total: list.length,
},
};
}
@Get('profile')
@ApiOperation({ summary: '获取当前用户信息' })
@ApiResponse({ status: 200, description: '获取成功' })
async getProfile(@CurrentUser() user: CurrentUserData) {
const data = await this.userService.getUserProfile(user.userId);
return {
code: 0,
message: '获取成功',
data,
};
}
@Get(':id')
@Roles('admin')
@ApiOperation({ summary: '根据ID获取用户管理员' })
@ApiResponse({ status: 200, description: '获取成功' })
@ApiResponse({ status: 404, description: '用户不存在' })
async findOne(@Param('id') id: string) {
const data = await this.userService.findById(+id);
return {
code: 0,
message: '获取成功',
data,
};
}
@Patch('profile')
@ApiOperation({ summary: '更新当前用户信息' })
@ApiResponse({ status: 200, description: '更新成功' })
async updateProfile(@CurrentUser() user: CurrentUserData, @Body() updateUserDto: UpdateUserDto) {
const data = await this.userService.update(user.userId, updateUserDto);
return {
code: 0,
message: '更新成功',
data,
};
}
@Patch(':id')
@Roles('admin')
@ApiOperation({ summary: '更新用户信息(管理员)' })
@ApiResponse({ status: 200, description: '更新成功' })
@ApiResponse({ status: 404, description: '用户不存在' })
async update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) {
const data = await this.userService.update(+id, updateUserDto);
return {
code: 0,
message: '更新成功',
data,
};
}
@Delete(':id')
@Roles('admin')
@ApiOperation({ summary: '删除用户(管理员)' })
@ApiResponse({ status: 200, description: '删除成功' })
@ApiResponse({ status: 404, description: '用户不存在' })
async remove(@Param('id') id: string) {
await this.userService.remove(+id);
return {
code: 0,
message: '删除成功',
};
}
}