Skip to content

Commit

Permalink
Merge pull request #51 from connectattoo/feature/define-user-tags-limit
Browse files Browse the repository at this point in the history
CT-24/CT-165
  • Loading branch information
natanaelsc authored Jun 25, 2024
2 parents d1024d4 + 72817de commit ac32f8c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/modules/profile/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Controller,
Get,
Patch,
Post,
Put,
Delete,
Req,
Expand All @@ -15,12 +14,12 @@ import { ISignedRequest } from '../auth/interfaces/signed-request.interface';
import { IMeProfile } from './interface/me.interface';
import { IUpdateProfile } from './interface/update-profile.interface';
import { ArrayDuplicatedIndexPipe } from '../../shared/utils/array-duplicated-index.util';
import { ArrayLengthPipe } from '../../shared/utils/array-length.util';
import { IGetTags } from '../tag/interface/get-tags.interface';
import { FileInterceptor } from '@nestjs/platform-express';
import { UpdateImageDTO } from './dto/update-image.dto';
import { PatchProfileDTO } from './dto/patch-profile.dto';
import { IPatchProfile } from './interface/patch-profile.interface';
import { TagArrayLengthPipe } from '../tag/pipes/tag-array-length.pipe';

@Controller('profile')
export class ProfileController {
Expand All @@ -31,15 +30,16 @@ export class ProfileController {
return await this.profileService.me(req.user.profileId);
}

@Get('tags')
@Get('/me/tags')
async getTags(@Req() req: ISignedRequest): Promise<IGetTags[]> {
return await this.profileService.getTags(req.user.profileId);
}

@Post('tags')
@Patch('/me/tags')
async setTags(
@Req() req: ISignedRequest,
@Body(new ArrayLengthPipe(1, 5), ArrayDuplicatedIndexPipe) tags: string[],
@Body(new TagArrayLengthPipe(5, 5), ArrayDuplicatedIndexPipe)
tags: string[],
): Promise<void> {
await this.profileService.setTags(req.user.profileId, tags);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/profile/profile.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class ProfileRepository {
return await this.prismaService.profile.update({
where: { id: profileId },
data: {
tags: { connect: tags.map((tag) => ({ id: tag })) },
tags: { set: tags.map((tag) => ({ id: tag })) },
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ConflictException, NotFoundException } from '@nestjs/common';
import {
BadRequestException,
ConflictException,
NotFoundException,
} from '@nestjs/common';

export class TagBusinessExceptions {
static tagNotFoundException() {
Expand All @@ -12,4 +16,10 @@ export class TagBusinessExceptions {
static tagAlreadyExistsException() {
throw new ConflictException('Tag já cadastrado.');
}

static tagsLengthIncorrectException() {
throw new BadRequestException(
'A quantidade de tags não deve ser superior ou inferior a 5.',
);
}
}
12 changes: 12 additions & 0 deletions src/modules/tag/pipes/tag-array-length.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ArrayLengthPipe } from '~/shared/utils/array-length.util';
import { TagBusinessExceptions } from '../exceptions/tags-business.exceptions';

export class TagArrayLengthPipe extends ArrayLengthPipe {
constructor(min: number, max: number) {
super(min, max);
}

protected arrayLengthValidation(): void {
throw TagBusinessExceptions.tagsLengthIncorrectException();
}
}
2 changes: 1 addition & 1 deletion src/modules/tag/tag.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { TagRepository } from './tag.repository';
import { TagBusinessExceptions } from './exceptions/profile-business.exceptions';
import { TagBusinessExceptions } from './exceptions/tags-business.exceptions';
import { IGetTags } from './interface/get-tags.interface';

@Injectable()
Expand Down
13 changes: 10 additions & 3 deletions src/shared/utils/array-length.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ export class ArrayLengthPipe implements PipeTransform {
) {}

transform(value: string[]) {
if (!Array.isArray(value))
throw GenericValidationsExceptions.NotIsArrayException();
if (!Array.isArray(value)) this.notIsArrayValidation();

if (value.length > this.max || value.length < this.min)
throw GenericValidationsExceptions.ArrayLengthException();
this.arrayLengthValidation();

return value;
}

protected notIsArrayValidation() {
throw GenericValidationsExceptions.NotIsArrayException();
}

protected arrayLengthValidation() {
throw GenericValidationsExceptions.ArrayLengthException();
}
}

0 comments on commit ac32f8c

Please sign in to comment.