第一次提交

This commit is contained in:
dmy
2026-01-09 23:18:52 +08:00
commit d9105797f4
46 changed files with 15003 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';
@Entity('keywords')
export class Keyword {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
word: string;
@Column({ default: 1 })
weight: number; // 1-5级
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}

View File

@@ -0,0 +1,22 @@
import { Controller, Get, Post, Body, Delete, Param } from '@nestjs/common';
import { KeywordsService } from './keywords.service';
@Controller('api/keywords')
export class KeywordsController {
constructor(private readonly keywordsService: KeywordsService) {}
@Get()
findAll() {
return this.keywordsService.findAll();
}
@Post()
create(@Body('word') word: string, @Body('weight') weight: number) {
return this.keywordsService.create(word, weight);
}
@Delete(':id')
remove(@Param('id') id: string) {
return this.keywordsService.remove(id);
}
}

View File

@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Keyword } from './keyword.entity';
import { KeywordsService } from './keywords.service';
import { KeywordsController } from './keywords.controller';
@Module({
imports: [TypeOrmModule.forFeature([Keyword])],
providers: [KeywordsService],
controllers: [KeywordsController],
exports: [KeywordsService],
})
export class KeywordsModule {}

View File

@@ -0,0 +1,35 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Keyword } from './keyword.entity';
@Injectable()
export class KeywordsService implements OnModuleInit {
constructor(
@InjectRepository(Keyword)
private keywordRepository: Repository<Keyword>,
) {}
async onModuleInit() {
// 初始预设关键词
const defaultKeywords = ["山东", "海", "建设", "工程", "采购"];
for (const word of defaultKeywords) {
const exists = await this.keywordRepository.findOne({ where: { word } });
if (!exists) {
await this.keywordRepository.save({ word, weight: 1 });
}
}
}
findAll() {
return this.keywordRepository.find();
}
create(word: string, weight: number = 1) {
return this.keywordRepository.save({ word, weight });
}
remove(id: string) {
return this.keywordRepository.delete(id);
}
}