Skip to content

Commit

Permalink
refactor(tools): granulariza geradores de entidades e casos de uso
Browse files Browse the repository at this point in the history
  • Loading branch information
guiseek committed Dec 7, 2024
1 parent a6a3122 commit 965a679
Show file tree
Hide file tree
Showing 25 changed files with 292 additions and 15 deletions.
5 changes: 5 additions & 0 deletions tools/plugin/dx-dev/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"factory": "./src/generators/entity/generator",
"schema": "./src/generators/entity/schema.json",
"description": "Entity generator"
},
"use-cases": {
"factory": "./src/generators/use-cases/generator",
"schema": "./src/generators/use-cases/schema.json",
"description": "Use Cases"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/client';
import { <%= className %>Service } from '../services';
import {
UseCase,
<%= className %>,
Editable<%= className %>,
} from '@devmx/shared-api-interfaces';

export class Create<%= className %>UseCase
implements UseCase<Editable<%= className %>, <%= className %>>
{
constructor(private <%= propertyName %>Service: <%= className %>Service) {}

execute(data: Editable<%= className %>) {
return this.<%= propertyName %>Service.create(data);
}
}

export function provideCreate<%= className %>UseCase() {
return createUseCaseProvider(Create<%= className %>UseCase, [<%= className %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/client';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= className %>Service } from '../services';

export class Delete<%= className %>UseCase implements UseCase<string, <%= className %> | null> {
constructor(private <%= propertyName %>Service: <%= className %>Service) {}

execute(id: string) {
return this.<%= propertyName %>Service.delete(id);
}
}

export function provideDelete<%= className %>UseCase() {
return createUseCaseProvider(Delete<%= className %>UseCase, [<%= className %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/client';
import { <%= className %>Service } from '../services';
import {
Page,
UseCase,
<%= className %>,
QueryParams,
} from '@devmx/shared-api-interfaces';

export class Find<%= classNamePlural %>UseCase
implements UseCase<QueryParams<<%= className %>>, Page<<%= className %>>>
{
constructor(private <%= propertyName %>Service: <%= className %>Service) {}

execute(params: QueryParams<<%= className %>>) {
return this.<%= propertyName %>Service.find(params);
}
}

export function provideFind<%= classNamePlural %>UseCase() {
return createUseCaseProvider(Find<%= classNamePlural %>UseCase, [<%= className %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/client';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= className %>Service } from '../services';

export class Find<%= className %>ByIDUseCase
implements UseCase<string, <%= className %> | null>
{
constructor(private <%= propertyName %>Service: <%= className %>Service) {}

execute(id: string) {
return this.<%= propertyName %>Service.findOne(id);
}
}

export function provideFind<%= className %>ByIDUseCase() {
return createUseCaseProvider(Find<%= className %>ByIDUseCase, [<%= className %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { UseCase, <%= className %>, Editable<%= className %> } from '@devmx/shared-api-interfaces';
import { createUseCaseProvider } from '@devmx/shared-util-data/client';
import { <%= className %>Service } from '../services';

export class Update<%= className %>UseCase implements UseCase<Editable<%= className %>, <%= className %>> {
constructor(private <%= propertyName %>Service: <%= className %>Service) {}

execute(data: Editable<%= className %>) {
return this.<%= propertyName %>Service.update(data.id, data);
}
}

export function provideUpdate<%= className %>UseCase() {
return createUseCaseProvider(Update<%= className %>UseCase, [<%= className %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { UseCase, <%= className %>, Editable<%= className %> } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

export class Create<%= className %>UseCase implements UseCase<Editable<%= className %>, <%= className %>> {
constructor(private <%= propertyNamePlural %>Service: <%= classNamePlural %>Service) {}

execute(data: Editable<%= className %>) {
return this.<%= propertyNamePlural %>Service.create(data);
}
}

export function provideCreate<%= className %>UseCase() {
return createUseCaseProvider(Create<%= className %>UseCase, [<%= classNamePlural %>Service]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

export class Delete<%= className %>UseCase implements UseCase<string, <%= className %>> {
constructor(private <%= propertyNamePlural %>Service: <%= classNamePlural %>Service) {}

async execute(id: string) {
return this.<%= propertyNamePlural %>Service.delete(id);
}
}

export function provideDelete<%= className %>UseCase() {
return createUseCaseProvider(Delete<%= className %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= classNamePlural %>Service } from '../services';
import {
Page,
UseCase,
<%= className %>,
QueryParams,
} from '@devmx/shared-api-interfaces';

export class Find<%= classNamePlural %>UseCase
implements UseCase<QueryParams<<%= className %>>, Page<<%= className %>>>
{
constructor(private <%= propertyNamePlural %>Service: <%= classNamePlural %>Service) {}

async execute(params: QueryParams<<%= className %>>) {
return this.<%= propertyNamePlural %>Service.find(params);
}
}

export function provideFind<%= classNamePlural %>UseCase() {
return createUseCaseProvider(Find<%= classNamePlural %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

export class Find<%= className %>ByIDUseCase implements UseCase<string, <%= className %> | null> {
constructor(private <%= propertyNamePlural %>Service: <%= classNamePlural %>Service) {}

async execute(id: string) {
return this.<%= propertyNamePlural %>Service.findOne(id);
}
}

export function provideFind<%= className %>ByIDUseCase() {
return createUseCaseProvider(Find<%= className %>ByIDUseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { UseCase, <%= className %>, Editable<%= className %> } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

export class Update<%= className %>UseCase implements UseCase<Editable<%= className %>, <%= className %>> {
constructor(private <%= propertyNamePlural %>Service: <%= classNamePlural %>Service) {}

async execute(data: Editable<%= className %>) {
return this.<%= propertyNamePlural %>Service.update(data.id, data);
}
}

export function provideUpdate<%= className %>UseCase() {
return createUseCaseProvider(Update<%= className %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const variable = "<%= name %>";
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { UseCase, <%= className %>, Editable<%= className %> } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

Expand All @@ -8,3 +9,7 @@ export class Create<%= className %>UseCase implements UseCase<Editable<%= classN
return this.<%= propertyNamePlural %>Service.create(data);
}
}

export function provideCreate<%= className %>UseCase() {
return createUseCaseProvider(Create<%= className %>UseCase, [<%= classNamePlural %>Service]);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

Expand All @@ -8,3 +9,7 @@ export class Delete<%= className %>UseCase implements UseCase<string, <%= classN
return this.<%= propertyNamePlural %>Service.delete(id);
}
}

export function provideDelete<%= className %>UseCase() {
return createUseCaseProvider(Delete<%= className %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= classNamePlural %>Service } from '../services';
import {
Page,
Expand All @@ -15,3 +16,7 @@ export class Find<%= classNamePlural %>UseCase
return this.<%= propertyNamePlural %>Service.find(params);
}
}

export function provideFind<%= classNamePlural %>UseCase() {
return createUseCaseProvider(Find<%= classNamePlural %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { <%= className %>, UseCase } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

Expand All @@ -8,3 +9,7 @@ export class Find<%= className %>ByIDUseCase implements UseCase<string, <%= clas
return this.<%= propertyNamePlural %>Service.findOne(id);
}
}

export function provideFind<%= className %>ByIDUseCase() {
return createUseCaseProvider(Find<%= className %>ByIDUseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createUseCaseProvider } from '@devmx/shared-util-data/server';
import { UseCase, <%= className %>, Editable<%= className %> } from '@devmx/shared-api-interfaces';
import { <%= classNamePlural %>Service } from '../services';

Expand All @@ -8,3 +9,7 @@ export class Update<%= className %>UseCase implements UseCase<Editable<%= classN
return this.<%= propertyNamePlural %>Service.update(data.id, data);
}
}

export function provideUpdate<%= className %>UseCase() {
return createUseCaseProvider(Update<%= className %>UseCase, [<%= classNamePlural %>Service])
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import { pluralize } from '../../../utils';
import { names } from '@nx/devkit';
import { getAllNames } from '../../../utils';
import {
EntityGeneratorSchema,
NormalizedEntityGeneratorSchema,
} from '../schema';

function getAllNames(original: string) {
const normalized = names(original);
const pluralNames = {
namePlural: pluralize(normalized.name, 2),
classNamePlural: pluralize(normalized.className, 2),
fileNamePlural: pluralize(normalized.fileName, 2),
propertyNamePlural: pluralize(normalized.propertyName, 2),
constantNamePlural: pluralize(normalized.constantName, 2),
};
return { ...normalized, ...pluralNames };
}

export function normalizeOptions(
options: EntityGeneratorSchema
): NormalizedEntityGeneratorSchema {
Expand Down
19 changes: 19 additions & 0 deletions tools/plugin/dx-dev/src/generators/use-cases/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Tree, formatFiles, generateFiles } from '@nx/devkit';
import { UseCasesGeneratorSchema } from './schema';
import { normalizeOptions } from './lib';
import { join } from 'path';

export async function useCasesGenerator(
tree: Tree,
options: UseCasesGeneratorSchema
) {
const normalizedOptions = normalizeOptions(options);

const srcFolder = join(__dirname, '..', '..', 'files', 'use-cases');

generateFiles(tree, srcFolder, 'packages', normalizedOptions);

await formatFiles(tree);
}

export default useCasesGenerator;
1 change: 1 addition & 0 deletions tools/plugin/dx-dev/src/generators/use-cases/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './normalize-options';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getAllNames } from '../../../utils';
import {
UseCasesGeneratorSchema,
NormalizedUseCasesGeneratorSchema,
} from '../schema';

export function normalizeOptions(
options: UseCasesGeneratorSchema
): NormalizedUseCasesGeneratorSchema {
const allNames = getAllNames(options.name);
return { ...options, ...allNames };
}
17 changes: 17 additions & 0 deletions tools/plugin/dx-dev/src/generators/use-cases/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface UseCasesGeneratorSchema {
name: string;
scope: string;
}

export interface NormalizedUseCasesGeneratorSchema
extends EntityGeneratorSchema {
constantName: string;
propertyName: string;
className: string;
fileName: string;
namePlural: string;
classNamePlural: string;
fileNamePlural: string;
propertyNamePlural: string;
constantNamePlural: string;
}
23 changes: 23 additions & 0 deletions tools/plugin/dx-dev/src/generators/use-cases/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "https://json-schema.org/schema",
"$id": "UseCases",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"scope": {
"type": "string",
"description": ""
},
"x-prompt": "What scope would you like to place files?"
},
"required": ["name", "scope"]
}
14 changes: 14 additions & 0 deletions tools/plugin/dx-dev/src/utils/get-all-names.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { pluralize } from './pluralize';
import { names } from '@nx/devkit';

export function getAllNames(original: string) {
const normalized = names(original);
const pluralNames = {
namePlural: pluralize(normalized.name, 2),
classNamePlural: pluralize(normalized.className, 2),
fileNamePlural: pluralize(normalized.fileName, 2),
propertyNamePlural: pluralize(normalized.propertyName, 2),
constantNamePlural: pluralize(normalized.constantName, 2),
};
return { ...normalized, ...pluralNames };
}
3 changes: 2 additions & 1 deletion tools/plugin/dx-dev/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './pluralize';
export * from './get-all-names';
export * from './pluralize';

0 comments on commit 965a679

Please sign in to comment.