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;
|
||||
27
dist/modules/crawling/application/commands/StopCrawlCommand.js
vendored
Normal file
27
dist/modules/crawling/application/commands/StopCrawlCommand.js
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.StopCrawlCommand = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class StopCrawlCommand {
|
||||
constructor(repository, eventBus) {
|
||||
this.repository = repository;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
async execute(request) {
|
||||
const id = UniqueId_1.UniqueId.from(request.sessionId);
|
||||
const session = await this.repository.findById(id);
|
||||
if (!session) {
|
||||
return (0, Result_1.Err)('Session not found');
|
||||
}
|
||||
session.stop();
|
||||
await this.repository.update(session);
|
||||
const events = session.domainEvents;
|
||||
for (const event of events) {
|
||||
await this.eventBus.publish(event);
|
||||
}
|
||||
session.clearEvents();
|
||||
return (0, Result_1.Ok)(undefined);
|
||||
}
|
||||
}
|
||||
exports.StopCrawlCommand = StopCrawlCommand;
|
||||
28
dist/modules/crawling/application/queries/GetSessionQuery.js
vendored
Normal file
28
dist/modules/crawling/application/queries/GetSessionQuery.js
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.GetSessionQuery = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
||||
class GetSessionQuery {
|
||||
constructor(repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
async execute(request) {
|
||||
const id = UniqueId_1.UniqueId.from(request.sessionId);
|
||||
const session = await this.repository.findById(id);
|
||||
if (!session) {
|
||||
return (0, Result_1.Err)('Session not found');
|
||||
}
|
||||
const dto = {
|
||||
id: session.id.toString(),
|
||||
url: session.url,
|
||||
status: session.status,
|
||||
seed: session.seed,
|
||||
maxStates: session.maxStates,
|
||||
statesVisited: session.statesVisited,
|
||||
config: session.config,
|
||||
};
|
||||
return (0, Result_1.Ok)(dto);
|
||||
}
|
||||
}
|
||||
exports.GetSessionQuery = GetSessionQuery;
|
||||
23
dist/modules/crawling/application/queries/ListSessionsQuery.js
vendored
Normal file
23
dist/modules/crawling/application/queries/ListSessionsQuery.js
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ListSessionsQuery = void 0;
|
||||
const Result_1 = require("../../../../shared/domain/Result");
|
||||
class ListSessionsQuery {
|
||||
constructor(repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
async execute(_request) {
|
||||
const sessions = await this.repository.findAll();
|
||||
const dtos = sessions.map((session) => ({
|
||||
id: session.id.toString(),
|
||||
url: session.url,
|
||||
status: session.status,
|
||||
seed: session.seed,
|
||||
maxStates: session.maxStates,
|
||||
statesVisited: session.statesVisited,
|
||||
config: session.config,
|
||||
}));
|
||||
return (0, Result_1.Ok)(dtos);
|
||||
}
|
||||
}
|
||||
exports.ListSessionsQuery = ListSessionsQuery;
|
||||
Reference in New Issue
Block a user