feat:"完成页面接口的对接"
This commit is contained in:
@@ -10,7 +10,7 @@ export class CreateServiceDto {
|
||||
@IsString()
|
||||
description: string;
|
||||
|
||||
@IsEnum(['fabric', 'leather', 'cleaning', 'repair', 'custom'])
|
||||
@IsEnum(['leather', 'fabric', 'functional', 'antique', 'office', 'cleaning', 'repair', 'custom'])
|
||||
type: string;
|
||||
|
||||
@IsNotEmpty({ message: '基础价格不能为空' })
|
||||
@@ -19,6 +19,10 @@ export class CreateServiceDto {
|
||||
@Min(0)
|
||||
basePrice: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
icon?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
images?: string[];
|
||||
@@ -49,7 +53,7 @@ export class UpdateServiceDto {
|
||||
description?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(['fabric', 'leather', 'cleaning', 'repair', 'custom'])
|
||||
@IsEnum(['leather', 'fabric', 'functional', 'antique', 'office', 'cleaning', 'repair', 'custom'])
|
||||
type?: string;
|
||||
|
||||
@IsOptional()
|
||||
@@ -58,6 +62,10 @@ export class UpdateServiceDto {
|
||||
@Min(0)
|
||||
basePrice?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
icon?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
images?: string[];
|
||||
@@ -84,10 +92,14 @@ export class UpdateServiceDto {
|
||||
|
||||
export class QueryServiceDto {
|
||||
@IsOptional()
|
||||
@IsEnum(['fabric', 'leather', 'cleaning', 'repair', 'custom'])
|
||||
@IsEnum(['leather', 'fabric', 'functional', 'antique', 'office', 'cleaning', 'repair', 'custom'])
|
||||
type?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(['active', 'inactive'])
|
||||
status?: string = 'active';
|
||||
status?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
keyword?: string;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards, HttpCode } from '@nestjs/common';
|
||||
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
|
||||
import { ServiceService } from './service.service';
|
||||
import { CreateServiceDto, UpdateServiceDto, QueryServiceDto } from './dto/service.dto';
|
||||
@@ -12,30 +12,47 @@ export class ServiceController {
|
||||
|
||||
@ApiBearerAuth()
|
||||
@Post()
|
||||
@HttpCode(200)
|
||||
@Roles('admin')
|
||||
@ApiOperation({ summary: '创建服务(管理员)' })
|
||||
@ApiResponse({ status: 201, description: '创建成功' })
|
||||
@ApiResponse({ status: 200, description: '创建成功' })
|
||||
@ApiResponse({ status: 409, description: '服务类型已存在' })
|
||||
create(@Body() createServiceDto: CreateServiceDto) {
|
||||
return this.serviceService.create(createServiceDto);
|
||||
async create(@Body() createServiceDto: CreateServiceDto) {
|
||||
const data = await this.serviceService.create(createServiceDto);
|
||||
return {
|
||||
code: 0,
|
||||
message: '创建成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get()
|
||||
@ApiOperation({ summary: '获取服务列表' })
|
||||
@ApiQuery({ name: 'type', required: false, enum: ['fabric', 'leather', 'cleaning', 'repair', 'custom'] })
|
||||
@ApiQuery({ name: 'status', required: false, enum: ['active', 'inactive'], description: '默认为active' })
|
||||
@ApiQuery({ name: 'type', required: false, enum: ['leather', 'fabric', 'functional', 'antique', 'office', 'cleaning', 'repair', 'custom'] })
|
||||
@ApiQuery({ name: 'status', required: false, enum: ['active', 'inactive'] })
|
||||
@ApiQuery({ name: 'keyword', required: false, description: '搜索关键词' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
findAll(@Query() query: QueryServiceDto) {
|
||||
return this.serviceService.findAll(query);
|
||||
async findAll(@Query() query: QueryServiceDto) {
|
||||
const data = await this.serviceService.findAll(query);
|
||||
return {
|
||||
code: 0,
|
||||
message: '获取成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@Public()
|
||||
@Get('active')
|
||||
@ApiOperation({ summary: '获取所有有效服务' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
getActiveServices() {
|
||||
return this.serviceService.getActiveServices();
|
||||
async getActiveServices() {
|
||||
const data = await this.serviceService.getActiveServices();
|
||||
return {
|
||||
code: 0,
|
||||
message: '获取成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@Public()
|
||||
@@ -43,8 +60,13 @@ export class ServiceController {
|
||||
@ApiOperation({ summary: '根据ID获取服务详情' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
@ApiResponse({ status: 404, description: '服务不存在' })
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.serviceService.findOne(+id);
|
||||
async findOne(@Param('id') id: string) {
|
||||
const data = await this.serviceService.findOne(+id);
|
||||
return {
|
||||
code: 0,
|
||||
message: '获取成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@Public()
|
||||
@@ -52,8 +74,13 @@ export class ServiceController {
|
||||
@ApiOperation({ summary: '根据类型获取服务' })
|
||||
@ApiResponse({ status: 200, description: '获取成功' })
|
||||
@ApiResponse({ status: 404, description: '服务类型不存在' })
|
||||
findByType(@Param('type') type: string) {
|
||||
return this.serviceService.findByType(type);
|
||||
async findByType(@Param('type') type: string) {
|
||||
const data = await this.serviceService.findByType(type);
|
||||
return {
|
||||
code: 0,
|
||||
message: '获取成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@@ -63,8 +90,13 @@ export class ServiceController {
|
||||
@ApiResponse({ status: 200, description: '更新成功' })
|
||||
@ApiResponse({ status: 404, description: '服务不存在' })
|
||||
@ApiResponse({ status: 409, description: '服务类型已存在' })
|
||||
update(@Param('id') id: string, @Body() updateServiceDto: UpdateServiceDto) {
|
||||
return this.serviceService.update(+id, updateServiceDto);
|
||||
async update(@Param('id') id: string, @Body() updateServiceDto: UpdateServiceDto) {
|
||||
const data = await this.serviceService.update(+id, updateServiceDto);
|
||||
return {
|
||||
code: 0,
|
||||
message: '更新成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@@ -73,8 +105,12 @@ export class ServiceController {
|
||||
@ApiOperation({ summary: '删除服务(管理员)' })
|
||||
@ApiResponse({ status: 200, description: '删除成功' })
|
||||
@ApiResponse({ status: 404, description: '服务不存在' })
|
||||
remove(@Param('id') id: string) {
|
||||
return this.serviceService.remove(+id);
|
||||
async remove(@Param('id') id: string) {
|
||||
await this.serviceService.remove(+id);
|
||||
return {
|
||||
code: 0,
|
||||
message: '删除成功',
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@@ -83,8 +119,13 @@ export class ServiceController {
|
||||
@ApiOperation({ summary: '切换服务状态(管理员)' })
|
||||
@ApiResponse({ status: 200, description: '状态切换成功' })
|
||||
@ApiResponse({ status: 404, description: '服务不存在' })
|
||||
toggleStatus(@Param('id') id: string) {
|
||||
return this.serviceService.toggleStatus(+id);
|
||||
async toggleStatus(@Param('id') id: string) {
|
||||
const data = await this.serviceService.toggleStatus(+id);
|
||||
return {
|
||||
code: 0,
|
||||
message: '状态切换成功',
|
||||
data,
|
||||
};
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@@ -92,7 +133,11 @@ export class ServiceController {
|
||||
@Roles('admin')
|
||||
@ApiOperation({ summary: '更新服务排序(管理员)' })
|
||||
@ApiResponse({ status: 200, description: '排序更新成功' })
|
||||
updateSortOrder(@Body() serviceOrders: { id: number; sortOrder: number }[]) {
|
||||
return this.serviceService.updateSortOrder(serviceOrders);
|
||||
async updateSortOrder(@Body() serviceOrders: { id: number; sortOrder: number }[]) {
|
||||
await this.serviceService.updateSortOrder(serviceOrders);
|
||||
return {
|
||||
code: 0,
|
||||
message: '排序更新成功',
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -29,8 +29,8 @@ export class ServiceService {
|
||||
return this.serviceRepository.save(service);
|
||||
}
|
||||
|
||||
async findAll(query: QueryServiceDto): Promise<Service[]> {
|
||||
const { type, status } = query;
|
||||
async findAll(query: QueryServiceDto): Promise<any> {
|
||||
const { type, status, keyword } = query;
|
||||
const queryBuilder = this.serviceRepository.createQueryBuilder('service');
|
||||
|
||||
if (type) {
|
||||
@@ -41,10 +41,22 @@ export class ServiceService {
|
||||
queryBuilder.andWhere('service.status = :status', { status });
|
||||
}
|
||||
|
||||
if (keyword) {
|
||||
queryBuilder.andWhere(
|
||||
'(service.name LIKE :keyword OR service.description LIKE :keyword)',
|
||||
{ keyword: `%${keyword}%` }
|
||||
);
|
||||
}
|
||||
|
||||
queryBuilder.orderBy('service.sortOrder', 'ASC');
|
||||
queryBuilder.addOrderBy('service.createdAt', 'DESC');
|
||||
|
||||
return queryBuilder.getMany();
|
||||
const services = await queryBuilder.getMany();
|
||||
|
||||
return {
|
||||
list: services,
|
||||
total: services.length
|
||||
};
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<Service> {
|
||||
@@ -94,11 +106,16 @@ export class ServiceService {
|
||||
await this.serviceRepository.remove(service);
|
||||
}
|
||||
|
||||
async getActiveServices(): Promise<Service[]> {
|
||||
return this.serviceRepository.find({
|
||||
async getActiveServices(): Promise<any> {
|
||||
const services = await this.serviceRepository.find({
|
||||
where: { status: 'active' },
|
||||
order: { sortOrder: 'ASC', createdAt: 'DESC' }
|
||||
});
|
||||
|
||||
return {
|
||||
list: services,
|
||||
total: services.length
|
||||
};
|
||||
}
|
||||
|
||||
async updateSortOrder(serviceOrders: { id: number; sortOrder: number }[]): Promise<void> {
|
||||
|
||||
Reference in New Issue
Block a user