Skip to content

Commit

Permalink
add firstName and lastName to UserDTO and IUser (#14229)
Browse files Browse the repository at this point in the history
* add firstName and lastName to UserDTO and IUser

* additional fields generated by demand

* Set relatedByRelationship at fields used as label.

* Add builtIn to implemented user fields by default.

* improved users fields not builtin

* remove extra space

Co-authored-by: Marcelo Shima <marceloshima@gmail.com>
  • Loading branch information
VergilSkye and mshima authored Mar 11, 2021
1 parent ce8bb19 commit ac70eb6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
16 changes: 16 additions & 0 deletions generators/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,28 @@ module.exports = class extends BaseGenerator {
fieldTranslationKey: 'global.field.id',
fieldNameHumanized: 'ID',
id: true,
builtIn: true,
});

if (!user.fields.some(field => field.fieldName === 'login')) {
user.fields.push({
fieldName: 'login',
fieldType: TYPE_STRING,
builtIn: true,
});
}

if (!user.fields.some(field => field.fieldName === 'firstName')) {
user.fields.push({
fieldName: 'firstName',
fieldType: TYPE_STRING,
});
}

if (!user.fields.some(field => field.fieldName === 'lastName')) {
user.fields.push({
fieldName: 'lastName',
fieldType: TYPE_STRING,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ _%>
export interface IUser {
id?: <%= idType %>;
login?: string;
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
<%= field.fieldName %>?: string;
<%_ } _%>
}

export class User implements IUser {
constructor(
public id: <%= idType %>,
public login: string,
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
public <%= field.fieldName %>?: string,
<%_ } _%>
) {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,22 @@ public class <%= asDto('User') %> {
private String login;
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
private String <%= field.fieldName %>;
<%_ } _%>
<%_ if (databaseType !== 'no') { _%>
public <%= asDto('User') %>() {
// Empty constructor needed for Jackson.
}
public <%= asDto('User') %>(<%= asEntity('User') %> user) {
this.id = user.getId();
// Customize it here if you need firstName/lastName/etc
// Customize it here if you need, or not, firstName/lastName/etc
this.login = user.getLogin();
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
this.<%= field.fieldName %> = user.get<%= javaBeanCase(field.fieldName) %>();
<%_ } _%>
}
<%_ } _%>
Expand All @@ -63,12 +70,24 @@ public class <%= asDto('User') %> {
this.login = login;
}
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
public String get<%= javaBeanCase(field.fieldName) %>() {
return <%= field.fieldName %>;
}
public void set<%= javaBeanCase(field.fieldName) %>(String <%= field.fieldName %>) {
this.<%= field.fieldName %> = <%= field.fieldName %>;
}
<%_ } _%>
// prettier-ignore
@Override
public String toString() {
return "<%= asDto('User') %>{" +
"id='" + id + '\'' +
", login='" + login + '\'' +
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
", <%= field.fieldName %>='" + <%= field.fieldName %> + '\'' +
<%_ } _%>
"}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ class PublicUserResourceIT <% if (databaseType === 'cassandra') { %>extends Abst
private static final String DEFAULT_LOGIN = "johndoe";
<%_ const DEFAULT_USER = {firstName: 'john', lastName: 'doe'}; _%>
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
private static final String DEFAULT_<%= field.fieldName.toUpperCase() %> = "<%= DEFAULT_USER [field.fieldName] %>";
<%_ } _%>
@Autowired
private UserRepository userRepository;
<%_ if (searchEngine === 'elasticsearch') { _%>
Expand Down Expand Up @@ -180,6 +185,9 @@ class PublicUserResourceIT <% if (databaseType === 'cassandra') { %>extends Abst
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
.andExpect(jsonPath("$.[*].<%= field.fieldName %>").value(hasItem(DEFAULT_<%= field.fieldName.toUpperCase() %>)))
<%_ } _%>
.andExpect(jsonPath("$.[*].email").doesNotExist())
<%_ if (databaseType !== 'cassandra') { _%>
.andExpect(jsonPath("$.[*].imageUrl").doesNotExist())
Expand All @@ -194,6 +202,9 @@ class PublicUserResourceIT <% if (databaseType === 'cassandra') { %>extends Abst
.returnResult(<%= asDto('User') %>.class).getResponseBody().blockFirst();
assertThat(foundUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
<%_ for (field of user.fields.filter(field => !field.builtIn && field.relatedByOtherEntity)) { _%>
assertThat(foundUser.get<%= javaBeanCase(field.fieldName) %>()).isEqualTo(DEFAULT_<%= field.fieldName.toUpperCase() %>);
<%_ } _%>
<% } _%>
}
Expand Down
1 change: 1 addition & 0 deletions utils/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function prepareRelationshipForTemplates(entityWithConfig, relationship, generat
}
if (relationship.relatedField) {
relationship.otherEntityFieldCapitalized = relationship.relatedField.fieldNameCapitalized;
relationship.relatedField.relatedByOtherEntity = true;
} else {
relationship.otherEntityFieldCapitalized = _.upperFirst(relationship.otherEntityField);
}
Expand Down

0 comments on commit ac70eb6

Please sign in to comment.