Skip to content

Commit

Permalink
feat(tag): bookInfoId가 유효한지 확인하는 모듈 추가 (#479)
Browse files Browse the repository at this point in the history
* feat(tag): bookInfoId가 유효한지 확인하는 모듈 추가

bookInfoId가 유효한지 확인하는 모듈 추가

* feat(tag): regex, next return값 수정

regex, next return값 수정

* feat(tag): console.log 삭제

console.log 삭제

---------

Co-authored-by: cjho0316 <cjho03160316@gmail.com>
  • Loading branch information
nyj001012 and cjho0316 authored May 25, 2023
1 parent 0c059e1 commit 60f4987
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
17 changes: 13 additions & 4 deletions backend/src/tags/tags.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export const createDefaultTags = async (
const content = req?.body?.content.trim();
const tagsService = new TagsService();
const regex: RegExp = /[^가-힣a-zA-Z0-9_]/g;
if (content === '' || content.length > 42 || regex.test(content) === false) next(new ErrorResponse(errorCode.INVALID_INPUT_TAGS, 400));
if (await tagsService.isValidBookInfoId(parseInt(bookInfoId, 10)) === false) {
return next(new ErrorResponse(errorCode.INVALID_BOOKINFO_ID, 400));
}
if (content === '' || content.length > 42 || regex.test(content) === true)
return next(new ErrorResponse(errorCode.INVALID_INPUT_TAGS, 400));
await tagsService.createDefaultTags(tokenId, bookInfoId, content);
return res.status(status.CREATED).send();
};
Expand All @@ -31,8 +35,13 @@ export const createSuperTags = async (
const bookInfoId = req?.body?.bookInfoId;
const content = req?.body?.content.trim();
const tagsService = new TagsService();
const regex: RegExp = /[^가-힣a-zA-Z0-9_]/g;
if (content === '' || content === 'default' || content.length > 42 || regex.test(content) === false) next(new ErrorResponse(errorCode.INVALID_INPUT_TAGS, 400));
const regex: RegExp = /[^가-힣a-zA-Z0-9_]/g;
if (await tagsService.isValidBookInfoId(parseInt(bookInfoId, 10)) === false) {
return next(new ErrorResponse(errorCode.INVALID_BOOKINFO_ID, 400));
}
if (content === '' || content === 'default' || content.length > 42 || regex.test(content) === true) {
return next(new ErrorResponse(errorCode.INVALID_INPUT_TAGS, 400));
}
await tagsService.createSuperTags(tokenId, bookInfoId, content);
return res.status(status.CREATED).send();
};
Expand Down Expand Up @@ -125,7 +134,7 @@ export const updateSuperTags = async (
const content = req?.body?.content;
const tagsService = new TagsService();
const regex: RegExp = /[^가-힣a-zA-Z0-9_]/g;
if (content === '' || content === 'default' || content.length > 42 || regex.test(content) === false) {
if (content === '' || content === 'default' || content.length > 42 || regex.test(content) === true) {
return next(new ErrorResponse(errorCode.INVALID_INPUT_TAGS, 400));
}
if (await tagsService.isExistingSuperTag(superTagId, content) === true) {
Expand Down
14 changes: 14 additions & 0 deletions backend/src/tags/tags.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SuperTag from '../entity/entities/SuperTag';
import ErrorResponse from '../utils/error/errorResponse';
import { subDefaultTag, superDefaultTag } from '../DTO/tags.model';
import VTagsSubDefault from '../entity/entities/VTagsSubDefault';
import BookInfo from '../entity/entities/BookInfo';

export class SubTagRepository extends Repository<SubTag> {
private readonly vSubDefaultRepo: Repository<VTagsSubDefault>;
Expand Down Expand Up @@ -87,6 +88,8 @@ export class SuperTagRepository extends Repository<SuperTag> {

private readonly userRepo: Repository<User>;

private readonly bookInfoRepo: Repository<BookInfo>;

private readonly entityManager;

constructor(transactionQueryRunner?: QueryRunner) {
Expand All @@ -102,6 +105,10 @@ export class SuperTagRepository extends Repository<SuperTag> {
User,
this.entityManager,
);
this.bookInfoRepo = new Repository<BookInfo>(
BookInfo,
this.entityManager,
);
}

async getSuperTags(conditions: object) {
Expand Down Expand Up @@ -197,4 +204,11 @@ export class SuperTagRepository extends Repository<SuperTag> {
{ content, updateUserId, updatedAt: new Date() },
);
}

async countBookInfoId(bookInfoId: number): Promise<number> {
const count = await this.bookInfoRepo.count({
where: { id: bookInfoId },
});
return count;
}
}
8 changes: 8 additions & 0 deletions backend/src/tags/tags.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ export class TagsService {
await this.queryRunner.release();
}
}

async isValidBookInfoId(bookInfoId: number): Promise<boolean> {
const count: number = await this.superTagRepository.countBookInfoId(bookInfoId);
if (count === 0) {
return false;
}
return true;
}
}

export default TagsService;
1 change: 1 addition & 0 deletions backend/src/utils/error/errorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const NOT_FOUND_TAGS = '903';
export const CREATE_FAIL_TAGS = '904';
export const UPDATE_FAIL_TAGS = '905';
export const DEFAULT_TAG_ID = '906';
export const INVALID_BOOKINFO_ID = '907';
export const INVALID_TAG_ID = '910';

export const CLIENT_AUTH_FAILED_ERROR_MESSAGE = 'Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method.';

0 comments on commit 60f4987

Please sign in to comment.