Skip to content
Merged
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
46 changes: 46 additions & 0 deletions app/api/projects/[id]/comments/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth.config";
export async function POST(request: Request) {
try {
const url = new URL(request.url);
const pathParts = url.pathname.split("/");
const projectId = pathParts[pathParts.length - 2];
const session = await getServerSession(authOptions);

if (!session || !session.user || !session.user.id) {
return NextResponse.json(
{ error: "You must be logged in to comment" },
{ status: 401 },
);
}

const userId = session.user.id;
const { content, parentId } = await request.json();
if (!content || typeof content !== "string" || content.trim().length === 0) {
return NextResponse.json({ error: "Comment content is required" }, { status: 400 });
}

// Create the comment
const comment = await prisma.comment.create({
data: {
content: content.trim(),
project: { connect: { id: projectId } },
user: { connect: { id: userId } },
parent: parentId ? { connect: { id: parentId } } : undefined,
},
include: {
user: { select: { id: true, name: true, image: true } },
reactions: true,
_count: { select: { reactions: true, replies: true } },
},
});

return NextResponse.json(comment);
} catch (error) {
console.error("Error posting comment:", error);
return NextResponse.json(
{ error: "Failed to post comment" },
{ status: 500 },
);
}
}
import prisma from "@/lib/prisma";
import { type NextRequest, NextResponse } from "next/server";

Expand Down
97 changes: 53 additions & 44 deletions app/api/projects/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,61 @@ type ProjectWithRelations = Project & {

// Only use the request parameter and extract the ID from the URL
export async function GET(request: Request) {
try {
// Extract the ID from the URL path
const url = new URL(request.url);
const pathParts = url.pathname.split("/");
const id = pathParts[pathParts.length - 1]; // Get the last segment of the path
try {
// Extract the ID from the URL path
const url = new URL(request.url);
const pathParts = url.pathname.split("/");
const id = pathParts[pathParts.length - 1]; // Get the last segment of the path

const project = (await prisma.project.findUnique({
where: { id },
include: {
user: {
select: {
id: true,
name: true,
image: true,
},
},
votes: true,
teamMembers: true,
_count: {
select: {
votes: true,
},
},
},
})) as ProjectWithRelations | null;
const project = (await prisma.project.findUnique({
where: { id },
include: {
user: {
select: {
id: true,
name: true,
image: true,
},
},
votes: true,
teamMembers: true,
_count: {
select: {
votes: true,
},
},
},
})) as ProjectWithRelations | null;

if (!project) {
return NextResponse.json({ error: "Project not found" }, { status: 404 });
}
if (!project) {
return NextResponse.json({ error: "Project not found" }, { status: 404 });
}

// Calculate team members count manually
const teamMembersCount = project.teamMembers.length;
// Calculate upvotes, downvotes, and vote percentage
const upvotes = project.votes.filter((v) => v.type === 'UPVOTE').length;
const downvotes = project.votes.filter((v) => v.type === 'DOWNVOTE').length;
const totalVotes = upvotes + downvotes;
const votePercentage = totalVotes > 0 ? Math.round((upvotes / totalVotes) * 100) : 0;

// Return the project with the manually calculated team members count
return NextResponse.json({
...project,
_count: {
...project._count,
teamMembers: teamMembersCount,
},
});
} catch (error) {
console.error("Error fetching project:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
}
// Calculate team members count manually
const teamMembersCount = project.teamMembers.length;

// Return the project with the manually calculated team members count and voting info
return NextResponse.json({
...project,
_count: {
...project._count,
teamMembers: teamMembersCount,
},
upvotes,
downvotes,
votePercentage,
});
} catch (error) {
console.error("Error fetching project:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
}
}
Loading
Loading