119 lines
3.7 KiB
TypeScript
119 lines
3.7 KiB
TypeScript
|
|
import { Body, Context, Controller, Del, Get, Inject, Param, Post, Put, Query } from '@midwayjs/core';
|
|
import {
|
|
ApiBearerAuth,
|
|
ApiBody,
|
|
ApiExtension,
|
|
ApiOkResponse,
|
|
ApiOperation,
|
|
ApiTags,
|
|
} from '@midwayjs/swagger';
|
|
import { AreaService } from '../service/area.service';
|
|
import { CreateAreaDTO, QueryAreaDTO, UpdateAreaDTO } from '../dto/area.dto';
|
|
import { errorResponse, successResponse } from '../utils/response.util';
|
|
import { Area } from '../entity/area.entity';
|
|
import * as countries from 'i18n-iso-countries';
|
|
|
|
@ApiBearerAuth()
|
|
@ApiTags('Area')
|
|
@Controller('/area')
|
|
export class AreaController {
|
|
@Inject()
|
|
ctx: Context;
|
|
|
|
@Inject()
|
|
areaService: AreaService;
|
|
|
|
@ApiOperation({ summary: '获取国家列表' })
|
|
@ApiOkResponse({ description: '国家列表' })
|
|
@Get('/countries')
|
|
async getCountries() {
|
|
try {
|
|
// 注册中文语言包
|
|
countries.registerLocale(require('i18n-iso-countries/langs/zh.json'));
|
|
// 获取所有国家的中文名称
|
|
const countryNames = countries.getNames('zh', { select: 'official' });
|
|
// 格式化为 { code, name } 的数组
|
|
const countryList = Object.keys(countryNames).map(code => ({
|
|
code,
|
|
name: countryNames[code],
|
|
}));
|
|
return successResponse(countryList, '查询成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOperation({ summary: '创建区域' })
|
|
@ApiBody({ type: CreateAreaDTO })
|
|
@ApiOkResponse({ type: Area, description: '成功创建的区域' })
|
|
@Post('/')
|
|
async createArea(@Body() area: CreateAreaDTO) {
|
|
try {
|
|
const newArea = await this.areaService.createArea(area);
|
|
return successResponse(newArea, '创建成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOperation({ summary: '更新区域' })
|
|
@ApiBody({ type: UpdateAreaDTO })
|
|
@ApiOkResponse({ type: Area, description: '成功更新的区域' })
|
|
@Put('/:id')
|
|
async updateArea(@Param('id') id: number, @Body() area: UpdateAreaDTO) {
|
|
try {
|
|
const updatedArea = await this.areaService.updateArea(id, area);
|
|
return successResponse(updatedArea, '更新成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOperation({ summary: '删除区域' })
|
|
@ApiOkResponse({ description: '删除成功' })
|
|
@Del('/:id')
|
|
async deleteArea(@Param('id') id: number) {
|
|
try {
|
|
await this.areaService.deleteArea(id);
|
|
return successResponse(null, '删除成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOperation({ summary: '获取区域列表(分页)' })
|
|
@ApiOkResponse({ type: [Area], description: '区域列表' })
|
|
@ApiExtension('x-pagination', { currentPage: 1, pageSize: 10, total: 100 })
|
|
@Get('/')
|
|
async getAreaList(@Query() query: QueryAreaDTO) {
|
|
try {
|
|
const { list, total } = await this.areaService.getAreaList(query);
|
|
return successResponse({ list, total }, '查询成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
|
|
@ApiOperation({ summary: '根据ID获取区域详情' })
|
|
@ApiOkResponse({ type: Area, description: '区域详情' })
|
|
@Get('/:id')
|
|
async getAreaById(@Param('id') id: number) {
|
|
try {
|
|
const area = await this.areaService.getAreaById(id);
|
|
if (!area) {
|
|
return errorResponse('区域不存在');
|
|
}
|
|
return successResponse(area, '查询成功');
|
|
} catch (error) {
|
|
console.log(error);
|
|
return errorResponse(error?.message || error);
|
|
}
|
|
}
|
|
}
|