11import {
22 BadRequestException ,
3+ ConflictException ,
34 ForbiddenException ,
45 Injectable ,
56 NotFoundException ,
67} from '@nestjs/common' ;
7- import { DataSource , EntityManager } from 'typeorm' ;
8+ import { DataSource , EntityManager , In , Not } from 'typeorm' ;
89
910import {
1011 AddContentBodyDto ,
@@ -86,17 +87,23 @@ export class ContentsService {
8687 const content = new Content ( ) ;
8788
8889 if ( categoryId ) {
89- const category = await this . categoryRepository . findById (
90- categoryId ,
91- entityManager ,
92- ) ;
90+ const [ category , subCategories ] = await Promise . all ( [
91+ ( async ( ) => {
92+ const category = await this . categoryRepository . findById ( categoryId ) ;
9393
94- if ( ! category ) throw new NotFoundException ( 'Category not found' ) ;
94+ if ( ! category ) {
95+ throw new NotFoundException ( '카테고리가 존재하지 않습니다.' ) ;
96+ }
9597
96- await checkContentDuplicateAndAddCategorySaveLog (
97- link ,
98- category ,
99- userInDb ,
98+ return category ;
99+ } ) ( ) ,
100+ this . categoryRepository . findByParentId ( categoryId ) ,
101+ ] ) ;
102+
103+ await this . isDuplicatedContents (
104+ [ category , ...subCategories ] ,
105+ content . link ,
106+ content . id ,
100107 ) ;
101108
102109 content . category = category ;
@@ -184,8 +191,6 @@ export class ContentsService {
184191 reminder,
185192 favorite,
186193 categoryId,
187- categoryName,
188- parentId,
189194 } : UpdateContentBodyDto ,
190195 entityManager ?: EntityManager ,
191196 ) : Promise < AddContentOutput > {
@@ -197,33 +202,38 @@ export class ContentsService {
197202 reminder,
198203 favorite,
199204 } ;
200- const userInDb = await this . userRepository . findOneWithContentsAndCategories (
201- user . id ,
202- ) ;
203- if ( ! userInDb ) {
204- throw new NotFoundException ( 'User not found' ) ;
205- }
206205
207- const content = userInDb ?. contents ?. filter (
208- ( content ) => content . id === contentId ,
209- ) [ 0 ] ;
206+ const content = await this . contentRepository . findOne ( {
207+ where : {
208+ id : contentId ,
209+ } ,
210+ relations : [ 'category' ] ,
211+ } ) ;
212+
210213 if ( ! content ) {
211- throw new NotFoundException ( 'Content not found .' ) ;
214+ throw new NotFoundException ( '컨텐츠가 존재하지 않습니다 .' ) ;
212215 }
213216
214- if ( categoryId !== undefined ) {
215- const category =
216- categoryId !== null
217- ? await this . categoryRepository . findById ( categoryId , entityManager )
218- : null ;
217+ // 카테고리 변경이 발생하는 경우
218+ if ( categoryId && ! content . isSameCategory ( categoryId ) ) {
219+ const [ category , subCategories ] = await Promise . all ( [
220+ ( async ( ) => {
221+ const category = await this . categoryRepository . findById ( categoryId ) ;
219222
220- if ( category ) {
221- await checkContentDuplicateAndAddCategorySaveLog (
222- link ,
223- category ,
224- userInDb ,
225- ) ;
226- }
223+ if ( ! category ) {
224+ throw new NotFoundException ( '카테고리가 존재하지 않습니다.' ) ;
225+ }
226+
227+ return category ;
228+ } ) ( ) ,
229+ this . categoryRepository . findByParentId ( categoryId ) ,
230+ ] ) ;
231+
232+ await this . isDuplicatedContents (
233+ [ category , ...subCategories ] ,
234+ content . link ,
235+ content . id ,
236+ ) ;
227237
228238 await this . contentRepository . updateOne (
229239 {
@@ -471,4 +481,24 @@ export class ContentsService {
471481 throw e ;
472482 }
473483 }
484+
485+ private async isDuplicatedContents (
486+ categories : Category [ ] ,
487+ link : string ,
488+ id ?: number ,
489+ ) {
490+ const existingContents = await this . contentRepository . find ( {
491+ where : {
492+ ...( id && { id : Not ( id ) } ) ,
493+ category : {
494+ id : In ( categories . map ( ( category ) => category . id ) ) ,
495+ } ,
496+ link,
497+ } ,
498+ } ) ;
499+
500+ if ( existingContents . length > 0 ) {
501+ throw new ConflictException ( '이미 저장된 컨텐츠입니다.' ) ;
502+ }
503+ }
474504}
0 commit comments