-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create, get and update sprint controller
- Loading branch information
Showing
8 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- CreateEnum | ||
CREATE TYPE "SprintStatus" AS ENUM ('ongoing', 'upcoming', 'completed'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "issues" ADD COLUMN "sprintId" INTEGER; | ||
|
||
-- CreateTable | ||
CREATE TABLE "sprints" ( | ||
"sprint_id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"description" TEXT, | ||
"start_date" TIMESTAMP(3) NOT NULL, | ||
"end_date" TIMESTAMP(3) NOT NULL, | ||
"status" "SprintStatus" NOT NULL, | ||
"organizationId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "sprints_pkey" PRIMARY KEY ("sprint_id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "issues" ADD CONSTRAINT "issues_sprintId_fkey" FOREIGN KEY ("sprintId") REFERENCES "sprints"("sprint_id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "sprints" ADD CONSTRAINT "sprints_organizationId_fkey" FOREIGN KEY ("organizationId") REFERENCES "organizations"("organization_id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import sprintService from "../services/sprintService"; | ||
import ErrorHandlerClass from "../utils/errorClass"; | ||
import { SprintBodyInput } from "../interfaces/sprintInterface"; | ||
|
||
export const createSprint = async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
|
||
try { | ||
|
||
const newSprint = await sprintService.createSprint(req.body); | ||
|
||
res.status(201).json({ | ||
success: true, | ||
message: "New sprint created", | ||
sprint: newSprint, | ||
}); | ||
|
||
} catch (error) { | ||
next(new ErrorHandlerClass("Unable to create issue", 500)); | ||
} | ||
}; | ||
|
||
export const getAllSprints = async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
try { | ||
const allSprints = await sprintService.getAllSprints(); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
message: "All sprints fetched", | ||
sprints: allSprints | ||
}); | ||
} catch (error) { | ||
next(new ErrorHandlerClass("Failed to fetch sprints", 500)); | ||
} | ||
}; | ||
|
||
export const updateSprint = async (req: Request, res: Response, next: NextFunction): Promise<void> => { | ||
try { | ||
const { sprint_id } = req.params; | ||
const updateData: Partial<SprintBodyInput> = req.body; | ||
|
||
const updatedSprint = await sprintService.updateSprint(Number(sprint_id), updateData); | ||
|
||
res.status(200).json({ | ||
success: true, | ||
message: "Sprint updated successfully", | ||
sprint: updatedSprint, | ||
}); | ||
} catch (error) { | ||
next(new ErrorHandlerClass("Unable to update the sprint", 500)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface SprintBodyInput { | ||
name: string; | ||
description?: string; | ||
start_date: Date; | ||
end_date: Date; | ||
status: 'upcoming' | 'ongoing' | 'completed'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Organization, Prisma, Sprint } from "@prisma/client"; | ||
import prisma from "../config/primsa-client"; | ||
|
||
|
||
|
||
class SprintRepository { | ||
async createSprint(data: Prisma.SprintCreateInput): Promise<Sprint> { | ||
return await prisma.sprint.create({ data }); | ||
} | ||
|
||
async findOrganization(): Promise<Organization | null> { | ||
return prisma.organization.findFirst(); | ||
} | ||
|
||
async getAllSprints(): Promise<Sprint[]> { | ||
return await prisma.sprint.findMany({ | ||
include: { | ||
organization: true, | ||
}, | ||
}); | ||
} | ||
|
||
async updateSprint(sprint_id: number, data: Partial<Sprint>): Promise<Sprint | null> { | ||
return await prisma.sprint.update({ | ||
where: { sprint_id }, | ||
data, | ||
}); | ||
} | ||
|
||
|
||
async transaction(callback: (trx: Prisma.TransactionClient) => Promise<any>): Promise<any> { | ||
return prisma.$transaction(callback); | ||
} | ||
} | ||
|
||
|
||
export default new SprintRepository(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Router } from "express"; | ||
import { createSprint, 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) | ||
|
||
export default sprintRoutes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Prisma, Sprint } from "@prisma/client"; | ||
import sprintRepository from "../repositories/sprintRepository"; | ||
import { SprintBodyInput } from "../interfaces/sprintInterface"; | ||
import ErrorHandlerClass from "../utils/errorClass"; | ||
|
||
|
||
class SprintService { | ||
async createSprint(sprintData: SprintBodyInput): Promise<Sprint> { | ||
const { name, description, start_date, end_date, status } = sprintData; | ||
|
||
const organization = await sprintRepository.findOrganization(); | ||
|
||
if (!organization) { | ||
throw new ErrorHandlerClass("Organization Not found", 404); | ||
}; | ||
|
||
const newSprint = await sprintRepository.transaction(async (trx: Prisma.TransactionClient) => { | ||
|
||
const sprint = await trx.sprint.create({ | ||
data: { | ||
name, | ||
description, | ||
start_date, | ||
end_date, | ||
status, | ||
organization: { connect: { organization_id: organization.organization_id } } | ||
}, | ||
include: { | ||
organization: true | ||
} | ||
}); | ||
|
||
return sprint; | ||
}) | ||
return newSprint; | ||
}; | ||
|
||
async getAllSprints(): Promise<Sprint[]> { | ||
|
||
return await sprintRepository.getAllSprints(); | ||
} | ||
|
||
async updateSprint(sprint_id: number, sprintData: Partial<SprintBodyInput>): Promise<Sprint> { | ||
const updatedSprint = await sprintRepository.updateSprint(sprint_id, sprintData); | ||
|
||
if (!updatedSprint) { | ||
throw new ErrorHandlerClass("Sprint Not Found", 404); | ||
} | ||
|
||
return updatedSprint; | ||
} | ||
} | ||
|
||
export default new SprintService(); |