Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions prisma/migrations/20250119091721_alter_project_table/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:

- Added the required column `created_by` to the `projects` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "projects" ADD COLUMN "created_by" TEXT NOT NULL;

-- AddForeignKey
ALTER TABLE "projects" ADD CONSTRAINT "projects_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ model User {
username String @unique
bio String?
flags Flag[] // Relation to flags created by this user
projects Project[] // Relation to projects created by this user

@@map("users")
}
Expand All @@ -40,6 +41,8 @@ model Project {
name String @db.VarChar(100)
description String @db.VarChar(500)
logo String?
created_by String // Foreign key to User
created_by_user User @relation(fields: [created_by], references: [id])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
flags Flag[] // Relation to flags in this project
Expand Down
26 changes: 26 additions & 0 deletions src/controllers/projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Request, Response } from 'express';
import { IResponse } from '../types/response';
import { IProject } from '../types/projects';
import { getAllProjects } from '../models/projects';

export const getProjectsController = async (
req: Request,
res: Response,
): Promise<void> => {
try {
const projects = await getAllProjects();
const response: IResponse<IProject[]> = {
status: 200,
message: 'Projects retrieved successfully',
data: projects,
};

res.status(200).json(response);
} catch (error: unknown) {
if (error instanceof Error) {
res.status(500).json({ error: error.message });
} else {
res.status(500).json({ error: 'An unknown error occurred' });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'An unknown error occurred' lets define a standard constant and use it here?

}
}
};
30 changes: 30 additions & 0 deletions src/models/projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { PrismaClient } from '@prisma/client';
import { IProject } from '../types/projects';

export const checkProjectAccess = async (
project_id: string,
user_id: string,
): Promise<boolean> => {
const prisma = new PrismaClient();
const project = await prisma.project.findFirst({
where: {
id: project_id,
created_by: user_id,
},
});
return project !== null;
};
Comment on lines +4 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be split into two one model function and one middleware function the checking part middleware will do and model function will be only responsible for fetching the data


export const getAllProjects = async (): Promise<IProject[]> => {
const prisma = new PrismaClient();
const projects = await prisma.project.findMany();
return projects.map((project) => ({
id: project.id,
name: project.name,
description: project.description,
logo: project.logo,
created_by: project.created_by,
created_at: project.created_at.toISOString(),
updated_at: project.updated_at.toISOString(),
Comment on lines +22 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can write utils fo transforming this

}));
};
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import express from 'express';
import health from './health';
import flags from './flags';
import projects from './projects';

const router = express.Router();

export default (): express.Router => {
projects(router);
flags(router);
health(router);
return router;
Expand Down
6 changes: 6 additions & 0 deletions src/routes/projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import express from 'express';
import { getProjectsController } from '../controllers/projects';

export default (router: express.Router) => {
router.get('/projects', getProjectsController);
};
9 changes: 9 additions & 0 deletions src/types/projects.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IProject {
id: string;
name: string;
description: string;
logo: string | null;
created_by: string;
created_at: string;
updated_at: string;
}