Skip to content

Commit

Permalink
Adapt Angular entity model file template to be consistent across fram…
Browse files Browse the repository at this point in the history
…eworks
  • Loading branch information
RamiSouai committed Jul 26, 2023
1 parent 4360bae commit 0bed762
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
32 changes: 32 additions & 0 deletions generators/angular/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { isFilePending } from 'mem-fs-editor/state';

import BaseApplicationGenerator, { type Entity } from '../base-application/index.mjs';
import { GENERATOR_ANGULAR, GENERATOR_CLIENT, GENERATOR_LANGUAGES } from '../generator-list.mjs';
import { CommonDBTypes } from '../../jdl/jhipster/field-types.js';
import { defaultLanguage } from '../languages/support/index.mjs';
import { writeEntitiesFiles, postWriteEntitiesFiles, cleanupEntitiesFiles } from './entity-files-angular.mjs';
import { writeFiles } from './files-angular.mjs';
Expand All @@ -40,9 +41,12 @@ import {
generateTestEntityId as getTestEntityId,
generateTestEntityPrimaryKey as getTestEntityPrimaryKey,
generateTypescriptTestEntity as generateTestEntity,
generateEntityClientFields as getHydratedEntityClientFields,
generateEntityClientImports as formatEntityClientImports,
} from '../client/support/index.mjs';
import type { CommonClientServerApplication } from '../base-application/types.mjs';

const TYPE_BOOLEAN = CommonDBTypes.BOOLEAN;
const { ANGULAR } = clientFrameworkTypes;

export default class AngularGenerator extends BaseApplicationGenerator {
Expand Down Expand Up @@ -331,6 +335,34 @@ export default class AngularGenerator extends BaseApplicationGenerator {
return getTestEntityId(primaryKey, index, wrapped);
}

/**
* @private
* Generate Entity Client Field Default Values
*
* @param {Array|Object} fields - array of fields
* @returns {Array} defaultVariablesValues
*/
generateEntityClientFieldDefaultValues(fields) {
const defaultVariablesValues = {};
fields.forEach(field => {
const fieldType = field.fieldType;
const fieldName = field.fieldName;
if (fieldType === TYPE_BOOLEAN) {
defaultVariablesValues[fieldName] = `${fieldName}: false,`;
}
});
return defaultVariablesValues;
}

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);
}


/**
* @private
* Generate a test entity, for the PK references (when the PK is a composite, derived key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,34 @@
limitations under the License.
-%>
<%
const enumImports = this.generateEntityClientEnumImports(fields);
const enumImports = this.generateEntityClientEnumImports(fields.filter(field => !field.id));
const variablesWithTypes = this.generateEntityClientFields(primaryKey, fields.filter(field => !field.id), relationships, dto, customDateType = 'Date', embedded);
const typeImports = this.generateEntityClientImports(relationships, dto);
const defaultVariablesValues = this.generateEntityClientFieldDefaultValues(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.ownerSide || relationship.relationshipManyToMany) && 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(relationship => relationship.ownerSide || relationship.relationshipManyToMany)) {
const { propertyName, relationshipRequired, otherEntity, id, collection, otherEntityField } = relationship;
_%>
<%= propertyName %>?: Pick<I<%= otherEntity.entityAngularName %>, '<%= otherEntity.primaryKey.name %>'<% if (otherEntity.primaryKey.name !== otherEntityField) { %> | '<%= otherEntityField %>'<% } %>><% if (collection) { %>[]<% } %> | null;
<%_ } _%>
<%_ variablesWithTypes.forEach(variablesWithType => { _%>
<%- variablesWithType %>;
<%_ }); _%>
}
<%_ if (primaryKey) { _%>

export type New<%= entityAngularName %> = Omit<I<%= entityAngularName %>, '<%= primaryKey.name %>'> & { <%= primaryKey.name %>: null };
<%_ } _%>
export class <%= entityAngularName %> implements I<%= entityAngularName %> {
constructor(<% variablesWithTypes.forEach(variablesWithType => { %>
public <%- variablesWithType %>,<% }); %>
) {<% for (idx in defaultVariablesValues) { %>
<%- defaultVariablesValues[idx] %><% } %>
}
}

0 comments on commit 0bed762

Please sign in to comment.