Fix District/subDistrict

This commit is contained in:
Bright 2024-05-15 10:04:19 +07:00
parent 27fefee8d7
commit 20ccb3f26d
4 changed files with 57 additions and 17 deletions

View file

@ -18,6 +18,7 @@ import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { District, CreateDistrict, UpdateDistrict } from "../entities/District"; import { District, CreateDistrict, UpdateDistrict } from "../entities/District";
import { Province } from "../entities/Province";
import { Not } from "typeorm"; import { Not } from "typeorm";
@Route("api/v1/org/metadata/district") @Route("api/v1/org/metadata/district")
@ -30,6 +31,7 @@ import { Not } from "typeorm";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class DistrictController extends Controller { export class DistrictController extends Controller {
private districtRepository = AppDataSource.getRepository(District); private districtRepository = AppDataSource.getRepository(District);
private provinceRepository = AppDataSource.getRepository(Province);
/** /**
* API list * API list
@ -84,6 +86,13 @@ export class DistrictController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
} }
const chkProvince = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId }
})
if(!chkProvince){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางจังหวัดนี้");
}
const checkName = await this.districtRepository.findOne({ const checkName = await this.districtRepository.findOne({
where: { name: requestBody.name }, where: { name: requestBody.name },
}); });
@ -118,6 +127,14 @@ export class DistrictController extends Controller {
if (!_district) { if (!_district) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
} }
const chkProvince = await this.provinceRepository.findOne({
where: { id: requestBody.provinceId }
})
if(!chkProvince){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางจังหวัดนี้");
}
const checkName = await this.districtRepository.findOne({ const checkName = await this.districtRepository.findOne({
where: { id: Not(id), name: requestBody.name }, where: { id: Not(id), name: requestBody.name },
}); });

View file

@ -18,6 +18,7 @@ import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status"; import HttpStatusCode from "../interfaces/http-status";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import { SubDistrict, CreateSubDistrict, UpdateSubDistrict } from "../entities/SubDistrict"; import { SubDistrict, CreateSubDistrict, UpdateSubDistrict } from "../entities/SubDistrict";
import { District } from "../entities/District";
import { Not } from "typeorm"; import { Not } from "typeorm";
@Route("api/v1/org/metadata/subDistrict") @Route("api/v1/org/metadata/subDistrict")
@ -30,11 +31,12 @@ import { Not } from "typeorm";
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class SubDistrictController extends Controller { export class SubDistrictController extends Controller {
private subDistrictRepository = AppDataSource.getRepository(SubDistrict); private subDistrictRepository = AppDataSource.getRepository(SubDistrict);
private districtRepository = AppDataSource.getRepository(District);
/** /**
* API list * API list
* *
* @summary ORG_058 - CRUD (ADMIN) #62 * @summary ORG_058 - CRUD (ADMIN) #62
* *
*/ */
@Get() @Get()
@ -47,11 +49,11 @@ export class SubDistrictController extends Controller {
} }
/** /**
* API * API
* *
* @summary ORG_058 - CRUD (ADMIN) #62 * @summary ORG_058 - CRUD (ADMIN) #62
* *
* @param {string} id Id * @param {string} id Id
*/ */
@Get("{id}") @Get("{id}")
async GetById(@Path() id: string) { async GetById(@Path() id: string) {
@ -60,16 +62,16 @@ export class SubDistrictController extends Controller {
select: ["id", "name"], select: ["id", "name"],
}); });
if (!_subDistrict) { if (!_subDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางแขวงนี้");
} }
return new HttpSuccess(_subDistrict); return new HttpSuccess(_subDistrict);
} }
/** /**
* API body * API body
* *
* @summary ORG_058 - CRUD (ADMIN) #62 * @summary ORG_058 - CRUD (ADMIN) #62
* *
*/ */
@Post() @Post()
@ -80,7 +82,14 @@ export class SubDistrictController extends Controller {
) { ) {
const _subDistrict = Object.assign(new SubDistrict(), requestBody); const _subDistrict = Object.assign(new SubDistrict(), requestBody);
if (!_subDistrict) { if (!_subDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางแขวงนี้");
}
const chkDistrict = await this.districtRepository.findOne({
where: { id: requestBody.districtId }
})
if (!chkDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
} }
const checkName = await this.subDistrictRepository.findOne({ const checkName = await this.subDistrictRepository.findOne({
@ -100,11 +109,11 @@ export class SubDistrictController extends Controller {
} }
/** /**
* API body * API body
* *
* @summary ORG_058 - CRUD (ADMIN) #62 * @summary ORG_058 - CRUD (ADMIN) #62
* *
* @param {string} id Id * @param {string} id Id
*/ */
@Put("{id}") @Put("{id}")
async Put( async Put(
@ -115,8 +124,16 @@ export class SubDistrictController extends Controller {
) { ) {
const _subDistrict = await this.subDistrictRepository.findOne({ where: { id: id } }); const _subDistrict = await this.subDistrictRepository.findOne({ where: { id: id } });
if (!_subDistrict) { if (!_subDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางแขวงนี้");
} }
const chkDistrict = await this.districtRepository.findOne({
where: { id: requestBody.districtId }
})
if (!chkDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้");
}
const checkName = await this.subDistrictRepository.findOne({ const checkName = await this.subDistrictRepository.findOne({
where: { id: Not(id), name: requestBody.name }, where: { id: Not(id), name: requestBody.name },
}); });
@ -132,11 +149,11 @@ export class SubDistrictController extends Controller {
} }
/** /**
* API * API
* *
* @summary ORG_058 - CRUD (ADMIN) #62 * @summary ORG_058 - CRUD (ADMIN) #62
* *
* @param {string} id Id * @param {string} id Id
*/ */
@Delete("{id}") @Delete("{id}")
async Delete(@Path() id: string) { async Delete(@Path() id: string) {
@ -144,7 +161,7 @@ export class SubDistrictController extends Controller {
where: { id: id }, where: { id: id },
}); });
if (!_delSubDistrict) { if (!_delSubDistrict) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเพศนี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางแขวงนี้");
} }
await this.subDistrictRepository.delete(_delSubDistrict.id); await this.subDistrictRepository.delete(_delSubDistrict.id);
return new HttpSuccess(); return new HttpSuccess();

View file

@ -36,6 +36,9 @@ export class District extends EntityBase {
export class CreateDistrict { export class CreateDistrict {
@Column() @Column()
name: string; name: string;
@Column()
provinceId: string;
} }
export type UpdateDistrict = Partial<CreateDistrict>; export type UpdateDistrict = Partial<CreateDistrict>;

View file

@ -40,6 +40,9 @@ export class SubDistrict extends EntityBase {
export class CreateSubDistrict { export class CreateSubDistrict {
@Column() @Column()
name: string; name: string;
@Column()
districtId: string;
} }
export type UpdateSubDistrict = Partial<CreateSubDistrict>; export type UpdateSubDistrict = Partial<CreateSubDistrict>;