From 580e53809d5dd1011226463147ea5fc186c028e3 Mon Sep 17 00:00:00 2001 From: Bright Date: Mon, 10 Jun 2024 14:30:59 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=81=E0=B8=81=E0=B9=89=E0=B8=A5=E0=B8=9A?= =?UTF-8?q?=E0=B8=82=E0=B9=89=E0=B8=AD=E0=B8=A1=E0=B8=B9=E0=B8=A5=E0=B8=88?= =?UTF-8?q?=E0=B8=B1=E0=B8=87=E0=B8=AB=E0=B8=A7=E0=B8=B1=E0=B8=94/?= =?UTF-8?q?=E0=B8=AD=E0=B8=B3=E0=B9=80=E0=B8=A0=E0=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/DistrictController.ts | 21 +++++++++++++++------ src/controllers/ProvinceController.ts | 21 +++++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/controllers/DistrictController.ts b/src/controllers/DistrictController.ts index 778cc249..18ea210b 100644 --- a/src/controllers/DistrictController.ts +++ b/src/controllers/DistrictController.ts @@ -158,13 +158,22 @@ export class DistrictController extends Controller { */ @Delete("{id}") async Delete(@Path() id: string) { - const _delDistrict = await this.districtRepository.findOne({ - where: { id: id }, - }); - if (!_delDistrict) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้"); + // const _delDistrict = await this.districtRepository.findOne({ + // where: { id: id }, + // }); + // if (!_delDistrict) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางเขตนี้"); + // } + // await this.districtRepository.delete(_delDistrict.id); + let result :any + try { + result = await this.districtRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานข้อมูลเขต/อำเภอนี้อยู่"); + } + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - await this.districtRepository.delete(_delDistrict.id); return new HttpSuccess(); } } diff --git a/src/controllers/ProvinceController.ts b/src/controllers/ProvinceController.ts index 44f77347..60f59566 100644 --- a/src/controllers/ProvinceController.ts +++ b/src/controllers/ProvinceController.ts @@ -141,13 +141,22 @@ export class ProvinceController extends Controller { */ @Delete("{id}") async Delete(@Path() id: string) { - const _delProvince = await this.provinceRepository.findOne({ - where: { id: id }, - }); - if (!_delProvince) { - throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางจังหวัดนี้"); + // const _delProvince = await this.provinceRepository.findOne({ + // where: { id: id }, + // }); + // if (!_delProvince) { + // throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลสถานภาพทางจังหวัดนี้"); + // } + // await this.provinceRepository.delete(_delProvince.id); + let result :any + try { + result = await this.provinceRepository.delete({ id: id }); + } catch { + throw new HttpError( HttpStatusCode.NOT_FOUND, "ไม่สามารถลบได้เนื่องจากมีการใช้งานข้อมูลจังหวัดนี้อยู่"); + } + if (result.affected == undefined || result.affected <= 0) { + throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - await this.provinceRepository.delete(_delProvince.id); return new HttpSuccess(); } }