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 3 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,53 +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 (!viaService) { _%>
<%_ if (dtoMapstruct) { _%>
<%_ resultEntity = persistInstance; _%>
<%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>);
<%_ if (isPersisted) { _%>
<%= 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 (isPersisted) { _%>
<%= persistInstance %>.setIsPersisted();
<%_ } _%>
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
<%= entityInstance %>SearchRepository.index(<%= resultEntity %>);
<%_ if (returnDirectly) { _%>
return result;
<%_ } _%>
<%_ } _%>
<%_ } else { _%>
<%= returnPrefix %> <%= entityInstance %>Service.update(<%= instanceName %>);
<%_ if (dtoMapstruct) { _%>
<%- 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 && searchEngineElasticsearch) { _%>
return <%- entityInstance %>;
<%_ } _%>
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@
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';
_%>
<%_ if (viaService) { _%>
return <%= entityInstance %>Service.update(<%= instanceName %>)
<%_ } else { _%>
return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= instanceName %><%if (dtoMapstruct) { %>)<% } -%><% if (isPersisted) { %>.setIsPersisted()<% } %>)
<%_ if (searchEngineElasticsearch) { %>
return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= restInstance %><%if (dtoMapstruct) { %>)<% } -%><% if (requiresPersistableImplementation) { %>.setIsPersisted()<% } %>)
<%_ if (searchEngineElasticsearch) { %>
.flatMap(<%= entityInstance %>SearchRepository::save)
<%_ } -%>
<%_ if (dtoMapstruct) { %>
<%_ } -%>
<%_ if (dtoMapstruct) { %>
.map(<%= entityToDtoReference %>)
<%_ } -%>
<%_ if (returnDirectly) { _%>;<%_ } _%>
<%_ } _%>
<%_ } -%>
<%_ if (returnDirectly) { _%>;<%_ } _%>
Original file line number Diff line number Diff line change
Expand Up @@ -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_/update_template', {returnDirectly: true, mapsIdAssoc: mapsIdAssoc}); -%>
}

<%_ if (!serviceImpl) { _%>
Expand Down
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 @@ -258,18 +258,26 @@ 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_/update_template', {returnDirectly: false, isUsingMapsId: 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