Skip to content

Commit

Permalink
+ new route, userProject, for an intermediary table for users and pro…
Browse files Browse the repository at this point in the history
…jects

+ new tests for userProject
+ updated tests to not interfere with userProject tests
  • Loading branch information
grong0 committed Nov 10, 2024
1 parent 204f0e5 commit b7fb422
Show file tree
Hide file tree
Showing 7 changed files with 976 additions and 45 deletions.
8 changes: 3 additions & 5 deletions next/app/api/project/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ const prisma = new PrismaClient();
export async function GET(request: Request, { params }: { params: { id: string } }) {
const id = parseInt(params.id);

if (typeof id == "number") {
if (typeof id != "number") {
return new Response("id must be an integer", { status: 402 });
}

console.log("id thing: " + id);

const project = await prisma.project.findUnique({
where: {
id: id,
id,
},
});

if (project === null) {
return new Response(`project of id ${id} doesn't exist`);
return new Response(`project of 'id' ${id} doesn't exist`);
}

return Response.json(project, { status: 201 });
Expand Down
26 changes: 13 additions & 13 deletions next/app/api/project/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ export async function POST(request: Request) {
return new Response("title must be a string", { status: 422 });
}
if (typeof body.description != "string") {
return new Response("description must be a string", { status: 422 });
return new Response("'description' must be a string", { status: 422 });
}

let repoLink = "";
let contentURL = "";
if ("repoLink" in body) {
if (typeof body.repoLink != "string") {
return new Response("repoLink must be a string", { status: 422 });
return new Response("'repoLink' must be a string", { status: 422 });
}
repoLink = body.repoLink;
}
if ("contentURL" in body) {
if (typeof body.contentURL != "string") {
return new Response("contentURL must be a string", { status: 422 });
return new Response("'contentURL' must be a string", { status: 422 });
}
contentURL = body.contentURL;
}
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function PUT(request: Request) {
}

if (typeof body.id != "number") {
return new Response("id must be an integer", { status: 422 });
return new Response("'id' must be an integer", { status: 422 });
}

const project_exists =
Expand All @@ -75,32 +75,32 @@ export async function PUT(request: Request) {
})) !== null;

if (!project_exists) {
return new Response(`project of id: ${body.id} doesn't exist`, { status: 404 });
return new Response(`project of 'id': ${body.id} doesn't exist`, { status: 404 });
}

const data: { title?: string; description?: string; repoLink?: string; contentURL?: string } =
{};
if ("title" in body) {
if (typeof body.title != "string") {
return new Response("title must be a string", { status: 422 });
return new Response("'title' must be a string", { status: 422 });
}
data.title = body.title;
}
if ("description" in body) {
if (typeof body.description != "string") {
return new Response("description must be a string", { status: 422 });
return new Response("'description' must be a string", { status: 422 });
}
data.description = body.description;
}
if ("repoLink" in body) {
if (typeof body.repoLink != "string") {
return new Response("repoLink must be a string", { status: 422 });
return new Response("'repoLink' must be a string", { status: 422 });
}
data.repoLink = body.repoLink;
}
if ("contentURL" in body) {
if (typeof body.contentURL != "string") {
return new Response("contentURL must be a string", { status: 422 });
return new Response("'contentURL' must be a string", { status: 422 });
}
data.contentURL = body.contentURL;
}
Expand All @@ -112,7 +112,7 @@ export async function PUT(request: Request) {
data,
});

return Response.json(project, { status: 201 });
return Response.json(project, { status: 200 });
}

export async function DELETE(request: Request) {
Expand All @@ -128,7 +128,7 @@ export async function DELETE(request: Request) {
}

if (typeof body.id != "number") {
return new Response("id must be an integer", { status: 422 });
return new Response("'id' must be an integer", { status: 422 });
}

const projectExists =
Expand All @@ -139,12 +139,12 @@ export async function DELETE(request: Request) {
})) != null;

if (!projectExists) {
return new Response(`project with id ${body.id} doesn't exist`, { status: 404 });
return new Response(`project with 'id' ${body.id} doesn't exist`, { status: 404 });
}

const project = await prisma.project.delete({
where: { id: body.id },
});

return Response.json(project, { status: 201 });
return Response.json(project, { status: 200 });
}
41 changes: 41 additions & 0 deletions next/app/api/userProject/[id]/route.ts
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 });
}
195 changes: 195 additions & 0 deletions next/app/api/userProject/route.ts
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 });
}
16 changes: 14 additions & 2 deletions next/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ model User {
officers Officer[]
account Account[]
session Session[]
projects Project[]
projects UserProject[]
}

model Quote {
Expand Down Expand Up @@ -303,5 +303,17 @@ model Project {
contentURL String?
// Relational Field
contributers User[]
contributers UserProject[]
}

model UserProject {
id Int @id @default(autoincrement())
// Foreign Keys
userId Int
projectId Int
// Relational Fields
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
}
Loading

0 comments on commit b7fb422

Please sign in to comment.