update: add option for region
This commit is contained in:
164
src/controllers/region.ts
Normal file
164
src/controllers/region.ts
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
import { Response, NextFunction, Application } from "express";
|
||||||
|
import { Request } from "express-jwt";
|
||||||
|
import { ReturnHelper } from "../helpers/express/return";
|
||||||
|
import { OrmHelper } from "../helpers/orm";
|
||||||
|
import Joi from "joi";
|
||||||
|
import { ILogObj, Logger } from "tslog";
|
||||||
|
import { Language } from "../langs/lang";
|
||||||
|
import { Paging } from "entity";
|
||||||
|
import CommonHelper from "../helpers/common";
|
||||||
|
import * as fastcsv from 'fast-csv';
|
||||||
|
import dayjs from "dayjs";
|
||||||
|
import { Province, City, Subdistrict, Ward } from "entity";
|
||||||
|
|
||||||
|
const log: Logger<ILogObj> = new Logger({ name: '[RegionController]', type: 'pretty' });
|
||||||
|
|
||||||
|
export class RegionController {
|
||||||
|
static async province(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Region']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const repo = OrmHelper.DB.getRepository(Province);
|
||||||
|
|
||||||
|
const res_count = repo.createQueryBuilder("province");
|
||||||
|
const res_list = repo.createQueryBuilder("province");
|
||||||
|
|
||||||
|
const current_page = 1;
|
||||||
|
const total_count_data = await res_count.getCount();
|
||||||
|
const list_data = await res_list.getMany();
|
||||||
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
|
||||||
|
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async city(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Region']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['province_code'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Province Code.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { province_code } = req.params;
|
||||||
|
|
||||||
|
if (!/^\d{2}$/.test(province_code)) {
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, 0, 1, 0, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(City);
|
||||||
|
const res_count = repo.createQueryBuilder("city")
|
||||||
|
.where("city.code LIKE :province_code", { province_code: `${province_code}%` });
|
||||||
|
const res_list = repo.createQueryBuilder("city")
|
||||||
|
.where("city.code LIKE :province_code", { province_code: `${province_code}%` });
|
||||||
|
|
||||||
|
const current_page = 1;
|
||||||
|
const total_count_data = await res_count.getCount();
|
||||||
|
const list_data = await res_list.getMany();
|
||||||
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async subdistric(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Region']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['city_code'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'City Code.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { city_code } = req.params;
|
||||||
|
|
||||||
|
if (!/^\d{2}\.\d{2}$/.test(city_code)) {
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, 0, 1, 0, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Subdistrict);
|
||||||
|
const res_count = repo.createQueryBuilder("subdistrict")
|
||||||
|
.where("subdistrict.code LIKE :city_code", { city_code: `${city_code}%` });
|
||||||
|
const res_list = repo.createQueryBuilder("subdistrict")
|
||||||
|
.where("subdistrict.code LIKE :city_code", { city_code: `${city_code}%` });
|
||||||
|
|
||||||
|
const current_page = 1;
|
||||||
|
const total_count_data = await res_count.getCount();
|
||||||
|
const list_data = await res_list.getMany();
|
||||||
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async ward(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
|
/*
|
||||||
|
#swagger.tags = ['Region']
|
||||||
|
#swagger.security = [{
|
||||||
|
"bearerAuth": []
|
||||||
|
}]
|
||||||
|
#swagger.parameters['subdistric_code'] = {
|
||||||
|
in: 'path',
|
||||||
|
description: 'Subdistric Code.',
|
||||||
|
required: true,
|
||||||
|
type: 'string'
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { subdistric_code } = req.params;
|
||||||
|
|
||||||
|
if (!/^\d{2}\.\d{2}\.\d{2}$/.test(subdistric_code)) {
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, 0, 1, 0, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
const repo = OrmHelper.DB.getRepository(Ward);
|
||||||
|
const res_count = repo.createQueryBuilder("ward")
|
||||||
|
.where("ward.code LIKE :subdistric_code", { subdistric_code: `${subdistric_code}%` });
|
||||||
|
const res_list = repo.createQueryBuilder("ward")
|
||||||
|
.where("ward.code LIKE :subdistric_code", { subdistric_code: `${subdistric_code}%` });
|
||||||
|
|
||||||
|
const current_page = 1;
|
||||||
|
const total_count_data = await res_count.getCount();
|
||||||
|
const list_data = await res_list.getMany();
|
||||||
|
const count_data = CommonHelper.countObject(list_data);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
||||||
|
} catch (e: unknown) {
|
||||||
|
log.error(e);
|
||||||
|
const err = e as Error;
|
||||||
|
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_view, err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -63,6 +63,7 @@ import { FareTypeController } from '../controllers/faretype';
|
|||||||
import { CardTypeController } from '../controllers/card_type';
|
import { CardTypeController } from '../controllers/card_type';
|
||||||
import { PaymentMethodController } from '../controllers/payment-method';
|
import { PaymentMethodController } from '../controllers/payment-method';
|
||||||
import { RegistrationFeeController } from '../controllers/registrationfee';
|
import { RegistrationFeeController } from '../controllers/registrationfee';
|
||||||
|
import { RegionController } from '../controllers/region';
|
||||||
|
|
||||||
export class RoutePrivate {
|
export class RoutePrivate {
|
||||||
static setup(app: express.Application) {
|
static setup(app: express.Application) {
|
||||||
@ -480,9 +481,14 @@ export class RoutePrivate {
|
|||||||
app.put('/api/payment-method/update/:id', PaymentMethodController.update)
|
app.put('/api/payment-method/update/:id', PaymentMethodController.update)
|
||||||
app.delete('/api/payment-method/delete/:id/:hard', PaymentMethodController.delete)
|
app.delete('/api/payment-method/delete/:id/:hard', PaymentMethodController.delete)
|
||||||
app.put('/api/payment-method/restore/:id', PaymentMethodController.restore)
|
app.put('/api/payment-method/restore/:id', PaymentMethodController.restore)
|
||||||
|
|
||||||
app.get('/api/registration-fee/detail', RegistrationFeeController.detail)
|
app.get('/api/registration-fee/detail', RegistrationFeeController.detail)
|
||||||
app.post('/api/registration-fee/create', RegistrationFeeController.create)
|
app.post('/api/registration-fee/create', RegistrationFeeController.create)
|
||||||
app.put('/api/registration-fee/update/:id', RegistrationFeeController.update)
|
app.put('/api/registration-fee/update/:id', RegistrationFeeController.update)
|
||||||
|
|
||||||
|
app.get('/api/region/province', RegionController.province)
|
||||||
|
app.get('/api/region/city/:province_code', RegionController.city)
|
||||||
|
app.get('/api/region/subdistrict/:city_code', RegionController.subdistric)
|
||||||
|
app.get('/api/region/ward/:subdistric_code', RegionController.ward)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user