Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/be/478/comment #491

Merged
merged 7 commits into from
Nov 21, 2023
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
13 changes: 10 additions & 3 deletions ludos/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ import { GameController } from './controllers/game.controller';
import { GameService } from './services/game.service';
import { GameRepository } from './repositories/game.repository';
import { Game } from './entities/game.entity';
import { Comment } from './entities/comment.entity';
import { TokenDecoderMiddleware } from './middlewares/tokenDecoder.middleware';
import { ResetPasswordRepository } from './repositories/reset-password.repository';
import { CommentRepository } from './repositories/comment.repository';
import { S3Service } from './services/s3.service';
import { S3Controller } from './controllers/s3.controller';
import { CommentService } from './services/comment.service';
import { CommentController } from './controllers/comment.controller';
import { ReviewRepository } from './repositories/review.repository';
import { ReviewService } from './services/review.service';
import { ReviewController } from './controllers/review.controller';
import { Review } from './entities/review.entity';


@Module({
imports: [
ConfigModule.forRoot({
Expand All @@ -37,9 +42,9 @@ import { Review } from './entities/review.entity';
useClass: TypeOrmConfigService,
inject: [TypeOrmConfigService],
}),
TypeOrmModule.forFeature([User, Game, Review, ResetPassword]),
TypeOrmModule.forFeature([User, Game, ResetPassword, Comment, Review]),
],
controllers: [AppController, UserController, GameController, S3Controller, ReviewController],
controllers: [AppController, UserController, GameController, S3Controller, ReviewController, CommentController],
providers: [
AppService,
UserRepository,
Expand All @@ -48,8 +53,10 @@ import { Review } from './entities/review.entity';
GameService,
ResetPasswordRepository,
S3Service,
CommentRepository,
CommentService,
ReviewRepository,
ReviewService
ReviewService,
],
})
export class AppModule implements NestModule {
Expand Down
183 changes: 183 additions & 0 deletions ludos/backend/src/controllers/comment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import {
Body,
Controller,
HttpCode,
Get,
Post,
Put,
Delete,
Req,
UseGuards,
Param,
} from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiOkResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { WriteCommentDto } from '../dtos/comment/request/write-comment.dto';
import { EditCommentDto } from '../dtos/comment/request/edit-comment.dto';
import { GetCommentResponseDto } from '../dtos/comment/response/get-comment.response.dto';
import { CommentService } from '../services/comment.service';
import { AuthGuard } from '../services/guards/auth.guard';
import { AuthorizedRequest } from '../interfaces/common/authorized-request.interface';

@ApiTags('comment')
@Controller('comment')
export class CommentController {
constructor(private readonly commentService: CommentService) { }

@ApiOperation({ summary: 'Get comment details' })
@ApiOkResponse({
description: 'Comment details',
type: GetCommentResponseDto,
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Get(':commentId/info')
public async getComment(
@Req() req: AuthorizedRequest,
@Param('commentId') commentId: string,
) {
return await this.commentService.getComment(req.user.id, commentId);
}

@ApiOperation({ summary: 'Get comments of post/comment/review' })
@ApiOkResponse({
description: 'Comments of post/comment/review',
type: [GetCommentResponseDto],
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Get(':parentId')
public async getCommentDetails(
@Req() req: AuthorizedRequest,
@Param('parentId') parentId: string,
) {
return await this.commentService.getCommentsByParent(req.user.id, parentId);
}

@ApiOperation({ summary: 'Comment on a post/comment/review' })
@ApiOkResponse({
description: 'Comment',
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Post('/write-comment')
public async writeComment(
@Req() req: AuthorizedRequest,
@Body() input: WriteCommentDto,
) {
await this.commentService.writeComment(req.user.id, input);
}

@ApiOperation({ summary: 'Like a comment' })
@ApiOkResponse({
description: 'Like Comment',
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Post(':commentId/like-comment')
public async likeComment(
@Req() req: AuthorizedRequest,
@Param('commentId') commentId: string,
) {
await this.commentService.likeComment(req.user.id, commentId);
}

@ApiOperation({ summary: 'Dislike a comment' })
@ApiOkResponse({
description: 'Dislike Comment',
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Post(':commentId/dislike-comment')
public async dislikeComment(
@Req() req: AuthorizedRequest,
@Param('commentId') commentId: string,
) {
await this.commentService.dislikeComment(req.user.id, commentId);
}

@ApiOperation({ summary: 'Delete a comment' })
@ApiOkResponse({
description: 'Deleted Comment',
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Delete(':commentId/delete-comment')
public async deleteComment(
@Req() req: AuthorizedRequest,
@Param('commentId') commentId: string,
) {
console.log("comment id: ", commentId);
await this.commentService.deleteComment(req.user.id, commentId);
}

@ApiOperation({ summary: 'Edit a comment' })
@ApiOkResponse({
description: 'Edited Comment',
})
@ApiUnauthorizedResponse({
description: 'Invalid Credentials',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(200)
@ApiBearerAuth()
@UseGuards(AuthGuard)
@Put(':commentId/edit-comment')
public async editComment(
@Req() req: AuthorizedRequest,
@Param('commentId') commentId: string,
@Body() input: EditCommentDto,
) {
await this.commentService.editComment(req.user.id, commentId, input);
}
}
8 changes: 8 additions & 0 deletions ludos/backend/src/dtos/comment/request/edit-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
export class EditCommentDto {
@ApiProperty()
@IsString()
newText: string;
}

12 changes: 12 additions & 0 deletions ludos/backend/src/dtos/comment/request/write-comment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
export class WriteCommentDto {
@ApiProperty()
@IsString()
parentId: string;

@ApiProperty()
@IsString()
text: string;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ApiProperty } from '@nestjs/swagger';
import { User } from '../../../entities/user.entity';
import { IsDate, IsBoolean, IsString, IsNumber } from 'class-validator';

export class GetCommentResponseDto {
@ApiProperty({type: () => User})
author: User;

@ApiProperty()
@IsDate()
timestamp: Date;

@ApiProperty()
@IsString()
text: string;

@ApiProperty()
@IsString()
parentId: string;

@ApiProperty()
@IsBoolean()
edited: boolean;

@ApiProperty()
@IsNumber()
likeCount: number;

@ApiProperty()
@IsNumber()
dislikeCount: number;
}
38 changes: 38 additions & 0 deletions ludos/backend/src/entities/comment.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Entity,
Column,
PrimaryGeneratedColumn,
ManyToMany,
JoinTable,
ManyToOne,
} from 'typeorm';
import { User } from '../entities/user.entity';

@Entity('comments')
export class Comment {
@PrimaryGeneratedColumn('uuid')
id: string;

@ManyToOne(() => User, {eager: true})
author: User;

@Column()
timestamp: Date;

@Column()
parentId: string;

@Column()
text: string;

@ManyToMany('User', {eager: true})
@JoinTable({ name: 'comment_user_likes' })
likedUsers: User[]

@ManyToMany('User', {eager: true})
@JoinTable({ name: 'comment_user_dislikes' })
dislikedUsers: User[];

@Column({default: false})
edited: boolean;
}
39 changes: 39 additions & 0 deletions ludos/backend/src/repositories/comment.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@nestjs/common';
import { Comment } from '../entities/comment.entity';
import { Repository, DataSource } from 'typeorm';

@Injectable()
export class CommentRepository extends Repository<Comment> {
constructor(dataSource: DataSource) {
super(Comment, dataSource.createEntityManager());
}

public async createComment(input: Partial<Comment>): Promise<Comment> {
let comment = this.create(input);
await this.insert(comment);
return comment;
}

public findCommentById(id: string): Promise<Comment> {
return this.findOneBy({ id });
//return this.findOne({ where: {id}, relations: {likedUsers: true, dislikedUsers: true} });
}

public async deleteComment(commentId: string) {
console.log("delete", commentId);
let x = await this.delete({id: commentId});
console.log(x);
}

public async editComment(commentId: string, newText: string) {
let comment = await this.findCommentById(commentId);
comment.text = newText;
comment.edited = true;
await this.save(comment);
}

public findCommentsByParent(parentId: string): Promise<Comment[]> {
return this.findBy({ parentId });
}

}
Loading
Loading