-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add api to get all projects #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; |
| 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' }); | ||
| } | ||
| } | ||
| }; | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can write utils fo transforming this |
||
| })); | ||
| }; | ||
| 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); | ||
| }; |
| 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; | ||
| } |
There was a problem hiding this comment.
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?