|
@@ -0,0 +1,79 @@
|
|
|
|
|
+import { Injectable } from '@nestjs/common';
|
|
|
|
|
+import { CreateArticleRequest } from '../dto/create-article.request';
|
|
|
|
|
+import { Repository } from 'typeorm';
|
|
|
|
|
+import { Article } from '../entity/article.entity';
|
|
|
|
|
+import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
+import { CategoryService } from './category.service';
|
|
|
|
|
+import { SearchArticleFilter } from '../dto/search-article.filter';
|
|
|
|
|
+import { BizException } from '../../core/exception/biz.exception';
|
|
|
|
|
+import { ArticleError } from '../error/article.error';
|
|
|
|
|
+import { ArticleStatusEnum } from '../enum/article-status.enum';
|
|
|
|
|
+
|
|
|
|
|
+@Injectable()
|
|
|
|
|
+export class ArticleService {
|
|
|
|
|
+ constructor(
|
|
|
|
|
+ @InjectRepository(Article)
|
|
|
|
|
+ private readonly articleRepository: Repository<Article>,
|
|
|
|
|
+ private readonly categoryService: CategoryService,
|
|
|
|
|
+ ) {}
|
|
|
|
|
+
|
|
|
|
|
+ async create(createArticleRequest: CreateArticleRequest) {
|
|
|
|
|
+ const category = await this.categoryService.get(
|
|
|
|
|
+ createArticleRequest.categoryId,
|
|
|
|
|
+ );
|
|
|
|
|
+ const article = new Article();
|
|
|
|
|
+ article.title = createArticleRequest.title;
|
|
|
|
|
+ article.content = createArticleRequest.content;
|
|
|
|
|
+ article.category = category;
|
|
|
|
|
+ return this.articleRepository.save(article);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async search(searchArticleFilter: SearchArticleFilter) {
|
|
|
|
|
+ return this.articleRepository.findAndCount({
|
|
|
|
|
+ where: searchArticleFilter.getConditions(),
|
|
|
|
|
+ skip: searchArticleFilter.getSkip(),
|
|
|
|
|
+ take: searchArticleFilter.getSize(),
|
|
|
|
|
+ order: searchArticleFilter.getOrderBy(),
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async get(id: string) {
|
|
|
|
|
+ const article = await this.articleRepository.findOneBy({ id });
|
|
|
|
|
+ if (!article) {
|
|
|
|
|
+ throw new BizException(ArticleError.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ return article;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async update(id: string, updateArticle: CreateArticleRequest) {
|
|
|
|
|
+ const article = await this.articleRepository.findOneBy({ id });
|
|
|
|
|
+ if (!article) {
|
|
|
|
|
+ throw new BizException(ArticleError.NOT_FOUND);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (article.category.id !== updateArticle.categoryId) {
|
|
|
|
|
+ article.category = await this.categoryService.get(
|
|
|
|
|
+ updateArticle.categoryId,
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ article.title = updateArticle.title;
|
|
|
|
|
+ article.content = updateArticle.content;
|
|
|
|
|
+ return this.articleRepository.save(article);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async publish(id: string) {
|
|
|
|
|
+ const article = await this.get(id);
|
|
|
|
|
+ article.status = ArticleStatusEnum.PUBLISHED;
|
|
|
|
|
+ return this.articleRepository.save(article);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async close(id: string) {
|
|
|
|
|
+ const article = await this.get(id);
|
|
|
|
|
+ article.status = ArticleStatusEnum.CLOSED;
|
|
|
|
|
+ return this.articleRepository.save(article);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async delete(id: string) {
|
|
|
|
|
+ const article = await this.get(id);
|
|
|
|
|
+ await this.articleRepository.remove(article);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|