From 8ccb4d5ca288e65f16473fde03bc93d14143dc01 Mon Sep 17 00:00:00 2001 From: Himanshu Sharma Date: Fri, 12 Jul 2024 18:46:41 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=AD=20Added=20delete=20sprint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/sprintController.ts | 23 ++++++++++++++++++++++- src/repositories/sprintRepository.ts | 14 ++++++++++++++ src/routes/sprints.ts | 3 ++- src/services/sprintService.ts | 12 ++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/controllers/sprintController.ts b/src/controllers/sprintController.ts index 4859d97..3a0a80b 100644 --- a/src/controllers/sprintController.ts +++ b/src/controllers/sprintController.ts @@ -49,4 +49,25 @@ export const updateSprint = async (req: Request, res: Response, next: NextFuncti } catch (error) { next(new ErrorHandlerClass("Unable to update the sprint", 500)); } -}; \ No newline at end of file +}; + +export const deleteSprintController = async (req: Request, res: Response, next: NextFunction): Promise => { + try { + const { sprint_id } = req.params; + const isSprint: boolean = await sprintService.checkSprintExist(Number(sprint_id)); + + if (!isSprint) { + return next(new ErrorHandlerClass('Sprint you are trying to delete does not exist', 404)); + } + + const deletedSprint = await sprintService.deleteSprintService(Number(sprint_id)); + + res.status(200).json({ + success: true, + message: "Sprint deleted successfully", + deletedSprint: deletedSprint + }); + } catch (error) { + next(new ErrorHandlerClass("Unable to delete the sprint", 500)); + } +} \ No newline at end of file diff --git a/src/repositories/sprintRepository.ts b/src/repositories/sprintRepository.ts index 3fcf279..495a49c 100644 --- a/src/repositories/sprintRepository.ts +++ b/src/repositories/sprintRepository.ts @@ -10,6 +10,12 @@ class SprintRepository { return prisma.organization.findFirst(); } + async findSprintById(sprint_id: number): Promise { + return await prisma.sprint.findUnique({ + where: { sprint_id } + }); + } + async getAllSprints(): Promise { return await prisma.sprint.findMany({ include: { @@ -30,6 +36,14 @@ class SprintRepository { }); } + async deleteSprint(sprint_id: number, trx?: Prisma.TransactionClient): Promise { + const client = trx || prisma; + + return await client.sprint.delete({ + where: { sprint_id } + }); + } + async transaction(callback: (trx: Prisma.TransactionClient) => Promise): Promise { return prisma.$transaction(callback); } diff --git a/src/routes/sprints.ts b/src/routes/sprints.ts index b021d3b..c5aaecb 100644 --- a/src/routes/sprints.ts +++ b/src/routes/sprints.ts @@ -1,10 +1,11 @@ import { Router } from "express"; -import { createSprint, getAllSprints, updateSprint } from "../controllers/sprintController"; +import { createSprint, deleteSprintController, getAllSprints, updateSprint } from "../controllers/sprintController"; const sprintRoutes: Router = Router(); sprintRoutes.post('/sprints/create', createSprint); sprintRoutes.get('/sprints', getAllSprints); sprintRoutes.patch('/sprints/update/:sprint_id', updateSprint) +sprintRoutes.delete('/sprints/delete/:sprint_id', deleteSprintController) export default sprintRoutes; \ No newline at end of file diff --git a/src/services/sprintService.ts b/src/services/sprintService.ts index 2438aee..628daf3 100644 --- a/src/services/sprintService.ts +++ b/src/services/sprintService.ts @@ -57,6 +57,18 @@ class SprintService { return updatedSprint; } + + async deleteSprintService(sprint_id: number): Promise { + const deletedSprint = await sprintRepository.transaction(async (trx: Prisma.TransactionClient) => { + return await sprintRepository.deleteSprint(sprint_id, trx); + }) + return deletedSprint; + }; + + async checkSprintExist(sprint_id: number): Promise { + const sprint = await sprintRepository.findSprintById(sprint_id); + return sprint !== null; + } } export default new SprintService(); \ No newline at end of file