Skip to content

Commit

Permalink
Merge pull request #657 from bounswe/be-617/searching-of-tags-refactor
Browse files Browse the repository at this point in the history
tags are lowercase
  • Loading branch information
batuhancetin authored Dec 21, 2023
2 parents eda83cc + 90d9a38 commit 54f62e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/backend/src/poll/dto/create-poll.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { ArrayNotEmpty, IsArray, IsLowercase, IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class CreatePollDto {
@ApiProperty({
Expand All @@ -21,12 +21,15 @@ export class CreatePollDto {
example: ['soccer', 'champions league'],
})
@IsArray()
@IsLowercase({ each: true })
@ArrayNotEmpty()
tags: Array<string>;

@ApiProperty({
example: ['Fenerbahçe', 'Galatasaray', 'Beşiktaş', 'Another Team'],
})
@IsArray()
@ArrayNotEmpty()
options: Array<string>;

@ApiProperty({
Expand Down
11 changes: 10 additions & 1 deletion app/backend/src/tag/tag.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { CreateTagDto } from './dto/create-tag.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Tag } from './entities/tag.entity';
Expand All @@ -11,6 +11,10 @@ export class TagService {
) {}

public async create(createTagDto: CreateTagDto): Promise<Tag> {
createTagDto.name = createTagDto.name.toLowerCase();
if (await this.tagRepository.findOneBy({ name: createTagDto.name })) {
throw new BadRequestException('Tag must be unique.');
}
const createdTag = this.tagRepository.create(createTagDto);
return await this.tagRepository.save(createdTag);
}
Expand Down Expand Up @@ -52,4 +56,9 @@ export class TagService {

return tags;
}

public async removeAll(): Promise<void> {
await this.tagRepository.delete({});
}

}

0 comments on commit 54f62e4

Please sign in to comment.