Skip to content

Commit

Permalink
create, get and update sprint controller
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeManSu committed Jul 8, 2024
1 parent a171332 commit ca26b89
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 1 deletion.
24 changes: 24 additions & 0 deletions prisma/migrations/20240707122833_sprints/migration.sql
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;
27 changes: 26 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ model Organization {
customers Customer[]
organization_peoples Organization_People[]
Issue Issue[]
Issue Issue[]
Sprints Sprint[]
@@map("organizations")
}
Expand Down Expand Up @@ -70,6 +71,9 @@ model Issue {
organizationId Int
organization Organization @relation(fields: [organizationId], references: [organization_id])
sprintId Int?
Sprint Sprint? @relation(fields: [sprintId], references: [sprint_id])
@@map("issues")
}

Expand All @@ -85,3 +89,24 @@ enum Priority {
low
medium
}

model Sprint {
sprint_id Int @id @default(autoincrement())
name String
description String?
start_date DateTime
end_date DateTime
status SprintStatus
organizationId Int
organization Organization @relation(fields: [organizationId], references: [organization_id])
issue Issue[]
@@map("sprints")
}

enum SprintStatus {
ongoing
upcoming
completed
}
52 changes: 52 additions & 0 deletions src/controllers/sprintController.ts
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));
}
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ app.get("/", async (req: Request, res: Response) => {
});

import issueRoutes from './routes/issues';
import sprintRoutes from './routes/sprints';

app.use('/api/v1', issueRoutes);
app.use('/api/v1', sprintRoutes)
app.use(errorHandlerMiddleware);


Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/sprintInterface.ts
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';
}
37 changes: 37 additions & 0 deletions src/repositories/sprintRepository.ts
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();
10 changes: 10 additions & 0 deletions src/routes/sprints.ts
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;
54 changes: 54 additions & 0 deletions src/services/sprintService.ts
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();

0 comments on commit ca26b89

Please sign in to comment.