Skip to content

Commit

Permalink
split up document generator to link and page generator
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelblum committed Sep 14, 2023
1 parent bad0e2b commit 9d6a8ec
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 59 deletions.
17 changes: 10 additions & 7 deletions demo/api/src/db/fixtures/fixtures.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Inject, Injectable } from "@nestjs/common";
import { Config } from "@src/config/config";
import { CONFIG } from "@src/config/config.module";
import { generateSeoBlock } from "@src/db/fixtures/generators/blocks/seo.generator";
import { DocumentGeneratorService } from "@src/db/fixtures/generators/document-generator.service";
import { ImageGeneratorService } from "@src/db/fixtures/generators/image-generator.service";
import { Link } from "@src/links/entities/link.entity";
import { PageTreeNodeScope } from "@src/page-tree/dto/page-tree-node-scope";
Expand All @@ -18,6 +17,9 @@ import faker from "faker";
import { Command, Console } from "nestjs-console";
import slugify from "slugify";

import { LinkGeneratorService } from "./generators/link-generator.service";
import { PageGeneratorService } from "./generators/page-generator.service";

const getDefaultPageInput = (): PageInput => {
const pageInput = new PageInput();
pageInput.seo = generateSeoBlock();
Expand All @@ -32,7 +34,8 @@ export class FixturesConsole {
@Inject(CONFIG) private readonly config: Config,
private readonly blobStorageBackendService: BlobStorageBackendService,
private readonly pageTreeService: PageTreeService,
private readonly documentGeneratorService: DocumentGeneratorService,
private readonly pageGeneratorService: PageGeneratorService,
private readonly linkGeneratorService: LinkGeneratorService,
private readonly imageGeneratorService: ImageGeneratorService,
private readonly orm: MikroORM,
@InjectRepository(Page) private readonly pagesRepository: EntityRepository<Page>,
Expand Down Expand Up @@ -72,17 +75,17 @@ export class FixturesConsole {
language: "en",
};

const node = await this.documentGeneratorService.generatePage({ name: "Home", scope });
await this.documentGeneratorService.generatePage({ name: "Sub", scope, parentId: node.id });
const node = await this.pageGeneratorService.generatePage({ name: "Home", scope });
await this.pageGeneratorService.generatePage({ name: "Sub", scope, parentId: node.id });

for (let i = 0; i < 3; i++) {
const page = await this.documentGeneratorService.generatePage({ name: `Page ${i}`, scope });
const page = await this.pageGeneratorService.generatePage({ name: `Page ${i}`, scope });

for (let subPageIndex = 0; subPageIndex < 3; subPageIndex++) {
if (faker.datatype.boolean()) {
await this.documentGeneratorService.generatePage({ name: `Sub-Page ${subPageIndex}`, scope, parentId: page.id });
await this.pageGeneratorService.generatePage({ name: `Sub-Page ${subPageIndex}`, scope, parentId: page.id });
} else {
await this.documentGeneratorService.generateLink({ name: `Sub-Link ${subPageIndex}`, scope, parentId: page.id });
await this.linkGeneratorService.generateLink({ name: `Sub-Link ${subPageIndex}`, scope, parentId: page.id });
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions demo/api/src/db/fixtures/fixtures.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@src/config/config.module";
import { FixturesConsole } from "@src/db/fixtures/fixtures.console";
import { DocumentGeneratorService } from "@src/db/fixtures/generators/document-generator.service";
import { ImageGeneratorService } from "@src/db/fixtures/generators/image-generator.service";
import { LinksModule } from "@src/links/links.module";
import { PagesModule } from "@src/pages/pages.module";
import { ConsoleModule } from "nestjs-console";

import { LinkGeneratorService } from "./generators/link-generator.service";
import { PageGeneratorService } from "./generators/page-generator.service";

@Module({
imports: [ConfigModule, ConsoleModule, PagesModule, LinksModule],
providers: [FixturesConsole, DocumentGeneratorService, ImageGeneratorService],
providers: [FixturesConsole, PageGeneratorService, LinkGeneratorService, ImageGeneratorService],
})
export class FixturesModule {}
66 changes: 66 additions & 0 deletions demo/api/src/db/fixtures/generators/link-generator.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { ExternalLinkBlock } from "@comet/blocks-api";
import { PageTreeNodeInterface, PageTreeNodeVisibility, PageTreeService } from "@comet/cms-api";
import { InjectRepository } from "@mikro-orm/nestjs";
import { EntityRepository } from "@mikro-orm/postgresql";
import { Injectable } from "@nestjs/common";
import { LinkBlock } from "@src/common/blocks/linkBlock/link.block";
import { Link } from "@src/links/entities/link.entity";
import { PageTreeNodeScope } from "@src/page-tree/dto/page-tree-node-scope";
import { PageTreeNodeCategory } from "@src/page-tree/page-tree-node-category";
import faker from "faker";
import slugify from "slugify";

interface GenerateLinkInput {
name: string;
scope: PageTreeNodeScope;
parentId?: string;
}

@Injectable()
export class LinkGeneratorService {
constructor(private readonly pageTreeService: PageTreeService, @InjectRepository(Link) private readonly linkRepository: EntityRepository<Link>) {}

async generateLink({ name, scope, parentId }: GenerateLinkInput): Promise<PageTreeNodeInterface> {
const id = faker.datatype.uuid();
const slug = slugify(name.toLowerCase());

const node = await this.pageTreeService.createNode(
{
name,
slug,
attachedDocument: {
id,
type: "Link",
},
parentId,
},
PageTreeNodeCategory.MainNavigation,
scope,
);
await this.pageTreeService.updateNodeVisibility(node.id, PageTreeNodeVisibility.Published);

// TODO: Put in own file, link-block.generator that handles the different link types (external, internal, news)
const externalLinkUrls = ["https://vivid-planet.com/", "https://github.com/", "https://gitlab.com", "https://stackoverflow.com/"];
const content = LinkBlock.blockInputFactory({
attachedBlocks: [
{
type: "external",
props: ExternalLinkBlock.blockDataFactory({
targetUrl: faker.random.arrayElement(externalLinkUrls),
openInNewWindow: faker.datatype.boolean(),
}),
},
],
activeType: "external",
});

await this.linkRepository.persistAndFlush(
this.linkRepository.create({
id,
content: content.transformToBlockData(),
}),
);

return node;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { ExternalLinkBlock } from "@comet/blocks-api";
import { PageTreeNodeInterface, PageTreeNodeVisibility, PageTreeService } from "@comet/cms-api";
import { InjectRepository } from "@mikro-orm/nestjs";
import { EntityRepository } from "@mikro-orm/postgresql";
import { Inject, Injectable } from "@nestjs/common";
import { LinkBlock } from "@src/common/blocks/linkBlock/link.block";
import { Config } from "@src/config/config";
import { CONFIG } from "@src/config/config.module";
import { generateBlocksBlock } from "@src/db/fixtures/generators/blocks/blocks.generator";
import { generateSeoBlock } from "@src/db/fixtures/generators/blocks/seo.generator";
import { Link } from "@src/links/entities/link.entity";
import { PageTreeNodeScope } from "@src/page-tree/dto/page-tree-node-scope";
import { PageTreeNodeCategory } from "@src/page-tree/page-tree-node-category";
import { Page } from "@src/pages/entities/page.entity";
Expand All @@ -24,12 +21,11 @@ interface GeneratePageInput {
}

@Injectable()
export class DocumentGeneratorService {
export class PageGeneratorService {
constructor(
@Inject(CONFIG) private readonly config: Config,
private readonly pageTreeService: PageTreeService,
@InjectRepository(Page) private readonly pagesRepository: EntityRepository<Page>,
@InjectRepository(Link) private readonly linkRepository: EntityRepository<Link>,
private readonly imageGeneratorService: ImageGeneratorService,
) {}

Expand Down Expand Up @@ -67,49 +63,4 @@ export class DocumentGeneratorService {

return node;
}

async generateLink({ name, scope, parentId }: GeneratePageInput): Promise<PageTreeNodeInterface> {
const id = faker.datatype.uuid();
const slug = slugify(name.toLowerCase());
console.log("slugLink", slug);

const node = await this.pageTreeService.createNode(
{
name,
slug,
attachedDocument: {
id,
type: "Link",
},
parentId,
},
PageTreeNodeCategory.MainNavigation,
scope,
);
await this.pageTreeService.updateNodeVisibility(node.id, PageTreeNodeVisibility.Published);

// TODO: Put in own file, link-block.generator that handles the different link types (external, internal, news)
const externalLinkUrls = ["https://vivid-planet.com/", "https://github.com/", "https://gitlab.com", "https://stackoverflow.com/"];
const content = LinkBlock.blockInputFactory({
attachedBlocks: [
{
type: "external",
props: ExternalLinkBlock.blockDataFactory({
targetUrl: faker.random.arrayElement(externalLinkUrls),
openInNewWindow: faker.datatype.boolean(),
}),
},
],
activeType: "external",
});

await this.linkRepository.persistAndFlush(
this.linkRepository.create({
id,
content: content.transformToBlockData(),
}),
);

return node;
}
}

0 comments on commit 9d6a8ec

Please sign in to comment.