feat: 检索历史接口
This commit is contained in:
1 parent
925bcdb987
commit
c69308623c
11 files changed
+128
-2
No files matched your search
@@ -9,10 +9,18 @@ import { ArticleController } from './controller/article.controller';
|
||||
import { CarousalController } from './controller/carousal.controller';
|
||||
import { CarousalService } from './service/carousal.service';
|
||||
import { Carousal } from './entity/carousal.entity';
|
||||
import { ArticleSearchRecord } from './entity/article-search-record.entity';
|
||||
|
||||
@Module({
|
||||
controllers: [CategoryController, ArticleController, CarousalController],
|
||||
imports: [TypeOrmModule.forFeature([Category, Article, Carousal])],
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([
|
||||
Category,
|
||||
Article,
|
||||
Carousal,
|
||||
ArticleSearchRecord,
|
||||
]),
|
||||
],
|
||||
providers: [CategoryService, ArticleService, CarousalService],
|
||||
})
|
||||
export class ArticleModule {}
|
||||
@@ -16,6 +16,8 @@ import { ArticleMapper } from '../mapper/article.mapper';
|
||||
import { SearchArticleFilter } from '../dto/search-article.filter';
|
||||
import { PageResult } from '../../core/vo/page-result';
|
||||
import { PageResultMapper } from '../../core/mapper/page-result.mapper';
|
||||
import { ArticleSearchRecordMapper } from '../mapper/article-search-record.mapper';
|
||||
import { ArticleSearchRecordVo } from '../vo/article-search-record.vo';
|
||||
|
||||
@Controller('articles')
|
||||
export class ArticleController {
|
||||
@@ -60,6 +62,24 @@ export class ArticleController {
|
||||
total,
|
||||
);
|
||||
}
|
||||
@Get('search-records')
|
||||
@ApiBearerAuth()
|
||||
@ApiOkResponse({
|
||||
description: '查询列表',
|
||||
schema: {
|
||||
allOf: [
|
||||
{
|
||||
type: 'array',
|
||||
items: { $ref: getSchemaPath(ArticleVo) }, // 泛型内容具体化
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
async searchRecords() {
|
||||
return ArticleSearchRecordMapper.toVos(
|
||||
await this.articleService.getSearchRecords(),
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiBearerAuth()
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Column, Entity } from 'typeorm';
|
||||
import { TraceableEntity } from '../../core/entity/traceable.entity';
|
||||
|
||||
@Entity()
|
||||
export class ArticleSearchRecord extends TraceableEntity {
|
||||
@Column()
|
||||
keyword: string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { ArticleSearchRecordVo } from '../vo/article-search-record.vo';
|
||||
import { ArticleSearchRecord } from '../entity/article-search-record.entity';
|
||||
import { DateUtil } from '../../core/util/date.util';
|
||||
|
||||
export class ArticleSearchRecordMapper {
|
||||
static toVo(entity: ArticleSearchRecord): ArticleSearchRecordVo {
|
||||
return {
|
||||
id: entity.id,
|
||||
keyword: entity.keyword,
|
||||
createdTime: DateUtil.format(entity.createdTime),
|
||||
updatedTime: DateUtil.format(entity.updatedTime),
|
||||
};
|
||||
}
|
||||
static toVos(entities: ArticleSearchRecord[]): ArticleSearchRecordVo[] {
|
||||
return entities.map((entity) => this.toVo(entity));
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ApiProperty, ApiSchema } from '@nestjs/swagger';
|
||||
import { BaseVo } from '../../core/vo/base.vo';
|
||||
|
||||
@ApiSchema({
|
||||
name: 'ArticleSearchRecord',
|
||||
})
|
||||
export class ArticleSearchRecordVo extends BaseVo {
|
||||
@ApiProperty()
|
||||
keyword: string;
|
||||
}
|
||||
@@ -19,6 +19,7 @@ import { UserBind } from './entity/user-bind.entity';
|
||||
import { FileController } from './controller/file.controller';
|
||||
import { FileService } from './service/file.service';
|
||||
import { CosStrategy } from './service/storage-strategy/cos.strategy';
|
||||
import { RequestContextModule } from 'nestjs-request-context';
|
||||
|
||||
@Module({
|
||||
controllers: [
|
||||
@@ -60,6 +61,7 @@ import { CosStrategy } from './service/storage-strategy/cos.strategy';
|
||||
expiresIn: JWT_EXPIRATION,
|
||||
},
|
||||
}),
|
||||
RequestContextModule,
|
||||
],
|
||||
providers: [
|
||||
{
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { BaseEntity } from './base.entity';
|
||||
import { ManyToOne } from 'typeorm';
|
||||
import { BeforeInsert, BeforeUpdate, ManyToOne } from 'typeorm';
|
||||
import { User } from './user.entity';
|
||||
import { RequestContext } from 'nestjs-request-context';
|
||||
|
||||
export abstract class TraceableEntity extends BaseEntity {
|
||||
@ManyToOne(() => User, { eager: true })
|
||||
@@ -8,4 +9,15 @@ export abstract class TraceableEntity extends BaseEntity {
|
||||
|
||||
@ManyToOne(() => User, { eager: true })
|
||||
updateBy: User;
|
||||
|
||||
@BeforeInsert()
|
||||
setCreatedBy() {
|
||||
this.createBy = RequestContext.currentContext.req.user.id || null;
|
||||
this.updateBy = RequestContext.currentContext.req.user.id || null;
|
||||
}
|
||||
|
||||
@BeforeUpdate()
|
||||
setUpdatedBy() {
|
||||
this.updateBy = RequestContext.currentContext.req.user.id || null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user