feat: 咨询分类接口

This commit is contained in:
ilhamtahir committed 2024-11-18 15:49:47 +08:00
1 parent 11b41a1a3e
commit 3283d94ea4
26 files changed
+354 -17

No files matched your search

+2 -1
View File
@@ -1,7 +1,7 @@
import { Body, Controller, HttpStatus, Post } from '@nestjs/common';
import { RoleService } from '../service/role.service';
import { CreateRoleRequest } from '../dto/create-role.request';
import { ApiResponse } from '@nestjs/swagger';
import { ApiBearerAuth, ApiResponse } from '@nestjs/swagger';
import { RoleVo } from '../vo/role.vo';
import { RoleMapper } from '../mapper/role.mapper';
@@ -14,6 +14,7 @@ export class RoleController {
description: 'Role',
type: RoleVo,
})
@ApiBearerAuth()
@Post()
async create(@Body() createRoleRequest: CreateRoleRequest) {
return RoleMapper.toVo(await this.roleService.create(createRoleRequest));
+4 -3
View File
@@ -1,25 +1,26 @@
import { Controller, Get, HttpStatus, Query } from '@nestjs/common';
import { UserService } from '../service/user.service';
import { SearchUserFilter } from '../dto/serach-user.filter';
import { SearchUserFilter } from '../dto/search-user.filter';
import { NoAuth } from '../decorators/no-auth.decorator';
import { PageResultMapper } from '../mapper/page-result.mapper';
import { UserVo } from '../vo/user.vo';
import { UserMapper } from '../mapper/user.mapper';
import {
ApiBearerAuth,
ApiExtraModels,
ApiOkResponse,
ApiResponse,
getSchemaPath,
} from '@nestjs/swagger';
import { PageResult } from '../vo/page-result';
@Controller('user')
@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}
@Get()
@NoAuth()
@ApiExtraModels(PageResult, UserVo) // 注册额外模型
@ApiBearerAuth()
@ApiOkResponse({
description: '分页用户列表',
schema: {
+1
View File
@@ -36,6 +36,7 @@ import { UserController } from './controller/user.controller';
database: configService.get<string>('DB_NAME'),
autoLoadEntities: true,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
subscribers: [__dirname + '/**/*.subscriber{.ts,.js}'],
synchronize: ['development', 'test'].includes(process.env.NODE_ENV),
logging: true,
}),
+16 -1
View File
@@ -40,5 +40,20 @@ export abstract class BaseFilter {
}
// 获取查询条件(由子类实现)
abstract getConditions(): Record<string, any>;
getConditions(): Record<string, any> {
return {};
}
// 解析排序参数
getOrderBy(): Record<string, 'ASC' | 'DESC'> {
const orderConditions: Record<string, 'ASC' | 'DESC'> = {};
if (this.order) {
this.order.forEach((condition) => {
const direction = condition[0] === '+' ? 'ASC' : 'DESC';
const field = condition.substring(1); // 移除第一个字符 (+ 或 -)
orderConditions[field] = direction;
});
}
return orderConditions;
}
}
File renamed without changes.
+2 -2
View File
@@ -3,9 +3,9 @@ import { ManyToOne } from 'typeorm';
import { User } from './user.entity';
export abstract class TraceableEntity extends BaseEntity {
@ManyToOne(() => User)
@ManyToOne(() => User, { eager: true })
createBy: User;
@ManyToOne(() => User)
@ManyToOne(() => User, { eager: true })
updateBy: User;
}
@@ -44,7 +44,8 @@ export class GlobalExceptionFilter implements ExceptionFilter {
const message =
typeof exceptionResponse === 'string'
? exceptionResponse
: (exceptionResponse as any).message || 'An unexpected error occurred.';
: (exceptionResponse as ErrorResponse).message ||
'An unexpected error occurred.';
return {
status,
+2 -1
View File
@@ -45,6 +45,7 @@ export class AuthGuard implements CanActivate {
}
// 将用户信息附加到请求对象
this.userService.setCurrentUser(user);
request.user = user;
return true;
} catch (error) {
@@ -52,7 +53,7 @@ export class AuthGuard implements CanActivate {
}
}
private extractTokenFromHeader(request: any): string | null {
const authHeader = request.headers['Authorization'];
const authHeader = request.headers['authorization'];
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return null;
}
+2 -2
View File
@@ -10,8 +10,8 @@ export class PageResultMapper {
return {
data: data,
pagination: {
page,
size,
page: Number(page),
size: Number(size),
total,
},
};
+2 -1
View File
@@ -3,7 +3,8 @@ import { UserVo } from '../vo/user.vo';
import { DateUtil } from '../util/date.util';
export class UserMapper {
static toVo(entity: User): UserVo {
static toVo(entity?: User): UserVo {
if (!entity) return null;
return {
id: entity.id,
username: entity.username,
+17 -1
View File
@@ -3,7 +3,8 @@ import { InjectRepository } from '@nestjs/typeorm';
import { User } from '../entity/user.entity';
import { Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';
import { SearchUserFilter } from '../dto/serach-user.filter';
import { SearchUserFilter } from '../dto/search-user.filter';
import { JwtPayload } from 'jsonwebtoken';
@Injectable()
export class UserService {
@@ -11,6 +12,21 @@ export class UserService {
@InjectRepository(User) private readonly userRepository: Repository<User>,
) {}
private currentUser: User;
setCurrentUser(user: User) {
this.currentUser = user;
}
getCurrentUser() {
return this.currentUser;
}
async setCurrentUserByJwtPayload(payload: JwtPayload) {
const user = await this.userRepository.findOneBy({ id: payload.sub });
this.setCurrentUser(user);
}
async findById(id: string) {
return this.userRepository.findOne({
where: {
+4 -1
View File
@@ -1,6 +1,9 @@
import { BaseVo } from './base.vo';
import { ApiProperty } from '@nestjs/swagger';
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
@ApiSchema({
name: 'Role',
})
export class RoleVo extends BaseVo {
@ApiProperty()
name: string;
+11
View File
@@ -0,0 +1,11 @@
import { BaseVo } from './base.vo';
import { UserVo } from './user.vo';
import { ApiProperty } from '@nestjs/swagger';
export class TraceableVo extends BaseVo {
@ApiProperty()
createBy: UserVo | null;
@ApiProperty()
updateBy: UserVo | null;
}