Skip to content

Commit

Permalink
🥭 Added delete sprint
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 12, 2024
1 parent aa963ca commit 8ccb4d5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
23 changes: 22 additions & 1 deletion src/controllers/sprintController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
};

export const deleteSprintController = async (req: Request, res: Response, next: NextFunction): Promise<void> => {
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));
}
}
14 changes: 14 additions & 0 deletions src/repositories/sprintRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class SprintRepository {
return prisma.organization.findFirst();
}

async findSprintById(sprint_id: number): Promise<Sprint | null> {
return await prisma.sprint.findUnique({
where: { sprint_id }
});
}

async getAllSprints(): Promise<Sprint[]> {
return await prisma.sprint.findMany({
include: {
Expand All @@ -30,6 +36,14 @@ class SprintRepository {
});
}

async deleteSprint(sprint_id: number, trx?: Prisma.TransactionClient): Promise<Sprint | null> {
const client = trx || prisma;

return await client.sprint.delete({
where: { sprint_id }
});
}

async transaction(callback: (trx: Prisma.TransactionClient) => Promise<any>): Promise<any> {
return prisma.$transaction(callback);
}
Expand Down
3 changes: 2 additions & 1 deletion src/routes/sprints.ts
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions src/services/sprintService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ class SprintService {

return updatedSprint;
}

async deleteSprintService(sprint_id: number): Promise<Sprint> {
const deletedSprint = await sprintRepository.transaction(async (trx: Prisma.TransactionClient) => {
return await sprintRepository.deleteSprint(sprint_id, trx);
})
return deletedSprint;
};

async checkSprintExist(sprint_id: number): Promise<boolean> {
const sprint = await sprintRepository.findSprintById(sprint_id);
return sprint !== null;
}
}

export default new SprintService();

0 comments on commit 8ccb4d5

Please sign in to comment.