passing result to DB service

This commit is contained in:
Kfir Dayan 2023-04-18 13:29:39 +03:00
parent 661d5e9880
commit a5799e4b48
4 changed files with 18 additions and 17 deletions

View file

@ -1,11 +1,16 @@
import { Body, Controller, Post } from '@nestjs/common';
import { CrawlerService } from '../crawler/crawler.service';
import { DbService } from '../db/db.service';
@Controller('/')
export class ApiController {
constructor(private crawlerService: CrawlerService) {}
constructor(private crawlerService: CrawlerService, private DbService: DbService) {}
@Post('crawl')
async crawl(@Body() body: { url: string }) {
return this.crawlerService.crawl(body.url);
const results = this.crawlerService.crawl(body.url);
results.then((data) => {
console.log(data)
this.DbService.insert(data, 'crawler');
});
}
}

View file

@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { ApiController } from './api.controller';
import { CrawlerService } from '../crawler/crawler.service';
import { DbService } from '../db/db.service';
@Module({
controllers: [ApiController],
providers: [CrawlerService]
providers: [CrawlerService, DbService]
})
export class ApiModule {}

View file

@ -42,18 +42,10 @@ export class CrawlerService {
// URLS //
const urls = await page.$$eval('a', links => links.map(link => link.href));
await browser.close();
// // save to db
// this.dbService.insert({
// url,
// domain,
// cssSheetsLocation,
// scriptsSheetsLocation,
// screenshotBuffer,
// urls
// }, 'crawler');
return {
cssSheetsLocation,
scriptsSheetsLocation
}
}
async downloadFiles(urls: string[], path: string) {

View file

@ -6,7 +6,10 @@ export class DbService {
console.log(`DbService constructor`);
}
insert(data, collection) {
console.log(`DbService insert`);
insert(data: {
cssSheetsLocation: string[];
scriptsSheetsLocation: string[];
}, collection: string) {
console.log({data, collection});
}
}