|
|
@@ -8,6 +8,9 @@ import { SearchArticleFilter } from '../dto/search-article.filter';
|
|
|
import { BizException } from '../../core/exception/biz.exception';
|
|
|
import { ArticleError } from '../error/article.error';
|
|
|
import { ArticleStatus } from '../enum/article.status';
|
|
|
+import { ArticleSearchRecord } from '../entity/article-search-record.entity';
|
|
|
+import { RequestContext } from 'nestjs-request-context';
|
|
|
+import { User } from '../../core/entity/user.entity';
|
|
|
|
|
|
@Injectable()
|
|
|
export class ArticleService {
|
|
|
@@ -16,6 +19,8 @@ export class ArticleService {
|
|
|
private readonly articleRepository: Repository<Article>,
|
|
|
@Inject(forwardRef(() => CategoryService))
|
|
|
private readonly categoryService: CategoryService,
|
|
|
+ @InjectRepository(ArticleSearchRecord)
|
|
|
+ private readonly articleSearchRecordRepository: Repository<ArticleSearchRecord>,
|
|
|
) {}
|
|
|
|
|
|
async create(createArticleRequest: CreateArticleRequest) {
|
|
|
@@ -36,6 +41,11 @@ export class ArticleService {
|
|
|
searchArticleFilter.categoryId,
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ if (searchArticleFilter.title) {
|
|
|
+ await this.traceSearchRecord(searchArticleFilter.title);
|
|
|
+ }
|
|
|
+
|
|
|
return this.articleRepository.findAndCount({
|
|
|
where: searchArticleFilter.getConditions(),
|
|
|
skip: searchArticleFilter.getSkip(),
|
|
|
@@ -44,6 +54,19 @@ export class ArticleService {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ private async traceSearchRecord(keyword: string) {
|
|
|
+ const existRecord = await this.articleSearchRecordRepository.findOneBy({
|
|
|
+ keyword,
|
|
|
+ });
|
|
|
+
|
|
|
+ if (existRecord) {
|
|
|
+ const articleSearchRecord = this.articleSearchRecordRepository.create({
|
|
|
+ keyword,
|
|
|
+ });
|
|
|
+ await this.articleSearchRecordRepository.save(articleSearchRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async get(id: string) {
|
|
|
const article = await this.articleRepository.findOneBy({ id });
|
|
|
if (!article) {
|
|
|
@@ -91,4 +114,16 @@ export class ArticleService {
|
|
|
},
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ async getSearchRecords(): Promise<ArticleSearchRecord[]> {
|
|
|
+ const currentUser = new User();
|
|
|
+ currentUser.id = RequestContext.currentContext.req.user.id;
|
|
|
+ return this.articleSearchRecordRepository.find({
|
|
|
+ where: {
|
|
|
+ createBy: currentUser,
|
|
|
+ },
|
|
|
+ take: 10,
|
|
|
+ skip: 0,
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|