From 329a3ef50f3be7228d0a6319944b54fb92bd2865 Mon Sep 17 00:00:00 2001 From: batuhancetin1 Date: Sun, 19 Nov 2023 23:43:29 +0300 Subject: [PATCH 1/3] bugs are fixed and codes are formatted --- app/backend/src/app.module.ts | 2 +- .../src/auth/dto/reset-password.dto.ts | 36 +-- app/backend/src/auth/guards/auth.guard.ts | 77 +++--- .../src/auth/guards/verification.guard.ts | 50 ++-- .../src/moderator/dto/create_moderator.dto.ts | 52 ++-- .../src/moderator/dto/verify_moderator.dto.ts | 33 ++- .../src/moderator/guards/moderator.guard.ts | 99 +++---- .../moderator/guards/verification.guard.ts | 50 ++-- .../src/moderator/moderator.controller.ts | 43 ++- app/backend/src/moderator/moderator.module.ts | 13 +- .../src/moderator/moderator.service.ts | 248 ++++++++++-------- .../src/option/dto/create-option.dto.ts | 30 +-- .../src/option/entities/option.entity.ts | 2 +- app/backend/src/option/option.service.ts | 5 +- app/backend/src/poll/entities/poll.entity.ts | 6 +- app/backend/src/poll/poll.controller.spec.ts | 63 +++-- app/backend/src/poll/poll.module.ts | 1 - app/backend/src/tag/dto/create-tag.dto.ts | 16 +- app/backend/src/tag/entities/tag.entity.ts | 6 +- app/backend/src/tag/tag.service.ts | 4 +- app/backend/src/user/dto/create-user.dto.ts | 8 +- app/backend/src/user/entities/user.entity.ts | 7 +- app/backend/src/user/user.controller.spec.ts | 50 ++-- app/backend/src/user/user.service.ts | 37 ++- 24 files changed, 505 insertions(+), 433 deletions(-) diff --git a/app/backend/src/app.module.ts b/app/backend/src/app.module.ts index 22e69112..f41404b1 100644 --- a/app/backend/src/app.module.ts +++ b/app/backend/src/app.module.ts @@ -23,7 +23,7 @@ import { Moderator } from './moderator/entities/moderator.entity'; port: 5432, password: 'password', username: 'postgres', - entities: [User, Poll, Tag, Option,Moderator], + entities: [User, Poll, Tag, Option, Moderator], database: 'postgres', synchronize: true, logging: true, diff --git a/app/backend/src/auth/dto/reset-password.dto.ts b/app/backend/src/auth/dto/reset-password.dto.ts index e542b70e..1c429bb1 100644 --- a/app/backend/src/auth/dto/reset-password.dto.ts +++ b/app/backend/src/auth/dto/reset-password.dto.ts @@ -2,24 +2,24 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator'; export class ResetPasswordDto { - @ApiProperty({ - example: 1234, - }) - @IsNotEmpty() - resetPasswordToken: number; + @ApiProperty({ + example: 1234, + }) + @IsNotEmpty() + resetPasswordToken: number; - @ApiProperty({ - example: 'test@gmail.com', - }) - @IsEmail() - @IsNotEmpty() - email: string; + @ApiProperty({ + example: 'test@gmail.com', + }) + @IsEmail() + @IsNotEmpty() + email: string; - @ApiProperty({ - example: '123456', - }) - @IsNotEmpty() - @IsString() - @MinLength(6) - password: string; + @ApiProperty({ + example: '123456', + }) + @IsNotEmpty() + @IsString() + @MinLength(6) + password: string; } diff --git a/app/backend/src/auth/guards/auth.guard.ts b/app/backend/src/auth/guards/auth.guard.ts index ea7d105e..82c1f17a 100644 --- a/app/backend/src/auth/guards/auth.guard.ts +++ b/app/backend/src/auth/guards/auth.guard.ts @@ -1,44 +1,41 @@ import { - CanActivate, - ExecutionContext, - Injectable, - UnauthorizedException, - } from '@nestjs/common'; - import { JwtService } from '@nestjs/jwt'; - import { Request } from 'express'; - import { UserService } from '../../user/user.service'; - - @Injectable() - export class AuthGuard implements CanActivate { - constructor( - private jwtService: JwtService, - private readonly userService: UserService, - ) {} - - async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); - const token = this.extractTokenFromHeader(request); - if (!token) { - throw new UnauthorizedException(); - } - try { - const payload = await this.jwtService.verifyAsync( - token, - { - secret: 'very-secret-key' - } - ); - const user = await this.userService.searchUser({email: payload.email}); + CanActivate, + ExecutionContext, + Injectable, + UnauthorizedException, +} from '@nestjs/common'; +import { JwtService } from '@nestjs/jwt'; +import { Request } from 'express'; +import { UserService } from '../../user/user.service'; - request['user'] = user[0]; - } catch { - throw new UnauthorizedException(); - } - return true; +@Injectable() +export class AuthGuard implements CanActivate { + constructor( + private jwtService: JwtService, + private readonly userService: UserService, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + const token = this.extractTokenFromHeader(request); + if (!token) { + throw new UnauthorizedException(); } - - private extractTokenFromHeader(request: Request): string | undefined { - const [type, token] = request.headers.authorization?.split(' ') ?? []; - return type === 'Bearer' ? token : undefined; + try { + const payload = await this.jwtService.verifyAsync(token, { + secret: 'very-secret-key', + }); + const user = await this.userService.searchUser({ email: payload.email }); + + request['user'] = user[0]; + } catch { + throw new UnauthorizedException(); } - } \ No newline at end of file + return true; + } + + private extractTokenFromHeader(request: Request): string | undefined { + const [type, token] = request.headers.authorization?.split(' ') ?? []; + return type === 'Bearer' ? token : undefined; + } +} diff --git a/app/backend/src/auth/guards/verification.guard.ts b/app/backend/src/auth/guards/verification.guard.ts index b15cadb5..b65c5e7e 100644 --- a/app/backend/src/auth/guards/verification.guard.ts +++ b/app/backend/src/auth/guards/verification.guard.ts @@ -1,29 +1,27 @@ import { - Injectable, - CanActivate, - ExecutionContext, - UnauthorizedException, - } from '@nestjs/common'; - - @Injectable() - export class VerificationGuard implements CanActivate { - constructor() {} - - public async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); - - - if (!request.user.isVerified) { - throw new UnauthorizedException( - { - message: 'User is not verified', - data: null, - errors: null, - }, - '401', - ); - } - return true; + Injectable, + CanActivate, + ExecutionContext, + UnauthorizedException, +} from '@nestjs/common'; + +@Injectable() +export class VerificationGuard implements CanActivate { + constructor() {} + + public async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + + if (!request.user.isVerified) { + throw new UnauthorizedException( + { + message: 'User is not verified', + data: null, + errors: null, + }, + '401', + ); } + return true; } - \ No newline at end of file +} diff --git a/app/backend/src/moderator/dto/create_moderator.dto.ts b/app/backend/src/moderator/dto/create_moderator.dto.ts index a573b830..ab9622e5 100644 --- a/app/backend/src/moderator/dto/create_moderator.dto.ts +++ b/app/backend/src/moderator/dto/create_moderator.dto.ts @@ -1,30 +1,28 @@ -import { ApiProperty } from "@nestjs/swagger"; -import { IsEmail, IsNotEmpty, IsString, MinLength } from "class-validator"; +import { ApiProperty } from '@nestjs/swagger'; +import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator'; +export class CreateModeratorDto { + @ApiProperty({ + uniqueItems: true, + example: 'test1@denrox.com', + }) + @IsEmail() + @IsNotEmpty() + email: string; + @ApiProperty({ + minLength: 6, + example: 'test11', + }) + @IsString() + @MinLength(6) + password: string; -export class CreateModeratorDto { - @ApiProperty({ - uniqueItems: true, - example: 'test1@denrox.com', - }) - @IsEmail() - @IsNotEmpty() - email: string; - - @ApiProperty({ - minLength: 6, - example: 'test11', - }) - @IsString() - @MinLength(6) - password: string; - - @ApiProperty({ - minLength: 6, - example: 'johnDoe', - }) - @IsString() - @MinLength(6) - username: string; - } \ No newline at end of file + @ApiProperty({ + minLength: 6, + example: 'johnDoe', + }) + @IsString() + @MinLength(6) + username: string; +} diff --git a/app/backend/src/moderator/dto/verify_moderator.dto.ts b/app/backend/src/moderator/dto/verify_moderator.dto.ts index 8f84450b..7458aa1c 100644 --- a/app/backend/src/moderator/dto/verify_moderator.dto.ts +++ b/app/backend/src/moderator/dto/verify_moderator.dto.ts @@ -1,19 +1,18 @@ -import { ApiProperty } from "@nestjs/swagger"; -import { IsEmail, IsNotEmpty, IsNumber } from "class-validator"; +import { ApiProperty } from '@nestjs/swagger'; +import { IsEmail, IsNotEmpty, IsNumber } from 'class-validator'; export class VerifyModeratorDto { - @ApiProperty({ - uniqueItems: true, - example: 'test1@denrox.com', - }) - @IsEmail() - @IsNotEmpty() - email: string; - - @ApiProperty({ - example: 1234, - }) - @IsNumber() - verificationCode: number; - } - \ No newline at end of file + @ApiProperty({ + uniqueItems: true, + example: 'test1@denrox.com', + }) + @IsEmail() + @IsNotEmpty() + email: string; + + @ApiProperty({ + example: 1234, + }) + @IsNumber() + verificationCode: number; +} diff --git a/app/backend/src/moderator/guards/moderator.guard.ts b/app/backend/src/moderator/guards/moderator.guard.ts index ba7c4bfe..ea34350c 100644 --- a/app/backend/src/moderator/guards/moderator.guard.ts +++ b/app/backend/src/moderator/guards/moderator.guard.ts @@ -1,61 +1,70 @@ import { - CanActivate, - ExecutionContext, - Injectable, - UnauthorizedException, - } from '@nestjs/common'; - import { JwtService } from '@nestjs/jwt'; - import { Request } from 'express'; + CanActivate, + ExecutionContext, + Injectable, + UnauthorizedException, +} from '@nestjs/common'; +import { JwtService } from '@nestjs/jwt'; +import { Request } from 'express'; import { ModeratorService } from '../moderator.service'; - - @Injectable() - export class ModeratorGuard implements CanActivate { - constructor( - private jwtService: JwtService, - private readonly moderatorService: ModeratorService, - ) {} - - async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); - const token = this.extractTokenFromHeader(request); - if (!token) { - throw new UnauthorizedException({ + +@Injectable() +export class ModeratorGuard implements CanActivate { + constructor( + private jwtService: JwtService, + private readonly moderatorService: ModeratorService, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + const token = this.extractTokenFromHeader(request); + if (!token) { + throw new UnauthorizedException( + { message: 'Token not found.', data: null, errors: null, }, - '401',); - } - try { - const payload = await this.jwtService.verifyAsync( - token, + '401', + ); + } + try { + const payload = await this.jwtService.verifyAsync(token, { + secret: 'very-secret-key', + }); + + const moderator = await this.moderatorService.searchModerators(true, { + where: { + email: payload.email, + }, + }); + + if (payload.userType != 1) { + throw new UnauthorizedException( { - secret: 'very-secret-key' - } - ); - const moderator = await this.moderatorService.searchModerator({email: payload.email}); - if (payload.userType != 1) { - throw new UnauthorizedException({ message: 'User is not moderator.', data: null, errors: null, }, - '401',); - } - request['moderator'] = moderator; - } catch { - throw new UnauthorizedException({ + '401', + ); + } + request['moderator'] = moderator; + } catch { + throw new UnauthorizedException( + { message: 'Token is invalid.', data: null, errors: null, }, - '401',); - } - return true; - } - - private extractTokenFromHeader(request: Request): string | undefined { - const [type, token] = request.headers.authorization?.split(' ') ?? []; - return type === 'Bearer' ? token : undefined; + '401', + ); } - } \ No newline at end of file + return true; + } + + private extractTokenFromHeader(request: Request): string | undefined { + const [type, token] = request.headers.authorization?.split(' ') ?? []; + return type === 'Bearer' ? token : undefined; + } +} diff --git a/app/backend/src/moderator/guards/verification.guard.ts b/app/backend/src/moderator/guards/verification.guard.ts index 0b7d3835..18283325 100644 --- a/app/backend/src/moderator/guards/verification.guard.ts +++ b/app/backend/src/moderator/guards/verification.guard.ts @@ -1,29 +1,27 @@ import { - Injectable, - CanActivate, - ExecutionContext, - UnauthorizedException, - } from '@nestjs/common'; - - @Injectable() - export class VerificationGuard implements CanActivate { - constructor() {} - - public async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest(); - - - if (!request.moderator.isVerified) { - throw new UnauthorizedException( - { - message: 'Moderator is not verified', - data: null, - errors: null, - }, - '401', - ); - } - return true; + Injectable, + CanActivate, + ExecutionContext, + UnauthorizedException, +} from '@nestjs/common'; + +@Injectable() +export class VerificationGuard implements CanActivate { + constructor() {} + + public async canActivate(context: ExecutionContext): Promise { + const request = context.switchToHttp().getRequest(); + + if (!request.moderator.isVerified) { + throw new UnauthorizedException( + { + message: 'Moderator is not verified', + data: null, + errors: null, + }, + '401', + ); } + return true; } - \ No newline at end of file +} diff --git a/app/backend/src/moderator/moderator.controller.ts b/app/backend/src/moderator/moderator.controller.ts index 386bf283..4c787ec5 100644 --- a/app/backend/src/moderator/moderator.controller.ts +++ b/app/backend/src/moderator/moderator.controller.ts @@ -1,9 +1,18 @@ -import { Body, Controller, Get, Post } from '@nestjs/common'; +import { + Body, + Controller, + Delete, + Get, + Param, + Post, + UseGuards, +} from '@nestjs/common'; import { ModeratorService } from './moderator.service'; import { ApiBearerAuth, ApiTags } from '@nestjs/swagger'; import { CreateModeratorDto } from './dto/create_moderator.dto'; -import { Verify } from 'crypto'; import { VerifyModeratorDto } from './dto/verify_moderator.dto'; +import { ModeratorGuard } from './guards/moderator.guard'; +import { VerificationGuard } from './guards/verification.guard'; @ApiBearerAuth() @Controller('moderator') @@ -11,16 +20,34 @@ import { VerifyModeratorDto } from './dto/verify_moderator.dto'; export class ModeratorController { constructor(private readonly moderatorService: ModeratorService) {} + @Post('register') + createModerator(@Body() createModeratorDto: CreateModeratorDto) { + return this.moderatorService.createModerator(createModeratorDto); + } + + @Post('register/verify') + verifyModerator(@Body() verifyModeratorDto: VerifyModeratorDto) { + return this.moderatorService.verifyModerator(verifyModeratorDto); + } + @Get() + findAll() { + return this.moderatorService.findAll(); + } + + @UseGuards(ModeratorGuard, VerificationGuard) + @Get('polls') fetchUnapprovedPolls() { return this.moderatorService.fetchUnapprovedPolls(); - @Post("register") - createModerator (@Body() createModeratorDto: CreateModeratorDto){ - return this.moderatorService.createModerator(createModeratorDto) } - @Post("register/verify") - verifyModerator (@Body() verifyModeratorDto: VerifyModeratorDto){ - return this.moderatorService.verifyModerator(verifyModeratorDto) + @Get(':id') + findOne(@Param('id') id: string) { + return this.moderatorService.findOneById(id); + } + + @Delete(':id') + remove(@Param('id') id: string) { + return this.moderatorService.removeById(id); } } diff --git a/app/backend/src/moderator/moderator.module.ts b/app/backend/src/moderator/moderator.module.ts index 86730398..0e227284 100644 --- a/app/backend/src/moderator/moderator.module.ts +++ b/app/backend/src/moderator/moderator.module.ts @@ -3,12 +3,19 @@ import { ModeratorService } from './moderator.service'; import { ModeratorController } from './moderator.controller'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Moderator } from './entities/moderator.entity'; +import { Poll } from '../poll/entities/poll.entity'; +import { JwtModule } from '@nestjs/jwt'; @Module({ - imports: [TypeOrmModule.forFeature([Moderator])], + imports: [ + TypeOrmModule.forFeature([Moderator, Poll]), + JwtModule.register({ + global: true, + secret: 'very-secret-key', + signOptions: { expiresIn: '10m' }, + }), + ], controllers: [ModeratorController], providers: [ModeratorService], }) export class ModeratorModule {} - - diff --git a/app/backend/src/moderator/moderator.service.ts b/app/backend/src/moderator/moderator.service.ts index 3e5db114..cf8494a5 100644 --- a/app/backend/src/moderator/moderator.service.ts +++ b/app/backend/src/moderator/moderator.service.ts @@ -1,4 +1,10 @@ -import { BadRequestException, ConflictException, Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; +import { + BadRequestException, + ConflictException, + Injectable, + NotFoundException, + UnauthorizedException, +} from '@nestjs/common'; import { CreateModeratorDto } from './dto/create_moderator.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; @@ -8,119 +14,139 @@ import { JwtService } from '@nestjs/jwt'; import { VerifyModeratorDto } from './dto/verify_moderator.dto'; import { Poll } from '../poll/entities/poll.entity'; - @Injectable() export class ModeratorService { + constructor( + @InjectRepository(Moderator) + private readonly moderatorRepository: Repository, + @InjectRepository(Poll) private readonly pollRepository: Repository, + private mailerService: MailerService, + private jwtService: JwtService, + ) {} + + public async searchModerators( + searchOne: boolean, + params, + ): Promise { + if (searchOne) { + return await this.moderatorRepository.findOne(params); + } else { + return await this.moderatorRepository.find(params); + } + } - constructor( - @InjectRepository(Moderator) private readonly moderatorRepository: Repository, - @InjectRepository(Poll) private readonly pollRepository: Repository, - private mailerService: MailerService, - private jwtService: JwtService, - ) {} - - public async searchModerators(searchOne:boolean, params): Promise { - if (searchOne){ - return await this.moderatorRepository.findOne(params); - }else{ - return await this.moderatorRepository.find(params); - } + async createModerator(createModeratorDto: CreateModeratorDto): Promise { + const moderator = await this.searchModerators(true, { + where: { + email: createModeratorDto.email, + }, + }); + + if (moderator) { + throw new ConflictException('Moderator already exists'); } - - async createModerator(createModeratorDto: CreateModeratorDto): Promise { - - const moderator = await this.searchModerators(true,{ - where:{ - email: createModeratorDto.email, - } - }); - - if (moderator) { - throw new ConflictException('Moderator already exists'); - } - - const moderatorUsername = await this.searchModerators(true,{ - where:{ - username: createModeratorDto.username, - } - }); - - if (moderatorUsername) { - throw new ConflictException('Username is already being used'); - } - const code = this.generateCode(); - - const createdModerator = this.moderatorRepository.create({ - verification_code: code, - ...createModeratorDto, - }); - - await this.moderatorRepository.save(createdModerator); - - this.mailerService - .sendMail({ - to: createModeratorDto.email, // list of receivers - from: 'esbatuhanes@gmail.com', // sender address - subject: 'Verification Code', // Subject line - text: 'Here is your verification code: ' + createdModerator.verification_code.toString(), // plaintext body - html: `Here is your verification code: ${createdModerator.verification_code.toString()}`, // HTML body content - }) - .then(() => console.log('Verification code is sent')) - .catch((err) => console.log(err)); - - const payload = { sub: createdModerator.id, email: createdModerator.email, userType: 1 }; - return { - user: createdModerator, - access_token: await this.jwtService.signAsync(payload), - }; - } - - public async verifyModerator(verifyModeratorDto: VerifyModeratorDto): Promise { - const moderator : Moderator = await this.searchModerators(true,{ - where:{ - email: verifyModeratorDto.email, - } - - }) as Moderator; - - if (!moderator) { - throw new NotFoundException('Moderator not found'); - } - if (moderator.verification_code !== verifyModeratorDto.verificationCode) { - throw new BadRequestException('Wrong verification Code'); - } - await this.moderatorRepository.update( - { email: verifyModeratorDto.email }, - { - isVerified: true, - }, - ); - } - - public async fetchUnapprovedPolls(): Promise { - return await this.pollRepository.find({ - where: { - is_approved_by_moderator: false - }, - relations: ['options', 'tags', 'creator'], - }); - } - - public async updateById(id: string, updateUserDto: any): Promise { - await this.moderatorRepository.update(id, updateUserDto); - } - - public async updatePassword(moderator: Moderator, password: string): Promise { - moderator.password = password; - await this.moderatorRepository.save(moderator); - } - - public async removeById(id: string): Promise { - await this.moderatorRepository.delete(id); - } - - public generateCode(): number { - return Math.floor(Math.random() * 9000 + 1000); - } + const moderatorUsername = await this.searchModerators(true, { + where: { + username: createModeratorDto.username, + }, + }); + + if (moderatorUsername) { + throw new ConflictException('Username is already being used'); + } + const code = this.generateCode(); + + const createdModerator = this.moderatorRepository.create({ + verification_code: code, + ...createModeratorDto, + }); + + await this.moderatorRepository.save(createdModerator); + + this.mailerService + .sendMail({ + to: createModeratorDto.email, // list of receivers + from: 'esbatuhanes@gmail.com', // sender address + subject: 'Verification Code', // Subject line + text: + 'Here is your verification code: ' + + createdModerator.verification_code.toString(), // plaintext body + html: `Here is your verification code: ${createdModerator.verification_code.toString()}`, // HTML body content + }) + .then(() => console.log('Verification code is sent')) + .catch((err) => console.log(err)); + + const payload = { + sub: createdModerator.id, + email: createdModerator.email, + userType: 1, + }; + return { + user: createdModerator, + access_token: await this.jwtService.signAsync(payload), + }; + } + + public async verifyModerator( + verifyModeratorDto: VerifyModeratorDto, + ): Promise { + const moderator: Moderator = (await this.searchModerators(true, { + where: { + email: verifyModeratorDto.email, + }, + })) as Moderator; + + if (!moderator) { + throw new NotFoundException('Moderator not found'); + } + if (moderator.verification_code !== verifyModeratorDto.verificationCode) { + throw new BadRequestException('Wrong verification Code'); + } + await this.moderatorRepository.update( + { email: verifyModeratorDto.email }, + { + isVerified: true, + }, + ); + } + + public async fetchUnapprovedPolls(): Promise { + return await this.pollRepository.find({ + where: { + is_approved_by_moderator: false, + }, + relations: ['options', 'tags', 'creator'], + }); + } + + public async findOneById(id: string): Promise { + return await this.moderatorRepository.findOne({ + where: { id }, + }); + } + + public async findAll(): Promise { + return await this.moderatorRepository.find(); + } + + public async updateById(id: string, updateUserDto: any): Promise { + await this.moderatorRepository.update(id, updateUserDto); + } + + public async updatePassword( + moderator: Moderator, + password: string, + ): Promise { + moderator.password = password; + await this.moderatorRepository.save(moderator); + } + + public async removeById(id: string): Promise { + await this.moderatorRepository.delete(id); + } + + public generateCode(): number { + return Math.floor(Math.random() * 9000 + 1000); + } } diff --git a/app/backend/src/option/dto/create-option.dto.ts b/app/backend/src/option/dto/create-option.dto.ts index 025ddd80..a2d0029a 100644 --- a/app/backend/src/option/dto/create-option.dto.ts +++ b/app/backend/src/option/dto/create-option.dto.ts @@ -1,18 +1,18 @@ -import { ApiProperty } from "@nestjs/swagger"; +import { ApiProperty } from '@nestjs/swagger'; export class CreateOptionDto { - @ApiProperty({ - example: 'answer', - description: 'The answer of the option', - format: 'string', - minLength: 1, - maxLength: 255, - }) - answer: string; - @ApiProperty({ - example: 'poll_id', - description: 'The poll id of the option', - format: 'uuid', - }) - poll_id: string; + @ApiProperty({ + example: 'answer', + description: 'The answer of the option', + format: 'string', + minLength: 1, + maxLength: 255, + }) + answer: string; + @ApiProperty({ + example: 'poll_id', + description: 'The poll id of the option', + format: 'uuid', + }) + poll_id: string; } diff --git a/app/backend/src/option/entities/option.entity.ts b/app/backend/src/option/entities/option.entity.ts index 0099d2ed..73ea566c 100644 --- a/app/backend/src/option/entities/option.entity.ts +++ b/app/backend/src/option/entities/option.entity.ts @@ -16,7 +16,7 @@ export class Option { @Column({ nullable: false }) answer: string; - @ManyToOne(() => Poll, poll => poll.options) // Establishing the many-to-one relationship + @ManyToOne(() => Poll, (poll) => poll.options) // Establishing the many-to-one relationship @JoinColumn() // Specifying the foreign key column poll: Relation; } diff --git a/app/backend/src/option/option.service.ts b/app/backend/src/option/option.service.ts index 532678ac..e36fe810 100644 --- a/app/backend/src/option/option.service.ts +++ b/app/backend/src/option/option.service.ts @@ -8,9 +8,10 @@ import { Option } from './entities/option.entity'; @Injectable() export class OptionService { constructor( - @InjectRepository(Option) private readonly optionRepository: Repository