fase(3): crawling module domain and application
This commit is contained in:
36
dist/modules/crawling/application/commands/StartCrawlCommand.js
vendored
Normal file
36
dist/modules/crawling/application/commands/StartCrawlCommand.js
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StartCrawlCommand = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const Url_1 = require("../../domain/value-objects/Url");
|
||||
const CrawlSession_1 = require("../../domain/entities/CrawlSession");
|
||||
class StartCrawlCommand {
|
||||
constructor(repository, eventBus) {
|
||||
this.repository = repository;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
async execute(request) {
|
||||
const urlResult = Url_1.Url.create(request.url);
|
||||
if (!urlResult.ok) {
|
||||
return (0, Result_1.Err)(urlResult.error);
|
||||
}
|
||||
const sessionResult = CrawlSession_1.CrawlSession.create({
|
||||
url: request.url,
|
||||
seed: request.seed,
|
||||
maxStates: request.maxStates,
|
||||
config: request.config,
|
||||
});
|
||||
if (!sessionResult.ok) {
|
||||
return (0, Result_1.Err)(sessionResult.error);
|
||||
}
|
||||
const session = sessionResult.value;
|
||||
await this.repository.save(session);
|
||||
const events = session.domainEvents;
|
||||
for (const event of events) {
|
||||
await this.eventBus.publish(event);
|
||||
}
|
||||
session.clearEvents();
|
||||
return (0, Result_1.Ok)({ sessionId: session.id.toString() });
|
||||
}
|
||||
}
|
||||
exports.StartCrawlCommand = StartCrawlCommand;
|
||||
Reference in New Issue
Block a user