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/482/BE/review-entity-and-create-flow #490

Merged
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
10 changes: 8 additions & 2 deletions ludos/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { TokenDecoderMiddleware } from './middlewares/tokenDecoder.middleware';
import { ResetPasswordRepository } from './repositories/reset-password.repository';
import { S3Service } from './services/s3.service';
import { S3Controller } from './controllers/s3.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: [
Expand All @@ -33,9 +37,9 @@ import { S3Controller } from './controllers/s3.controller';
useClass: TypeOrmConfigService,
inject: [TypeOrmConfigService],
}),
TypeOrmModule.forFeature([User, Game, ResetPassword]),
TypeOrmModule.forFeature([User, Game, Review, ResetPassword]),
],
controllers: [AppController, UserController, GameController, S3Controller],
controllers: [AppController, UserController, GameController, S3Controller, ReviewController],
providers: [
AppService,
UserRepository,
Expand All @@ -44,6 +48,8 @@ import { S3Controller } from './controllers/s3.controller';
GameService,
ResetPasswordRepository,
S3Service,
ReviewRepository,
ReviewService
],
})
export class AppModule implements NestModule {
Expand Down
106 changes: 106 additions & 0 deletions ludos/backend/src/controllers/review.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import {
Body,
Controller,
HttpCode,
Delete,
Param,
Post,
Put,
Req,
UseGuards,
} from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiConflictResponse,
ApiCreatedResponse,
ApiNotFoundResponse,
ApiTags,
} from '@nestjs/swagger';
import { ReviewCreateDto } from '../dtos/review/request/create.dto';
import { Review } from '../entities/review.entity';
import { AuthGuard } from '../services/guards/auth.guard';
import { AuthorizedRequest } from '../interfaces/common/authorized-request.interface';
import { ReviewService } from '../services/review.service';
import { ReviewEditDto } from '../dtos/review/request/edit.dto';

@ApiBearerAuth()
@UseGuards(AuthGuard)
@ApiTags('review')
@Controller('review')
export class ReviewController {
constructor(private readonly reviewService: ReviewService) {}

@ApiCreatedResponse({
description: 'Review created successfully',
type: Review,
})
@ApiConflictResponse({
description: 'Conflict in creating the review',
})
@ApiBadRequestResponse({
description: 'Bad Request',
})
@HttpCode(201)
@Post(':gameId')
public async createReview(
@Req() req: AuthorizedRequest,
@Param('gameId') gameId: string,
@Body() reviewCreateDto: ReviewCreateDto,
) {
const createdReview = await this.reviewService.createReview(
req.user.id,
gameId,
reviewCreateDto,
);
return createdReview;
}

@HttpCode(200)
@Post(':reviewId/like')
public async likeReview(
@Req() req: AuthorizedRequest,
@Param('reviewId') reviewId: string,
) {
await this.reviewService.likeReview(req.user.id, reviewId);
return { message: 'Review liked successfully.' };
}

@HttpCode(200)
@Post(':reviewId/dislike')
public async dislikeReview(
@Req() req: AuthorizedRequest,
@Param('reviewId') reviewId: string,
) {
await this.reviewService.dislikeReview(req.user.id, reviewId);
return { message: 'Review disliked successfully.' };
}

@ApiNotFoundResponse({ description: 'Review is not found!' })
@HttpCode(204)
@Delete(':reviewId')
public async deleteReview(
@Req() req: AuthorizedRequest,
@Param('reviewId') reviewId: string,
) {
await this.reviewService.deleteReview(req.user.id, reviewId);
return { message: 'Review deleted successfully.'};
}

@HttpCode(200)
@Put(':reviewId')
public async editReview(
@Req() req: AuthorizedRequest,
@Param('reviewId') reviewId: string,
@Body() reviewEditDto: ReviewEditDto,
) {
const editedReview = await this.reviewService.editReview(
req.user.id,
reviewId,
reviewEditDto,
);
return editedReview;
}

}

21 changes: 21 additions & 0 deletions ludos/backend/src/dtos/game/response/get.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';

export class GameGetResponseDto {
@ApiProperty()
id: string;

@ApiProperty()
title: string;

@ApiProperty()
coverLink: string;

@ApiProperty()
gameBio: string;

@ApiProperty()
releaseDate: Date;

@ApiProperty()
developer: string;
}
20 changes: 20 additions & 0 deletions ludos/backend/src/dtos/review/request/create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, IsNumber, Min, Max } from 'class-validator';

export class ReviewCreateDto {
@ApiProperty({
example: 'This game is amazing!',
})
@IsNotEmpty()
@IsString()
content: string;

@ApiProperty({
example: 4.5,
})
@IsNotEmpty()
@IsNumber()
@Min(0.0)
@Max(5.0)
rating: number;
}
22 changes: 22 additions & 0 deletions ludos/backend/src/dtos/review/request/edit.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsOptional, IsString, IsNumber, Min, Max} from 'class-validator';

export class ReviewEditDto {
@ApiProperty({
example: 'This game is amazing!',
})

@IsOptional()
@IsString()
content: string;

@ApiProperty({
example: 4.5,
})

@IsOptional()
@IsNumber()
@Min(0.0)
@Max(5.0)
rating: number;
}
21 changes: 21 additions & 0 deletions ludos/backend/src/dtos/review/response/create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ApiProperty } from '@nestjs/swagger';

export class ReviewCreateResponseDto {
@ApiProperty()
id: string;

@ApiProperty()
content: string;

@ApiProperty()
rating: number;

@ApiProperty()
createdAt: Date;

@ApiProperty()
userId: string;

@ApiProperty()
gameId: string;
}
15 changes: 15 additions & 0 deletions ludos/backend/src/dtos/review/response/edit.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';

export class ReviewEditResponseDto {
@ApiProperty()
id: string;

@ApiProperty()
content?: string;

@ApiProperty()
rating?: number;

@ApiProperty()
updatedAt: Date;
}
6 changes: 4 additions & 2 deletions ludos/backend/src/entities/game.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {
Entity,
JoinTable,
ManyToMany,
OneToMany,
PrimaryGeneratedColumn,
VirtualColumn,
} from 'typeorm';
import { User } from './user.entity';
import { Review } from './review.entity';

@Entity('games')
export class Game {
Expand Down Expand Up @@ -93,8 +95,8 @@ export class Game {
@Column('text')
trivia: string;

@Column('text', { array: true, default: '{}' })
reviews: string[];
@OneToMany('Review', 'game')
reviews: Review[];

@ManyToMany(() => User, (user) => user.followedGames)
@JoinTable({ name: 'game_user_follows' })
Expand Down
45 changes: 45 additions & 0 deletions ludos/backend/src/entities/review.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Column,
Entity,
ManyToOne,
PrimaryGeneratedColumn,
CreateDateColumn,
ManyToMany,
JoinTable,
UpdateDateColumn,
} from 'typeorm';
import { Game } from './game.entity';
import { User } from './user.entity';

@Entity('reviews')
export class Review {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column('text')
content: string;

@Column('float')
rating: number;

@ManyToOne(() => Game, game => game.reviews, { cascade: true })
game: Game;

@ManyToOne(() => User, user => user.reviews, { cascade: true })
user: User;

@ManyToMany('User', 'likedReviews')
@JoinTable({ name: 'review_user_likes' })
likedUsers: User[]

@ManyToMany('User', 'dislikedReviews')
@JoinTable({ name: 'review_user_dislikes' })
dislikedUsers: User[];

@CreateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP(6)' })
createdAt: Date;

@UpdateDateColumn({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP(6)', onUpdate: 'CURRENT_TIMESTAMP(6)' })
updatedAt: Date;
}

11 changes: 11 additions & 0 deletions ludos/backend/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {
PrimaryGeneratedColumn,
BeforeInsert,
BeforeUpdate,
OneToMany,
Index,
ManyToMany,
} from 'typeorm';
import * as bcrypt from 'bcrypt';
import { Game } from './game.entity';
import { UserType } from '../enums/user-type.enum';
import { Review } from './review.entity';
@Entity('users')
export class User {
@PrimaryGeneratedColumn('uuid')
Expand Down Expand Up @@ -46,6 +48,15 @@ export class User {
@Column({ nullable: true })
steamUrl: string;

@OneToMany('Review', 'user')
reviews: Review[];

@ManyToMany('Review', 'likedUsers')
likedReviews: Review[];

@ManyToMany('Review', 'user')
dislikedReviews: Review[];

@BeforeInsert()
@BeforeUpdate()
async hashPasswordBeforeInsertAndUpdate() {
Expand Down
38 changes: 38 additions & 0 deletions ludos/backend/src/repositories/review.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Injectable } from '@nestjs/common';
import { DataSource, Repository } from 'typeorm';
import { Review } from '../entities/review.entity';

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

public async createReview(input: Partial<Review>): Promise<Review> {
const review = this.create(input);
await this.insert(review);
return review;
}

public findReviewById(id: string): Promise<Review> {
return this.findOneBy({ id });
}

public findReviewByIdWithLikedUsers(id: string): Promise<Review> {
return this.findOne({ where: { id }, relations: ['likedUsers'] });
}

public findReviewByIdWithDislikedUsers(id: string): Promise<Review> {
return this.findOne({ where: { id }, relations: ['dislikedUsers'] });
}

public async updateReview(input: Partial<Review>): Promise<void> {
const review = this.create(input);
await this.save(review);
}

public async deleteReview(id: string): Promise<void> {
await this.delete(id);
}

}
Loading
Loading