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
3 changes: 3 additions & 0 deletions backend/src/controllers/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Response, Request } from "express";
import type { AuthenticatedRequest } from "../middleware/auth";
import { prisma } from "../services/db";
import { randomUUID } from "crypto";

/**
* GET /api/comment/:id
Expand Down Expand Up @@ -141,6 +142,7 @@ export const createComment = async (req: AuthenticatedRequest, res: Response) =>
try {
const newComment = await prisma.comment.create({
data: {
id: randomUUID(),
userId: req.user.id,
postId: postId ?? null,
parentId: parentId ?? null,
Expand Down Expand Up @@ -349,6 +351,7 @@ export const toggleCommentLike = async (req: AuthenticatedRequest, res: Response
// Like - add a new like
await prisma.commentLike.create({
data: {
id: randomUUID(),
commentId,
userId: req.user.id,
},
Expand Down
50 changes: 37 additions & 13 deletions backend/src/controllers/post.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response } from "express";
import { prisma } from "../services/db.js";
import { Prisma } from "@prisma/client";
import { randomUUID } from "crypto";

// CREATE POST
export const createPost = async (req: Request, res: Response) => {
Expand Down Expand Up @@ -53,18 +54,19 @@ export const createPost = async (req: Request, res: Response) => {
}
}

const newPost = await prisma.post.create({
data: {
userId,
movieId,
content,
type: type || "SHORT",
stars: stars ? parseInt(stars, 10) : null,
spoiler: spoiler || false,
tags: tags || [],
imageUrls: imageUrls || [],
repostedPostId,
},
const newPost = await prisma.post.create({
data: {
id: randomUUID(),
userId,
movieId,
content,
type: type || "SHORT",
stars: stars ? parseInt(stars, 10) : null,
spoiler: spoiler || false,
tags: tags || [],
imageUrls: imageUrls || [],
repostedPostId,
},
include: {
UserProfile: {
select: {
Expand Down Expand Up @@ -99,6 +101,7 @@ export const createPost = async (req: Request, res: Response) => {
export const getPostById = async (req: Request, res: Response) => {
try {
const { postId } = req.params;
const { currentUserId } = req.query;

if (!postId) {
return res.status(400).json({ message: "Post ID is required" });
Expand Down Expand Up @@ -133,7 +136,12 @@ export const getPostById = async (req: Request, res: Response) => {
createdAt: "desc",
},
},
PostReaction: true,
PostReaction: {
select: {
reactionType: true,
userId: true,
},
},
other_Post: {
include: {
UserProfile: {
Expand All @@ -153,13 +161,28 @@ export const getPostById = async (req: Request, res: Response) => {
if (!post) {
return res.status(404).json({ message: "Post not found" });
}

// Count reactions by type
const reactionCounts = post.PostReaction.reduce((acc: Record<string, number>, r: any) => {
acc[r.reactionType] = (acc[r.reactionType] || 0) + 1;
return acc;
}, {});

// Get current user's reactions if provided
const userReactions = currentUserId
? post.PostReaction
.filter(r => r.userId === currentUserId)
.map(r => r.reactionType)
: [];

res.json({
message: "Post found successfully",
data: {
...post,
Reposts: post.other_Post,
reactionCount: post.PostReaction.length,
reactionCounts,
userReactions,
commentCount: post.Comment.length,
repostCount: post.other_Post.length,
},
Expand Down Expand Up @@ -438,6 +461,7 @@ export const toggleReaction = async (req: Request, res: Response) => {
// Add reaction
await prisma.postReaction.create({
data: {
id: randomUUID(),
postId,
userId,
reactionType,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/controllers/ratings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Response,Request } from 'express';
import type { AuthenticatedRequest } from '../middleware/auth';
import { prisma } from '../services/db';
import { randomUUID } from 'crypto';

export const createRating = async (req: AuthenticatedRequest, res: Response) => {
const timestamp = new Date().toISOString();
Expand All @@ -13,6 +14,7 @@ export const createRating = async (req: AuthenticatedRequest, res: Response) =>
return res.status(400).json({ message: 'Stars must be between 0 and 5', timestamp, endpoint: '/api/ratings' });
}
const newRatingData = {
id: randomUUID(),
userId: req.user.id,
movieId: req.body.movieId,
stars,
Expand Down
4 changes: 2 additions & 2 deletions backend/src/tests/unit/post-reactions.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ describe("Post Reactions Controller Unit Tests", () => {
await toggleReaction(mockRequest as Request, mockResponse as Response);

expect(prisma.postReaction.create).toHaveBeenCalledWith({
data: {
data: expect.objectContaining({
postId: mockPostId,
userId: mockUserId,
reactionType,
},
}),
});
expect(responseObject.json).toHaveBeenCalledWith({
message: "Reaction added successfully",
Expand Down
1 change: 1 addition & 0 deletions backend/src/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export type GetMovieSummaryEnvelope = {
};

export type PostFormData = {
userId: string;
movieId: string;
content: string;
type: 'SHORT' | 'LONG';
Expand Down
Loading