feat: add comments functionality and integrate Socket.io for real-time updates
- Implemented a full comments module in the backend with repository, service, controller, and DTOs using NestJS. - Added frontend support for comments with a `CommentSection` component and integration into content pages. - Introduced `SocketProvider` on the frontend and integrated Socket.io for real-time communication. - Updated dependencies and configurations for Socket.io and WebSockets support.
This commit is contained in:
37
backend/src/comments/comments.service.ts
Normal file
37
backend/src/comments/comments.service.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import {
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { CommentsRepository } from "./repositories/comments.repository";
|
||||
import type { CreateCommentDto } from "./dto/create-comment.dto";
|
||||
|
||||
@Injectable()
|
||||
export class CommentsService {
|
||||
constructor(private readonly commentsRepository: CommentsRepository) {}
|
||||
|
||||
async create(userId: string, contentId: string, dto: CreateCommentDto) {
|
||||
return this.commentsRepository.create({
|
||||
userId,
|
||||
contentId,
|
||||
text: dto.text,
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByContentId(contentId: string) {
|
||||
return this.commentsRepository.findAllByContentId(contentId);
|
||||
}
|
||||
|
||||
async remove(userId: string, commentId: string, isAdmin = false) {
|
||||
const comment = await this.commentsRepository.findOne(commentId);
|
||||
if (!comment) {
|
||||
throw new NotFoundException("Comment not found");
|
||||
}
|
||||
|
||||
if (!isAdmin && comment.userId !== userId) {
|
||||
throw new ForbiddenException("You cannot delete this comment");
|
||||
}
|
||||
|
||||
await this.commentsRepository.delete(commentId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user