Skip to content

Commit

Permalink
Merge pull request #334 from Quickchive/feat/#333/restore-content
Browse files Browse the repository at this point in the history
Feat/#333/restore content
  • Loading branch information
stae1102 authored Jun 9, 2024
2 parents 03a7984 + 3a733bf commit 5eca7ba
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
4 changes: 4 additions & 0 deletions src/common/entities/core.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import {
CreateDateColumn,
DeleteDateColumn,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
Expand All @@ -17,4 +18,7 @@ export class CoreEntity {
@ApiProperty()
@UpdateDateColumn()
updatedAt!: Date;

@DeleteDateColumn({ nullable: true, default: null })
deletedAt: Date | null;
}
32 changes: 15 additions & 17 deletions src/contents/contents.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,21 +253,19 @@ export class ContentsController {
return this.contentsService.summarizeContent(user, contentId);
}

// @ApiOperation({
// summary: '간편 문서 요약',
// description: '성능 테스트를 위해 만든 간편 문서 요약 메서드',
// })
// @ApiOkResponse({
// description: '간편 문서 요약 성공 여부를 반환한다.',
// type: SummarizeContentOutput,
// })
// @ApiBadRequestResponse({
// description: 'naver 서버에 잘못된 요청을 보냈을 경우',
// })
// @Post('summarize')
// async testSummarizeContent(
// @Body() content: SummarizeContentBodyDto,
// ): Promise<SummarizeContentOutput> {
// return this.contentsService.testSummarizeContent(content);
// }
@ApiOperation({
summary: '삭제된 컨텐츠 복원',
description: '삭제된 컨텐츠를 복원합니다.',
})
@ApiOkResponse({
description: '삭제된 컨텐츠 복원 성공 여부를 반환합니다.',
})
@UseGuards(JwtAuthGuard)
@Patch(':contentId/restore')
async restoreContent(
@AuthUser() user: User,
@Param('contentId', new ParseIntPipe()) contentId: number,
) {
return this.contentsService.restoreContent(user, contentId);
}
}
27 changes: 27 additions & 0 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,31 @@ export class ContentsService {
throw e;
}
}

async restoreContent(user: User, contentId: number) {
try {
const content = await this.contentRepository.findOne({
where: {
id: contentId,
},
withDeleted: true,
});

if (!content) {
throw new NotFoundException('Content not found.');
}
if (content.userId !== user.id) {
throw new ForbiddenException(
'You are not allowed to restore this content',
);
}

content.deletedAt = null;
await this.contentRepository.save(content);

return {};
} catch (e) {
throw e;
}
}
}
4 changes: 2 additions & 2 deletions src/contents/repository/content.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class ContentRepository extends Repository<Content> {

async deleteOne(contentId: number, entityManager?: EntityManager) {
return entityManager
? entityManager.delete(Content, contentId)
: this.delete({ id: contentId });
? entityManager.softDelete(Content, contentId)
: this.softDelete({ id: contentId });
}
}

0 comments on commit 5eca7ba

Please sign in to comment.