Skip to content

Commit

Permalink
fix problem with assigning to the current context in handlebars partials
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangsa committed Dec 16, 2024
1 parent b74a7b8 commit 35271c4
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
{{~assign 'entity' aggregateCommandsForMethod.entity }}
{{~assign '_entity' aggregateCommandsForMethod.entity }}
{{~assign 'hasNaturalId' (isTruthy method.naturalId) }}
{{~assign 'notHasNaturalId' (isFalsy hasNaturalId) }}
{{~#if (isCrudMethod 'create' method=method entity=entity )}}
log.debug("[CRUD] Request to save {{entity.className}}: {}", input);
var {{entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{entity.instanceName}} = {{entity.instanceName}}Repository.save({{entity.instanceName}});
{{~#if (isCrudMethod 'create' method=method entity=_entity )}}
log.debug("[CRUD] Request to save {{_entity.className}}: {}", input);
var {{_entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{_entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{_entity.instanceName}} = {{_entity.instanceName}}Repository.save({{_entity.instanceName}});
// TODO: may need to reload the entity to fetch relationships 'mapped by id'
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
{{~else if (isCrudMethod 'list' method=method entity=entity )}}
return {{wrapWithMapper _entity}};
{{~else if (isCrudMethod 'list' method=method entity=_entity )}}
{{~#if method.options.paginated}}
log.debug("[CRUD] Request list of {{entity.classNamePlural}}: {}", pageable);
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper entity}};
log.debug("[CRUD] Request list of {{_entity.classNamePlural}}: {}", pageable);
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper _entity}};
{{~else}}
log.debug("Request list of {{entity.classNamePlural}}");
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll();
return {{wrapWithMapper entity}};
log.debug("Request list of {{_entity.classNamePlural}}");
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll();
return {{wrapWithMapper _entity}};
{{~/if}}
{{~else if (isCrudMethod 'search' method=method entity=entity )}}
log.debug("[CRUD] Request to search {{entity.classNamePlural}}: {} - {}", input, pageable);
{{~else if (isCrudMethod 'search' method=method entity=_entity )}}
log.debug("[CRUD] Request to search {{_entity.classNamePlural}}: {} - {}", input, pageable);
// TODO implement this search by criteria
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to get {{entity.className}} : {}", id);
var {{entity.instanceName}} = {{entity.instanceName}}Repository.findById(id);
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=entity ) hasNaturalId)}}
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=_entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to get {{_entity.className}} : {}", id);
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.findById(id);
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=_entity ) hasNaturalId)}}
{{{logMethodCall method}}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature entity}}};
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'delete' method=method entity=entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to delete {{entity.className}} : {}", id);
{{entity.instanceName}}Repository.deleteById(id);
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature _entity}}};
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'delete' method=method entity=_entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to delete {{_entity.className}} : {}", id);
{{_entity.instanceName}}Repository.deleteById(id);
{{~> (partial '../withEvents')}}
{{~else if (and (isCrudMethod 'delete' method=method entity=entity ) hasNaturalId)}}
{{~else if (and (isCrudMethod 'delete' method=method entity=_entity ) hasNaturalId)}}
{{{logMethodCall method}}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature entity}}};
if({{entity.instanceName}}.isPresent()) {
{{entity.instanceName}}Repository.delete({{entity.instanceName}}.get());
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature _entity}}};
if({{_entity.instanceName}}.isPresent()) {
{{_entity.instanceName}}Repository.delete({{_entity.instanceName}}.get());
{{~> (partial '../withEvents')}}
}
{{~/if}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
{{~assign "entity" aggregateCommandsForMethod.entity}}
{{~assign "_entity" aggregateCommandsForMethod.entity}}
{{~assign "returnEntity" (methodReturnEntity method)}}
{{{logMethodCall method}}}
{{!-- @Async --}}
{{~#if method.options.async}}
{{#if method.returnType}}return CompletableFuture.completedFuture({{/if}} {{method.name}}Sync({{methodParametersCallSignature method}}){{#if method.returnType}}){{/if}};
{{!-- list search --}}
{{~else if (and entity method.returnType method.returnTypeIsArray)}}
{{~#if (eq entity.name returnEntity)}}
return {{entity.instanceName}}Repository.findAll({{#if method.options.paginated}}pageable{{/if}});
{{~else if (and _entity method.returnType method.returnTypeIsArray)}}
{{~#if (eq _entity.name returnEntity)}}
return {{_entity.instanceName}}Repository.findAll({{#if method.options.paginated}}pageable{{/if}});
{{~else}}
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll({{#if method.options.paginated}}pageable{{/if}});
return {{wrapWithMapper entity}};
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll({{#if method.options.paginated}}pageable{{/if}});
return {{wrapWithMapper _entity}};
{{~/if}}
{{!-- Optional<Entity> patch(id, Map) --}}
{{~else if (and entity method.options.patch method.paramId method.parameter method.returnType method.returnTypeIsOptional)}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{findById method}}}.map(existing{{entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{entity.className}}, {{{mapperInputCallSignature method.parameter}}});
{{~else if (and _entity method.options.patch method.paramId method.parameter method.returnType method.returnTypeIsOptional)}}
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{findById method}}}.map(existing{{_entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{_entity.className}}, {{{mapperInputCallSignature method.parameter}}});
})
.map({{entity.instanceName}}Repository::save)
{{~#unless (eq entity.name method.returnType)}}
.map({{_entity.instanceName}}Repository::save)
{{~#unless (eq _entity.name method.returnType)}}
.map({{asInstanceName service.name}}Mapper::as{{returnType}})
{{~/unless}}
;
{{~> (partial '../withEvents')}}
return {{entity.instanceName}};
return {{_entity.instanceName}};
{{!-- Optional<Entity> update(id, Entity) --}}
{{~else if (and entity method.paramId method.parameter method.returnType method.returnTypeIsOptional)}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{findById method}}}.map(existing{{entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{entity.className}}, {{{mapperInputCallSignature method.parameter}}});
{{~else if (and _entity method.paramId method.parameter method.returnType method.returnTypeIsOptional)}}
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{findById method}}}.map(existing{{_entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{_entity.className}}, {{{mapperInputCallSignature method.parameter}}});
})
.map({{entity.instanceName}}Repository::save)
{{~#unless (eq entity.name method.returnType)}}
.map({{_entity.instanceName}}Repository::save)
{{~#unless (eq _entity.name method.returnType)}}
.map({{asInstanceName service.name}}Mapper::as{{returnType}})
{{~/unless}}
;
{{~> (partial '../withEvents')}}
return {{entity.instanceName}};
return {{_entity.instanceName}};
{{!-- Entity update(id, Entity) --}}
{{~else if (and entity method.paramId method.parameter method.returnType)}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{findById method}}}.map(existing{{entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{entity.className}}, {{{mapperInputCallSignature method.parameter}}});
{{~else if (and _entity method.paramId method.parameter method.returnType)}}
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{findById method}}}.map(existing{{_entity.className}} -> {
return {{asInstanceName service.name}}Mapper.update(existing{{_entity.className}}, {{{mapperInputCallSignature method.parameter}}});
})
.map({{entity.instanceName}}Repository::save)
.map({{_entity.instanceName}}Repository::save)
.orElseThrow();
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
return {{wrapWithMapper _entity}};
{{!-- Optional<Entity> get(id) --}}
{{~else if (and entity method.paramId method.returnType method.returnTypeIsOptional)}}
{{~assign 'needMapping' (not (eq entity.name method.returnType))}}
return {{entity.instanceName}}Repository.{{{findById method}}}{{#if needMapping}}.map({{asInstanceName service.name}}Mapper::as{{method.returnType}}){{/if}};
{{~else if (and _entity method.paramId method.returnType method.returnTypeIsOptional)}}
{{~assign 'needMapping' (not (eq _entity.name method.returnType))}}
return {{_entity.instanceName}}Repository.{{{findById method}}}{{#if needMapping}}.map({{asInstanceName service.name}}Mapper::as{{method.returnType}}){{/if}};
{{!-- Entity get(id) --}}
{{~else if (and entity method.paramId method.returnType)}}
return {{entity.instanceName}}Repository.{{{findById method}}};
{{~else if (and _entity method.paramId method.returnType)}}
return {{_entity.instanceName}}Repository.{{{findById method}}};
{{!-- Optional<Entity> get(MyEntity) --}}
{{~else if (and entity method.parameter method.returnType method.returnTypeIsOptional)}}
var {{entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{~else if (and _entity method.parameter method.returnType method.returnTypeIsOptional)}}
var {{_entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{_entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
// TODO: implement this method
{{~> (partial '../withEvents')}}
return Optional.ofNullable({{wrapWithMapper entity}});
return Optional.ofNullable({{wrapWithMapper _entity}});
{{!-- Optional<Entity> get(MyEntity) --}}
{{~else if (and entity method.parameter method.returnType)}}
var {{entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{~else if (and _entity method.parameter method.returnType)}}
var {{_entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{_entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
// TODO: implement this method
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
return {{wrapWithMapper _entity}};
{{!-- Optional<Entity> get() --}}
{{~else if (and entity method.returnType)}}
var {{entity.instanceName}} = new {{entity.className}}();
{{~else if (and _entity method.returnType)}}
var {{_entity.instanceName}} = new {{_entity.className}}();
// TODO: implement this method
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
return {{wrapWithMapper _entity}};
{{!-- void get() --}}
{{~else if (and entity)}}
var {{entity.instanceName}} = new {{entity.className}}();
{{~else if (and _entity)}}
var {{_entity.instanceName}} = new {{_entity.className}}();
// TODO: implement this method
{{~> (partial '../withEvents')}}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
{{assign 'entity' aggregateCommandsForMethod.entity }}
{{~assign '_entity' aggregateCommandsForMethod.entity }}
{{~assign 'hasNaturalId' (isTruthy method.naturalId) }}
{{~assign 'notHasNaturalId' (isFalsy hasNaturalId) }}
{{~#if (isCrudMethod 'create' method=method entity=entity )}}
log.debug("[CRUD] Request to save {{entity.className}}: {}", input);
var {{entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{entity.instanceName}} = {{entity.instanceName}}Repository.save({{entity.instanceName}});
{{~#if (isCrudMethod 'create' method=method entity=_entity )}}
log.debug("[CRUD] Request to save {{_entity.className}}: {}", input);
var {{_entity.instanceName}} = {{asInstanceName service.name}}Mapper.update(new {{_entity.className}}(), {{{mapperInputCallSignature method.parameter}}});
{{_entity.instanceName}} = {{_entity.instanceName}}Repository.save({{_entity.instanceName}});
// TODO: may need to reload the entity to fetch relationships 'mapped by id'
{{~> (partial '../withEvents')}}
return {{wrapWithMapper entity}};
{{~else if (isCrudMethod 'list' method=method entity=entity )}}
return {{wrapWithMapper _entity}};
{{~else if (isCrudMethod 'list' method=method entity=_entity )}}
{{~#if method.options.paginated}}
log.debug("[CRUD] Request list of {{entity.classNamePlural}}: {}", pageable);
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper entity}};
log.debug("[CRUD] Request list of {{_entity.classNamePlural}}: {}", pageable);
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper _entity}};
{{~else}}
log.debug("Request list of {{entity.classNamePlural}}");
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll();
return {{wrapWithMapper entity}};
log.debug("Request list of {{_entity.classNamePlural}}");
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll();
return {{wrapWithMapper _entity}};
{{~/if}}
{{~else if (isCrudMethod 'search' method=method entity=entity )}}
log.debug("[CRUD] Request to search {{entity.classNamePlural}}: {} - {}", input, pageable);
{{~else if (isCrudMethod 'search' method=method entity=_entity )}}
log.debug("[CRUD] Request to search {{_entity.classNamePlural}}: {} - {}", input, pageable);
// TODO implement this search by criteria
var {{entity.instanceNamePlural}} = {{entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to get {{entity.className}} : {}", id);
var {{entity.instanceName}} = {{entity.instanceName}}Repository.findById(id);
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=entity ) hasNaturalId)}}
var {{_entity.instanceNamePlural}} = {{_entity.instanceName}}Repository.findAll(pageable);
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=_entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to get {{_entity.className}} : {}", id);
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.findById(id);
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'get' method=method entity=_entity ) hasNaturalId)}}
{{{logMethodCall method}}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature entity}}};
return {{wrapWithMapper entity}};
{{~else if (and (isCrudMethod 'delete' method=method entity=entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to delete {{entity.className}} : {}", id);
{{entity.instanceName}}Repository.deleteById(id);
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature _entity}}};
return {{wrapWithMapper _entity}};
{{~else if (and (isCrudMethod 'delete' method=method entity=_entity ) notHasNaturalId)}}
log.debug("[CRUD] Request to delete {{_entity.className}} : {}", id);
{{_entity.instanceName}}Repository.deleteById(id);
{{~> (partial '../withEvents')}}
{{~else if (and (isCrudMethod 'delete' method=method entity=entity ) hasNaturalId)}}
{{~else if (and (isCrudMethod 'delete' method=method entity=_entity ) hasNaturalId)}}
{{{logMethodCall method}}}
var {{entity.instanceName}} = {{entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature entity}}};
if({{entity.instanceName}}.isPresent()) {
{{entity.instanceName}}Repository.delete({{entity.instanceName}}.get());
{{~> (partial '../withEvents')}}
var {{_entity.instanceName}} = {{_entity.instanceName}}Repository.{{{naturalIdsRepoMethodCallSignature _entity}}};
if({{_entity.instanceName}}.isPresent()) {
{{_entity.instanceName}}Repository.delete({{_entity.instanceName}}.get());
{{~> (partial '../withEvents')}}
}
{{~/if}}

Loading

0 comments on commit 35271c4

Please sign in to comment.