Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor angular entity template #25067

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion generators/angular/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import {
addIconImport,
} from './support/index.js';
import {
generateEntityClientImports as formatEntityClientImports,
generateTypescriptTestEntity as generateTestEntity,
generateEntityClientFields as getHydratedEntityClientFields,
generateEntityClientEnumImports as getClientEnumImportsFormat,
getTypescriptKeyType as getTSKeyType,
generateTestEntityId as getTestEntityId,
generateTestEntityPrimaryKey as getTestEntityPrimaryKey,
generateTypescriptTestEntity as generateTestEntity,
} from '../client/support/index.js';
import type { CommonClientServerApplication } from '../base-application/types.js';
import { createNeedleCallback, mutateData } from '../base/support/index.js';
Expand Down Expand Up @@ -348,6 +350,14 @@ export default class AngularGenerator extends BaseApplicationGenerator {
this.needleApi.clientAngular.addGlobalSCSSStyle(style, comment);
}

generateEntityClientFields(primaryKey, fields, relationships, dto, customDateType = 'dayjs.Dayjs', embedded = false) {
return getHydratedEntityClientFields(primaryKey, fields, relationships, dto, customDateType, embedded, ANGULAR);
}

generateEntityClientImports(relationships, dto) {
return formatEntityClientImports(relationships, dto, ANGULAR);
}

/**
* Returns the typescript import section of enums referenced by all fields of the entity.
* @param fields returns the import of enums that are referenced by the fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,26 @@
limitations under the License.
-%>
<%
const enumImports = this.generateEntityClientEnumImports(fields);
const variablesWithTypes = this.generateEntityClientFields(primaryKey, fields, relationships, dto, customDateType = 'dayjs.Dayjs', embedded);
const typeImports = this.generateEntityClientImports(relationships, dto);
const enumImports = this.generateEntityClientEnumImports(fields.filter(field => !field.id));
%>
<%_ if (anyFieldIsDateDerived) { _%>
import dayjs from 'dayjs/esm';
<%_ } _%>
<%_ for (const relationshipsByType of Object.values(differentRelationships).filter(relationshipsByType => relationshipsByType.some(relationship => relationship.otherEntity.entityAngularName !== entityAngularName))) {
const { otherEntity } = relationshipsByType[0];
_%>
import { I<%- otherEntity.entityAngularName %> } from 'app/entities/<%= otherEntity.entityClientRootFolder %><%= otherEntity.entityFolderName %>/<%= otherEntity.entityFileName %>.model';
<%_ } _%>
<%_ enumImports.forEach((importedPath, importedType) => { _%>
<%_ typeImports.forEach( (importedPath, importedType) => { _%>
<%_ if (importedType !== `I${entityReactName}`) { _%>
import { <%- importedType %> } from '<%- importedPath %>';
<%_ } _%>
<%_ }); _%>
<%_ enumImports.forEach( (importedPath, importedType) => { _%>
import { <%- importedType %> } from '<%- importedPath %>';
<%_ }); _%>

export interface I<%= entityAngularName %> {
<%_ for (const field of fields) {
const { fieldName, fieldValidationRequired, id } = field;
const tsType = `${field.fieldIsEnum ? 'keyof typeof ' : ''}${field.tsType}`;
_%>
<%= fieldName %><% if (!id /* && !fieldValidationRequired */) { %>?<% } %>: <%= tsType %><% if (!id /* && !fieldValidationRequired */) { %> | null<% } %>;
<%_ if (field.fieldTypeBinary && !field.blobContentTypeText) { _%>
<%= fieldName %>ContentType<% if (!id /* && !fieldValidationRequired */) { %>?<% } %>: string<% if (!id /* && !fieldValidationRequired */) { %> | null<% } %>,
<%_ } _%>
<%_ } _%>
<%_ for (const relationship of relationships.filter(rel => rel.persistableRelationship || rel.relationshipEagerLoad)) {
const { propertyName, relationshipRequired, otherEntity, collection, otherEntityField } = relationship;
_%>
<%= propertyName %>?: <% if (!otherEntity.embedded && (dtoMapstruct || otherEntity.builtInUser)) { %>Pick<<% } %>I<%= otherEntity.entityAngularName %><% if (!otherEntity.embedded && (dtoMapstruct || otherEntity.builtInUser)) { %>, '<%= otherEntity.primaryKey.name %>'<% if (otherEntity.primaryKey.name !== otherEntityField) { %> | '<%= otherEntityField %>'<% } %>><% } %><% if (collection) { %>[]<% } %> | null;
<%_ } _%>
<%_ variablesWithTypes.forEach(variablesWithType => { _%>
<%- variablesWithType %>;
<%_ }); _%>
}
<%_ if (!readOnly && primaryKey) { _%>

Expand Down
5 changes: 4 additions & 1 deletion generators/client/support/entity-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ const generateEntityClientFields = (
if (nullable) {
tsType += ' | null';
}
variablesWithTypes.push(`${fieldName}?: ${tsType}`);

const required = clientFramework === ANGULAR && field.id;

variablesWithTypes.push(`${fieldName}${required ? '' : '?'}: ${tsType}`);
});

const relevantRelationships = filterRelevantRelationships(relationships);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const typeImports = this.generateEntityClientImports(relationships, dto);
const defaultVariablesValues = this.generateEntityClientFieldDefaultValues(fields.filter(field => !field.id));
const enumImports = this.generateEntityClientEnumImports(fields.filter(field => !field.id));
%>
<%_ if (anyFieldIsInstant || anyFieldIsZonedDateTime || anyFieldIsLocalDate) { _%>
<%_ if (anyFieldIsDateDerived) { _%>
import dayjs from 'dayjs';
<%_ } _%>
<%_ typeImports.forEach( (importedPath, importedType) => { _%>
Expand Down