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

simplify update_template #24927

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,22 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
<%_
const mapper = entityInstance + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDto = mapper + '.' + 'toDto';
const returnPrefix = (returnDirectly && !searchEngineElasticsearch) ? 'return' : restClass + ' result =';
let resultEntity;
_%>
<%_ if (dtoMapstruct) { _%>
<%_ resultEntity = persistInstance; _%>
<%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= restInstance %>);
<%_ if (isUsingMapsId) { _%>
<%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= mapsIdAssoc.otherEntityName %>Id = <%= restInstance %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdAssoc.otherEntity.entityInstance %>Repository.findById(<%= mapsIdAssoc.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 %> <%= mapsIdAssoc.otherEntityName %>Id = <%= restInstance %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
<%= mapsIdAssoc.otherEntity.entityInstance %>Repository.findById(<%= mapsIdAssoc.otherEntityName %>Id).ifPresent(<%= restInstance %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.otherEntityName %><%_ } _%>);
<%_ } _%>
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%- persistClass %> <%- persistInstance %> = <%- entityInstance %>Mapper.toEntity(<%- restInstance %>);
<%_ } _%>
<%_ if (!saveNewEntityOperation && requiresPersistableImplementation) { _%>
<%- persistInstance %>.setIsPersisted();
<%_ } _%>
<%_ if (saveNewEntityOperation && 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(<%= resultEntity %>);
<%_ if (returnDirectly) { _%>
return result;
<%_ } _%>
<%- entityInstance %>SearchRepository.index(<%- persistInstance %>);
<%_ } _%>
<%_ if (dtoMapstruct) { _%>
<%- returnDirectly ? 'return' : `${dtoInstance} =` %> <%- entityInstance %>Mapper.toDto(<%- persistInstance %>);
<%_ } else if (returnDirectly && searchEngineElasticsearch) { _%>
return <%- entityInstance %>;
<%_ } _%>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
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 entityToDtoReference = mapper + '::' + 'toDto';
_%>
return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= instanceName %><%if (dtoMapstruct) { %>)<% } -%>)
return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= restInstance %><%if (dtoMapstruct) { %>)<% } -%><% if (!saveNewEntityOperation && requiresPersistableImplementation) { %>.setIsPersisted()<% } %>)
<%_ if (searchEngineElasticsearch) { %>
.flatMap(<%= entityInstance %>SearchRepository::save)
<%_ } -%>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class <%= serviceClassName %><% if (serviceImpl) { %> implements <%= enti
<%_ } _%>
public <% if (reactive) { %>Mono<<% } %><%= instanceType %><% if (reactive) { %>><% } %> save(<%= instanceType %> <%= instanceName %>) {
log.debug("Request to save <%= entityClass %> : {}", <%= instanceName %>);
<%- include('/_global_partials_entity_/save_template', {viaService: false, returnDirectly: true, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: false, isPersisted: false}); -%>
<%- include('/_global_partials_entity_/save_template', {saveNewEntityOperation: true, returnDirectly: true}); -%>
}
<%_ if (updatableEntity) { _%>

Expand All @@ -128,7 +128,7 @@ public class <%= serviceClassName %><% if (serviceImpl) { %> implements <%= enti
<%_ } _%>
public <% if (reactive) { %>Mono<<% } %><%= instanceType %><% if (reactive) { %>><% } %> update(<%= instanceType %> <%= instanceName %>) {
log.debug("Request to update <%= entityClass %> : {}", <%= instanceName %>);
<%- include('/_global_partials_entity_/update_template', {viaService: false, returnDirectly: true, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: false, isPersisted: requiresPersistableImplementation}); -%>
<%- include('/_global_partials_entity_/save_template', {saveNewEntityOperation: false, returnDirectly: true}); -%>
}

<%_ if (!serviceImpl) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ public class <%= entityClass %>Resource {
<%_ } _%>
<%_ } _%>
<%_ if (serviceNo) { _%>
<%- include('/_global_partials_entity_/save_template', {returnDirectly: false, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc}); -%>
<%- include('/_global_partials_entity_/save_template', {saveNewEntityOperation: true, returnDirectly: false}); -%>
<%_ } _%>
<%_ if (reactive) { _%>
<%_ if (!serviceNo) { _%>
return <%= entityInstance %>Service.save(<%= instanceName %>)
return <%= entityInstance %>Service
.save(<%= instanceName %>)
<%_ } _%>
.map(result -> {
try {
Expand All @@ -212,11 +213,11 @@ public class <%= entityClass %>Resource {
});
<%_ } else { _%>
<%_ if (!serviceNo) { _%>
<%- restClass %> result = <%= entityInstance %>Service.save(<%= instanceName %>);
<%- restInstance %> = <%= entityInstance %>Service.save(<%= instanceName %>);
<%_ } _%>
return ResponseEntity.created(new URI("/api/<%= entityApiUrl %>/" + result.get<%= primaryKey.nameCapitalized %>()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, result.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(result);
return ResponseEntity.created(new URI("/api/<%= entityApiUrl %>/" + <%- restInstance %>.get<%= primaryKey.nameCapitalized %>()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, <%- restInstance %>.get<%= primaryKey.nameCapitalized %>()<% if (!primaryKey.typeString) { %>.toString()<% } %>))
.body(<%- restInstance %>);
<%_ } _%>
}
<%_ } _%>
Expand All @@ -235,7 +236,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 @@ -271,18 +272,27 @@ public class <%= entityClass %>Resource {
<%_ } _%>
<% } %>
<%_ } _%>
<%- include('/_global_partials_entity_/update_template', {viaService: viaService, returnDirectly: false, isUsingMapsId: false, mapsIdAssoc: mapsIdAssoc, isController: true, isPersisted: requiresPersistableImplementation}); -%>
<%_ if (serviceNo) { _%>
<%- include('/_global_partials_entity_/save_template', {saveNewEntityOperation: false, returnDirectly: false}); -%>
<%_ } _%>
<%_ if (reactive) { _%>
.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)
);
<%_ if (!serviceNo) { _%>
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)
);
});
<%_ } else { _%>
<%_ if (!serviceNo) { _%>
<%- 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
Loading