Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jan 22, 2024
1 parent 31b4d72 commit c299e25
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,22 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_ const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDto = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::' + 'toDto';
const returnPrefix = (returnDirectly && !searchEngineElasticsearch) ? 'return' : instanceType + ' result =';
let resultEntity;
let mapsIdEntityInstance;
let mapsIdRepoInstance;
let otherEntityName;
if (isUsingMapsId) {
mapsIdEntityInstance = mapsIdAssoc.otherEntityNameCapitalized.charAt(0).toLowerCase() + mapsIdAssoc.otherEntityNameCapitalized.slice(1);
mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`;
otherEntityName = mapsIdAssoc.otherEntityName;
} _%>
<%_ if (dtoMapstruct) { _%>
<%_ resultEntity = persistInstance; _%>
<%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>);
<%_ if (requiresPersistableImplementation) { _%>
<%= persistInstance %>.setIsPersisted();
<%_ } _%>
<%_ if (isUsingMapsId) { _%>
<%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= persistInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%>);
<%_ } _%>
<%= persistInstance %> = <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%= returnPrefix %> <%= entityToDto %>(<%= persistInstance %>);
<%_ } else {
resultEntity = 'result'; _%>
<%_ if (isUsingMapsId) { _%>
<%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>);
<%_ } _%>
<%_ if (requiresPersistableImplementation) { _%>
<%= persistInstance %>.setIsPersisted();
<%_ } _%>
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
<%= entityInstance %>SearchRepository.index(<%= resultEntity %>);
<%_ if (returnDirectly) { _%>
return result;
<%_ } _%>
<%_ } _%>
<%- persistClass %> <%- persistInstance %> = <%- entityInstance %>Mapper.toEntity(<%- restInstance %>);
<%_ } _%>
<%_ if (requiresPersistableImplementation) { _%>
<%- persistInstance %>.setIsPersisted();
<%_ } _%>
<%_ if (isUsingMapsId) { _%>
<%- mapsIdAssoc.otherEntity.primaryKey.type %> <%- mapsIdAssoc.otherEntityName %>Id = <%- persistInstance %>.get<%- mapsIdAssoc.relationshipNameCapitalized %>().get<%- mapsIdAssoc.otherEntity.primaryKey.nameCapitalized %>();
<%- mapsIdAssoc.otherEntity.entityInstance %>Repository.findById(<%- mapsIdAssoc.otherEntityName %>Id).ifPresent(<%- persistInstance %>::<%_ if (!fluentMethods) { _%>set<%- mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%- mapsIdAssoc.relationshipName %><%_ } _%>);
<%_ } _%>
<%- returnDirectly && !searchEngineElasticsearch && !dtoMapstruct ? 'return' : `${persistInstance} =` %> <%- entityInstance %>Repository.save(<%- persistInstance %>);
<%_ if (searchEngineElasticsearch) { _%>
<%- entityInstance %>SearchRepository.index(<%- persistInstance %>);
<%_ } _%>
<%_ if (dtoMapstruct) { _%>
<%- returnDirectly ? 'return' : `${dtoInstance} =` %> <%- entityInstance %>Mapper.toDto(<%- persistInstance %>);
<%_ } else if (returnDirectly) { _%>
return <%- entityInstance %>;
<%_ } _%>
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public class <%= entityClass %>Resource {
@PutMapping("/{<%= primaryKey.name %>}")
public <% if (reactive) { %>Mono<<% } %>ResponseEntity<<%= instanceType %>><% if (reactive) { %>><% } %> update<%= entityClass %>(
@PathVariable(value = "<%= primaryKey.name %>", required = false) final <%= primaryKey.type %> <%= primaryKey.name %>,
<% if (anyPropertyHasValidation) { %>@Valid <% } %>@RequestBody <%= instanceType %> <%= instanceName %>
<% if (anyPropertyHasValidation) { %>@Valid <% } %>@RequestBody <%= RestClass %> <%= restInstance %>
) throws URISyntaxException {
log.debug("REST request to update <%= entityClass %> : {}, {}", <%= primaryKey.name %>, <%= instanceName %>);
if (<%= instanceName %>.get<%= primaryKey.nameCapitalized %>() == null) {
Expand Down Expand Up @@ -263,21 +263,21 @@ public class <%= entityClass %>Resource {
<%_ } _%>
<%_ if (reactive) { _%>
<%_ if (!serviceNo) { _%>
return <%= entityInstance %>Service.update(<%= instanceName %>)
return <%= entityInstance %>Service.update(<%= restInstance %>)
<%_ } _%>
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
.map(result -> ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, result.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(result)
);
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
.map(result -> ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, result.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(result)
);
});
<%_ } else { _%>
<%_ if (!serviceNo) { _%>
<%- restClass %> result = <%= entityInstance %>Service.update(<%= instanceName %>);
<%- restInstance %> = <%= entityInstance %>Service.update(<%= restInstance %>);
<%_ } _%>
return ResponseEntity.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, <%= instanceName %>.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(result);
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, <%- restInstance %>.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(<%- restInstance %>);
<%_ } _%>
}

Expand Down

0 comments on commit c299e25

Please sign in to comment.