From a5799e4b48c5ca86274aa1589568e9bb879fd553 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Tue, 18 Apr 2023 13:29:39 +0300 Subject: [PATCH] passing result to DB service --- src/api/api.controller.ts | 9 +++++++-- src/api/api.module.ts | 3 ++- src/crawler/crawler.service.ts | 16 ++++------------ src/db/db.service.ts | 7 +++++-- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/api/api.controller.ts b/src/api/api.controller.ts index d3497e1..d8cf5f2 100644 --- a/src/api/api.controller.ts +++ b/src/api/api.controller.ts @@ -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'); + }); } } diff --git a/src/api/api.module.ts b/src/api/api.module.ts index 1602fc5..551e6fa 100644 --- a/src/api/api.module.ts +++ b/src/api/api.module.ts @@ -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 {} diff --git a/src/crawler/crawler.service.ts b/src/crawler/crawler.service.ts index 074a97d..0deb083 100644 --- a/src/crawler/crawler.service.ts +++ b/src/crawler/crawler.service.ts @@ -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) { diff --git a/src/db/db.service.ts b/src/db/db.service.ts index dc92d3f..9d7c5a2 100644 --- a/src/db/db.service.ts +++ b/src/db/db.service.ts @@ -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}); } }