61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common';
|
|
import { CreateScrapConfigDto } from '../dto/scrap-config/create-scrap-config';
|
|
import { ScrapConfigsService } from '../services/scrap-config.service';
|
|
import { UpdateScrapConfigDto } from '../dto/scrap-config/update-scrap-config';
|
|
import { ScrapConfig } from '../entities/scrap-config.entity';
|
|
import { IsNull, Not } from 'typeorm';
|
|
import { ScrapItemsService } from '../services/scrap-item-config.service';
|
|
|
|
@Controller('admin/scrap-configs')
|
|
export class ScrapConfigsController {
|
|
constructor(
|
|
private readonly scrapConfigsService: ScrapConfigsService,
|
|
private readonly scrapItemsService: ScrapItemsService,
|
|
) {}
|
|
|
|
@Post()
|
|
async create(@Body() data: CreateScrapConfigDto) {
|
|
return await this.scrapConfigsService.create(data);
|
|
}
|
|
|
|
@Put(':id')
|
|
async update(
|
|
@Param('id') id: ScrapConfig['id'],
|
|
@Body() data: UpdateScrapConfigDto,
|
|
) {
|
|
return await this.scrapConfigsService.update(id, data);
|
|
}
|
|
|
|
@Get()
|
|
async test() {
|
|
const scrapConfigs = await this.scrapConfigsService.scrapConfigRepo.find({
|
|
where: {
|
|
search_url: Not(IsNull()),
|
|
keywords: Not(IsNull()),
|
|
},
|
|
relations: {
|
|
web_bid: true,
|
|
},
|
|
});
|
|
|
|
const models = this.scrapConfigsService.scrapModels(scrapConfigs);
|
|
|
|
await Promise.allSettled(
|
|
models.map(async (item) => {
|
|
await item.action();
|
|
|
|
Object.keys(item.results).forEach(async (key) => {
|
|
const data = item.results[key];
|
|
|
|
await this.scrapItemsService.scrapItemRepo.upsert(data, [
|
|
'model',
|
|
'scrap_config',
|
|
]);
|
|
});
|
|
}),
|
|
);
|
|
|
|
return { a: 'abc' };
|
|
}
|
|
}
|