-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ new route, userProject, for an intermediary table for users and pro…
…jects + new tests for userProject + updated tests to not interfere with userProject tests
- Loading branch information
Showing
7 changed files
with
976 additions
and
45 deletions.
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
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,41 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
const prisma = new PrismaClient(); | ||
|
||
export async function GET(request: Request, { params }: { params: { id: string } }) { | ||
const id = parseInt(params.id); | ||
|
||
if (typeof id != "number") { | ||
return new Response("id must be an integer", { status: 402 }); | ||
} | ||
|
||
const userProject = await prisma.userProject.findUnique({ | ||
where: { | ||
id, | ||
}, | ||
select: { | ||
id: true, | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
project: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
repoLink: true, | ||
contentURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
if (userProject === null) { | ||
return new Response(`project of 'id' ${id} doesn't exist`) | ||
} | ||
|
||
return Response.json(userProject, { status: 200 }); | ||
} |
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,195 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
const prisma = new PrismaClient(); | ||
|
||
export async function GET(request: Request) { | ||
const userProjects = await prisma.userProject.findMany({ | ||
select: { | ||
id: true, | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
project: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
repoLink: true, | ||
contentURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return Response.json(userProjects, { status: 200 }); | ||
} | ||
|
||
export async function POST(request: Request) { | ||
let body: { userId: number; projectId: number }; | ||
try { | ||
body = await request.json(); | ||
} catch { | ||
return new Response("Invalid JSON", { status: 422 }); | ||
} | ||
|
||
if (!("userId" in body) || !("projectId" in body)) { | ||
return new Response("'userId' and 'projectId' must be included in the body", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
if (typeof body.userId != "number") { | ||
return new Response("'userId' must be a number", { status: 422 }); | ||
} | ||
if (typeof body.projectId != "number") { | ||
return new Response("'projectId' must be a number", { status: 422 }); | ||
} | ||
|
||
const userProject = await prisma.userProject.create({ | ||
data: { | ||
userId: body.userId, | ||
projectId: body.projectId, | ||
}, | ||
select: { | ||
id: true, | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
project: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
repoLink: true, | ||
contentURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return Response.json(userProject, { status: 201 }); | ||
} | ||
|
||
export async function PUT(request: Request) { | ||
let body: { id: number; userId?: number; projectId?: number }; | ||
try { | ||
body = await request.json(); | ||
} catch { | ||
return new Response("Invalid JSON", { status: 422 }); | ||
} | ||
|
||
if (!("id" in body)) { | ||
return new Response("'id' must be included in the body", { | ||
status: 400, | ||
}); | ||
} | ||
if (typeof body.id != "number") { | ||
return new Response("'id' must be a number", { status: 422 }); | ||
} | ||
|
||
const userProjectExists = | ||
(await prisma.userProject.findUnique({ | ||
where: { | ||
id: body.id, | ||
}, | ||
})) != null; | ||
if (!userProjectExists) { | ||
return new Response(`userProject with 'id' ${body.id} doesn't exist`, { status: 404 }); | ||
} | ||
|
||
let data: { userId?: number; projectId?: number } = {}; | ||
if ("userId" in body) { | ||
if (typeof body.userId != "number") { | ||
return new Response("'userId' must be a number", { status: 422 }); | ||
} | ||
data.userId = body.userId; | ||
} | ||
if ("projectId" in body) { | ||
if (typeof body.projectId != "number") { | ||
return new Response("'projectId' must be a number", { status: 422 }); | ||
} | ||
data.projectId = body.projectId; | ||
} | ||
|
||
const userProject = await prisma.userProject.update({ | ||
where: { | ||
id: body.id, | ||
}, | ||
data, | ||
select: { | ||
id: true, | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
project: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
repoLink: true, | ||
contentURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return Response.json(userProject, { status: 200 }); | ||
} | ||
|
||
export async function DELETE(request: Request) { | ||
let body: { id: number }; | ||
try { | ||
body = await request.json(); | ||
} catch { | ||
return new Response("Invalid JSON", { status: 422 }); | ||
} | ||
|
||
if (!("id" in body)) { | ||
return new Response("'id' must be included in the body"); | ||
} | ||
|
||
const userProjectExists = | ||
(await prisma.userProject.findUnique({ | ||
where: { | ||
id: body.id, | ||
}, | ||
})) != null; | ||
|
||
if (!userProjectExists) { | ||
return new Response(`userProject with 'id' ${body.id} doesn't exist`); | ||
} | ||
|
||
const userProject = await prisma.userProject.delete({ | ||
where: { | ||
id: body.id, | ||
}, | ||
select: { | ||
id: true, | ||
user: { | ||
select: { | ||
id: true, | ||
name: true, | ||
email: true, | ||
}, | ||
}, | ||
project: { | ||
select: { | ||
id: true, | ||
title: true, | ||
description: true, | ||
repoLink: true, | ||
contentURL: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
return Response.json(userProject, { status: 200 }); | ||
} |
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
Oops, something went wrong.