diff --git a/prisma/migrations/20250119091721_alter_project_table/migration.sql b/prisma/migrations/20250119091721_alter_project_table/migration.sql new file mode 100644 index 0000000..66f45aa --- /dev/null +++ b/prisma/migrations/20250119091721_alter_project_table/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index b8ebbd9..8c37be3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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") } @@ -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 diff --git a/src/controllers/projects.ts b/src/controllers/projects.ts new file mode 100644 index 0000000..aa6316a --- /dev/null +++ b/src/controllers/projects.ts @@ -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 => { + try { + const projects = await getAllProjects(); + const response: IResponse = { + 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' }); + } + } +}; diff --git a/src/models/projects.ts b/src/models/projects.ts new file mode 100644 index 0000000..8584437 --- /dev/null +++ b/src/models/projects.ts @@ -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 => { + const prisma = new PrismaClient(); + const project = await prisma.project.findFirst({ + where: { + id: project_id, + created_by: user_id, + }, + }); + return project !== null; +}; + +export const getAllProjects = async (): Promise => { + 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(), + })); +}; diff --git a/src/routes/index.ts b/src/routes/index.ts index 612396a..1ec4af3 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -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; diff --git a/src/routes/projects.ts b/src/routes/projects.ts new file mode 100644 index 0000000..6b8aeeb --- /dev/null +++ b/src/routes/projects.ts @@ -0,0 +1,6 @@ +import express from 'express'; +import { getProjectsController } from '../controllers/projects'; + +export default (router: express.Router) => { + router.get('/projects', getProjectsController); +}; diff --git a/src/types/projects.d.ts b/src/types/projects.d.ts new file mode 100644 index 0000000..bae9742 --- /dev/null +++ b/src/types/projects.d.ts @@ -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; +}