From 439e1ffaa474c2d2287925bb9349b8dc6343b4a1 Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Wed, 28 Feb 2024 10:36:44 +0700 Subject: [PATCH] remove try catch --- src/controllers/BloodGroupController.ts | 53 +++++---------- src/controllers/EducationLevelController.ts | 72 ++++++++++----------- src/controllers/GenderController.ts | 52 +++++---------- 3 files changed, 67 insertions(+), 110 deletions(-) diff --git a/src/controllers/BloodGroupController.ts b/src/controllers/BloodGroupController.ts index f746073c..cfb9447b 100644 --- a/src/controllers/BloodGroupController.ts +++ b/src/controllers/BloodGroupController.ts @@ -50,17 +50,13 @@ export class BloodGroupController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - const bloodGroup = Object.assign(new BloodGroup(), requestBody); - bloodGroup.createdUserId = request.user.sub; - bloodGroup.createdFullName = request.user.name; - bloodGroup.lastUpdateUserId = request.user.sub; - bloodGroup.lastUpdateFullName = request.user.name; - await this.bloodGroupRepository.save(bloodGroup); - return new HttpSuccess(); - } catch (error) { - return error; - } + const bloodGroup = Object.assign(new BloodGroup(), requestBody); + bloodGroup.createdUserId = request.user.sub; + bloodGroup.createdFullName = request.user.name; + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); } /** @@ -90,15 +86,11 @@ export class BloodGroupController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - bloodGroup.lastUpdateUserId = request.user.sub; - bloodGroup.lastUpdateFullName = request.user.name; - this.bloodGroupRepository.merge(bloodGroup, requestBody); - await this.bloodGroupRepository.save(bloodGroup); - return new HttpSuccess(); - } catch (error) { - return error; - } + bloodGroup.lastUpdateUserId = request.user.sub; + bloodGroup.lastUpdateFullName = request.user.name; + this.bloodGroupRepository.merge(bloodGroup, requestBody); + await this.bloodGroupRepository.save(bloodGroup); + return new HttpSuccess(); } /** @@ -116,12 +108,9 @@ export class BloodGroupController extends Controller { if (!delBloodGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } - try { - await this.bloodGroupRepository.delete({ id: id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + + await this.bloodGroupRepository.delete({ id: id }); + return new HttpSuccess(); } /** @@ -140,11 +129,7 @@ export class BloodGroupController extends Controller { if (!bloodGroup) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - try { - return new HttpSuccess(bloodGroup); - } catch (error) { - return error; - } + return new HttpSuccess(bloodGroup); } /** @@ -163,10 +148,6 @@ export class BloodGroupController extends Controller { if (!bloodGroup) { return new HttpSuccess([]); } - try { - return new HttpSuccess(bloodGroup); - } catch (error) { - return error; - } + return new HttpSuccess(bloodGroup); } } diff --git a/src/controllers/EducationLevelController.ts b/src/controllers/EducationLevelController.ts index a8ba9cbe..66169e24 100644 --- a/src/controllers/EducationLevelController.ts +++ b/src/controllers/EducationLevelController.ts @@ -50,17 +50,13 @@ export class EducationLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - const educationLevel = Object.assign(new EducationLevel(), requestBody); - educationLevel.createdUserId = request.user.sub; - educationLevel.createdFullName = request.user.name; - educationLevel.lastUpdateUserId = request.user.sub; - educationLevel.lastUpdateFullName = request.user.name; - await this.educationLevelRepository.save(educationLevel); - return new HttpSuccess(); - } catch (error) { - return error; - } + const educationLevel = Object.assign(new EducationLevel(), requestBody); + educationLevel.createdUserId = request.user.sub; + educationLevel.createdFullName = request.user.name; + educationLevel.lastUpdateUserId = request.user.sub; + educationLevel.lastUpdateFullName = request.user.name; + await this.educationLevelRepository.save(educationLevel); + return new HttpSuccess(); } /** @@ -90,15 +86,11 @@ export class EducationLevelController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - educationLevel.lastUpdateUserId = request.user.sub; - educationLevel.lastUpdateFullName = request.user.name; - this.educationLevelRepository.merge(educationLevel, requestBody); - await this.educationLevelRepository.save(educationLevel); - return new HttpSuccess(); - } catch (error) { - return error; - } + educationLevel.lastUpdateUserId = request.user.sub; + educationLevel.lastUpdateFullName = request.user.name; + this.educationLevelRepository.merge(educationLevel, requestBody); + await this.educationLevelRepository.save(educationLevel); + return new HttpSuccess(); } /** @@ -116,12 +108,8 @@ export class EducationLevelController extends Controller { if (!delEducationLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งตามไอดีนี้ : " + id); } - try { - await this.educationLevelRepository.delete({ id: id }); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.educationLevelRepository.delete({ id: id }); + return new HttpSuccess(); } /** @@ -135,16 +123,20 @@ export class EducationLevelController extends Controller { async detailEducationLevel(@Path() id: string) { const educationLevel = await this.educationLevelRepository.findOne({ where: { id }, - select: ["id", "name", "rank", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], + select: [ + "id", + "name", + "rank", + "createdAt", + "lastUpdatedAt", + "createdFullName", + "lastUpdateFullName", + ], }); if (!educationLevel) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - try { - return new HttpSuccess(educationLevel); - } catch (error) { - return error; - } + return new HttpSuccess(educationLevel); } /** @@ -156,17 +148,21 @@ export class EducationLevelController extends Controller { @Get() async listEducationLevel() { const educationLevel = await this.educationLevelRepository.find({ - select: ["id", "name","rank", "createdAt", "lastUpdatedAt", "createdFullName", "lastUpdateFullName"], + select: [ + "id", + "name", + "rank", + "createdAt", + "lastUpdatedAt", + "createdFullName", + "lastUpdateFullName", + ], order: { createdAt: "ASC" }, }); if (!educationLevel) { return new HttpSuccess([]); } - try { - return new HttpSuccess(educationLevel); - } catch (error) { - return error; - } + return new HttpSuccess(educationLevel); } } diff --git a/src/controllers/GenderController.ts b/src/controllers/GenderController.ts index 0f5ac7ef..ee1a21a9 100644 --- a/src/controllers/GenderController.ts +++ b/src/controllers/GenderController.ts @@ -47,12 +47,7 @@ export class GenderController extends Controller { if (!_gender) { return new HttpSuccess([]); } - try { - return new HttpSuccess(_gender); - } catch (error) { - console.log(error); - return error; - } + return new HttpSuccess(_gender); } /** @@ -71,11 +66,8 @@ export class GenderController extends Controller { if (!_gender) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } - try { - return new HttpSuccess(_gender); - } catch (error) { - return error; - } + + return new HttpSuccess(_gender); } /** @@ -103,16 +95,12 @@ export class GenderController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - _gender.createdUserId = request.user.sub; - _gender.createdFullName = request.user.name; - _gender.lastUpdateUserId = request.user.sub; - _gender.lastUpdateFullName = request.user.name; - await this.genderRepository.save(_gender); - return new HttpSuccess(); - } catch (error) { - return error; - } + _gender.createdUserId = request.user.sub; + _gender.createdFullName = request.user.name; + _gender.lastUpdateUserId = request.user.sub; + _gender.lastUpdateFullName = request.user.name; + await this.genderRepository.save(_gender); + return new HttpSuccess(); } /** @@ -140,15 +128,11 @@ export class GenderController extends Controller { throw new HttpError(HttpStatusCode.NOT_FOUND, "ชื่อนี้มีอยู่ในระบบแล้ว"); } - try { - _gender.lastUpdateUserId = request.user.sub; - _gender.lastUpdateFullName = request.user.name; - this.genderRepository.merge(_gender, requestBody); - await this.genderRepository.save(_gender); - return new HttpSuccess(); - } catch (error) { - return error; - } + _gender.lastUpdateUserId = request.user.sub; + _gender.lastUpdateFullName = request.user.name; + this.genderRepository.merge(_gender, requestBody); + await this.genderRepository.save(_gender); + return new HttpSuccess(); } /** @@ -166,11 +150,7 @@ export class GenderController extends Controller { if (!_delGender) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตามไอดีนี้ : " + id); } - try { - await this.genderRepository.delete(_delGender.id); - return new HttpSuccess(); - } catch (error) { - return error; - } + await this.genderRepository.delete(_delGender.id); + return new HttpSuccess(); } }