Skip to content

Commit

Permalink
Merge pull request jhipster#24913 from mshima/updatable-entity
Browse files Browse the repository at this point in the history
rework non updatable entities
  • Loading branch information
DanielFran authored Jan 20, 2024
2 parents 2778b84 + b2b2726 commit 4a3a03e
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const <%= entityInstance %>Route: Routes = [
},
canActivate: [UserRouteAccessService]
},
<%_ } _%>
<%_ if (!readOnly && updatableEntity) { _%>
{
path: ':<%= primaryKey.name %>/edit',
component: <%= entityAngularName %>UpdateComponent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ _%>
data-cy="entityDetailsBackButton">
<fa-icon icon="arrow-left"></fa-icon>&nbsp;<span <%= jhiPrefix %>Translate="entity.action.back">__jhiTransformTranslate__('entity.action.back')</span>
</button>
<%_ if (!readOnly) { _%>
<%_ if (!readOnly && updatableEntity) { _%>
<button type="button"
[routerLink]="['/<%= entityPage %>', <%= entityInstance %>.<%= primaryKey.name %>, 'edit']"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ _%>
<fa-icon icon="eye"></fa-icon>
<span class="d-none d-md-inline" <%= jhiPrefix %>Translate="entity.action.view">__jhiTransformTranslate__('entity.action.view')</span>
</a>
<%_ if (!readOnly) { _%>
<%_ if (!readOnly && updatableEntity) { _%>
<a [routerLink]="['/<%= entityPage %>', <%= entityInstance %>.<%= primaryKey.name %>, 'edit']"
class="btn btn-primary btn-sm" data-cy="entityEditButton">
<fa-icon icon="pencil-alt"></fa-icon>
<span class="d-none d-md-inline" <%= jhiPrefix %>Translate="entity.action.edit">__jhiTransformTranslate__('entity.action.edit')</span>
</a>
<%_ } _%>
<%_ if (!readOnly) { _%>
<button type="submit" (click)="delete(<%= entityInstance %>)"
class="btn btn-danger btn-sm"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ describe('<%= entityAngularName %> Service', () => {
req.flush(returnedFromService);
expect(expectedResult).toMatchObject(expected);
});
<%_ } _%>
<%_ if (!readOnly && updatableEntity) { _%>

it('should update a <%= entityAngularName %>', () => {
const <%= entityInstance %> = { ...sampleWithRequiredData };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import { createRequestOption } from 'app/core/request/request-util';
import { <%= searchType %> } from 'app/core/request/request.model';
<%_ } _%>
import { I<%= entityAngularName %><% if (!readOnly) { %>, New<%= entityAngularName %><% } %> } from '../<%= entityFileName %>.model';
<%_ if (!readOnly) { _%>
<%_ if (!readOnly && updatableEntity) { _%>

export type PartialUpdate<%= entityAngularName %> = Partial<I<%= entityAngularName %>> & Pick<I<%= entityAngularName %>, '<%= primaryKey.name %>'>;
<%_ } _%>
Expand All @@ -69,6 +69,8 @@ export type <%= entityRestName %> = RestOf<I<%= entityAngularName %>>;
<%_ if (!readOnly) { _%>

export type New<%= entityRestName %> = RestOf<New<%= entityAngularName %>>;
<%_ } _%>
<%_ if (!readOnly && updatableEntity) { _%>

export type PartialUpdate<%= entityRestName %> = RestOf<PartialUpdate<%= entityAngularName %>>;
<%_ } _%>
Expand Down Expand Up @@ -97,6 +99,8 @@ export class <%= entityAngularName %>Service {
{ observe: 'response' })
<% if (anyFieldIsDateDerived) { %>.pipe(map(res => this.convertResponseFromServer(res)))<% } %>;
}
<%_ } _%>
<%_ if (!readOnly && updatableEntity) { _%>

update(<%= entityInstance %>: I<%= entityAngularName %>): Observable<EntityResponseType> {
<%_ if (anyFieldIsDateDerived) { _%>
Expand Down Expand Up @@ -170,7 +174,7 @@ export class <%= entityAngularName %>Service {
}

<%_ if (anyFieldIsDateDerived) { _%>
protected convertDateFromClient<T extends I<%= entityAngularName %><% if (!readOnly) { %> | New<%= entityAngularName %> | PartialUpdate<%= entityAngularName %><% } %>>(<%= entityInstance %>: T): RestOf<T> {
protected convertDateFromClient<T extends I<%= entityAngularName %><% if (!readOnly) { %> | New<%= entityAngularName %><% } %><% if (!readOnly && updatableEntity) { %> | PartialUpdate<%= entityAngularName %><% } %>>(<%= entityInstance %>: T): RestOf<T> {
return {
...<%= entityInstance %>,
<%_ for (const field of fields) { _%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ _%>
});
}

get<%= entityAngularName %>(form: <%= entityAngularName %>FormGroup): I<%= entityAngularName %> | New<%= entityAngularName %> {
get<%= entityAngularName %>(form: <%= entityAngularName %>FormGroup):<% if (updatableEntity) { %> I<%= entityAngularName %> |<% } %> New<%= entityAngularName %> {
<%_ if (anyFieldIsTimeDerived) { _%>
return this.convert<%= entityAngularName %>RawValueTo<%= entityAngularName %>(form.getRawValue() as <%= entityAngularName %>FormRawValue | New<%= entityAngularName %>FormRawValue);
<%_ } else { _%>
return form.getRawValue() as I<%= entityAngularName %> | New<%= entityAngularName %>;
return form.getRawValue() as<% if (updatableEntity) { %> I<%= entityAngularName %> |<% } %> New<%= entityAngularName %>;
<%_ } _%>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
});

describe('save', () => {
<%_ if (updatableEntity) { _%>
it('Should call update service on save for existing entity', () => {
// GIVEN
const saveSubject = new Subject<HttpResponse<I<%= entityAngularName %>>>();
Expand All @@ -199,6 +200,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
expect(comp.isSaving).toEqual(false);
});

<%_ } _%>
it('Should call create service on save for new entity', () => {
// GIVEN
const saveSubject = new Subject<HttpResponse<I<%= entityAngularName %>>>();
Expand All @@ -221,6 +223,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
expect(comp.isSaving).toEqual(false);
expect(comp.previousState).toHaveBeenCalled();
});
<%_ if (updatableEntity) { _%>

it('Should set isSaving to false on error', () => {
// GIVEN
Expand All @@ -241,6 +244,7 @@ describe('<%= entityAngularName %> Management Update Component', () => {
expect(comp.isSaving).toEqual(false);
expect(comp.previousState).not.toHaveBeenCalled();
});
<%_ } _%>
});

<%_ const trackedRelationships = Object.values(differentRelationships).filter(arr => arr.some(rel => rel.persistableRelationship && rel.otherEntity.primaryKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,15 @@ _%>
save(): void {
this.isSaving = true;
const <%= entityInstance %> = this.<%= entityInstance %>FormService.get<%= entityAngularName %>(this.editForm);
<%_ if (updatableEntity) { _%>
if (<%= entityInstance %>.<%= primaryKey.name %> !== null) {
this.subscribeToSaveResponse(this.<%= entityInstance %>Service.update(<%= entityInstance %>));
} else {
<%_ } _%>
this.subscribeToSaveResponse(this.<%= entityInstance %>Service.create(<%= entityInstance %>));
<%_ if (updatableEntity) { _%>
}
<%_ } _%>
}

protected subscribeToSaveResponse(result: Observable<HttpResponse<I<%= entityAngularName %>>>): void {
Expand Down
3 changes: 2 additions & 1 deletion generators/base-application/support/prepare-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const BASE_TEMPLATE_DATA = {
microserviceName: undefined,

requiresPersistableImplementation: false,
updatableEntity: undefined,
anyFieldIsDateDerived: false,
anyFieldIsTimeDerived: false,
anyFieldIsInstant: false,
Expand Down Expand Up @@ -605,7 +606,7 @@ function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) {

entity.updatableEntity =
entity.fields.some(field => !field.id && !field.transient) ||
entity.relationships.some(relationship => !relationship.id && relationship.ownerSide);
entity.relationships.some(relationship => !relationship.id && relationship.persistableRelationship);

entity.allReferences
.filter(reference => reference.relationship && reference.relationship.relatedField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {
entityTableSelector,
entityDetailsButtonSelector,
entityDetailsBackButtonSelector,
<%_ if (!readOnly) { _%>
<%_ if (!readOnly) { _%>
entityCreateButtonSelector,
entityCreateSaveButtonSelector,
entityCreateCancelButtonSelector,
<%_ if (updatableEntity) { _%>
entityEditButtonSelector,
<%_ } _%>
entityDeleteButtonSelector,
entityConfirmDeleteButtonSelector,
<%_ } _%>
<%_ } _%>
} from '../../support/entity';
<%_

Expand Down Expand Up @@ -247,7 +249,7 @@ describe('<%= entityClass %> e2e test', () => {
cy.url().should('match', <%= entityInstance %>PageUrlPattern);
});
<%_ if (!readOnly) { _%>
<%_ if (!readOnly && updatableEntity) { _%>
it('edit button click should load edit <%= entityClass %> page and go back', () => {
cy.get(entityEditButtonSelector).first().click();
cy.getEntityCreateUpdateHeading('<%= entityClass %>');
Expand All @@ -269,6 +271,8 @@ describe('<%= entityClass %> e2e test', () => {
cy.url().should('match', <%= entityInstance %>PageUrlPattern);
});
<%_ } _%>
<%_ if (!readOnly) { _%>
it<%= skipCreateTest %>('last delete button click should delete instance of <%= entityClass %>', () => {
<%_ if (clientFrameworkReact) { _%>
cy.intercept('GET', '/<%= baseApi + entityApiUrl %>/*').as('dialogDeleteRequest');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ 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 %><%_ } _%>);
<%_ } _%>
<%_ if (updatableEntity) { _%>
<%= persistInstance %> = <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%_ } else { _%>
// no save call needed as we have no fields that can be updated
<%_ } _%>
<%= returnPrefix %> <%= entityToDto %>(<%= persistInstance %>);
<%_ } else {
resultEntity = 'result'; _%>
Expand All @@ -59,12 +55,7 @@ if (isUsingMapsId) {
<%_ if (isPersisted) { _%>
<%= persistInstance %>.setIsPersisted();
<%_ } _%>
<%_ if (updatableEntity) { _%>
<%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
<%_ } else { _%>
// no save call needed as we have no fields that can be updated
<%= returnPrefix %> <%= persistInstance %>;
<%_ } _%>
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>
<%= entityInstance %>SearchRepository.index(<%= resultEntity %>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ _%>
<%_ if (viaService) { _%>
return <%= entityInstance %>Service.update(<%= instanceName %>)
<%_ } else { _%>
<%_ if (updatableEntity) { _%>
return <%= entityInstance %>Repository.save(<%if (dtoMapstruct) { %><%= dtoToEntity %>(<% } %><%= instanceName %><%if (dtoMapstruct) { %>)<% } -%><% if (isPersisted) { %>.setIsPersisted()<% } %>)
<%_ } else { _%>
// no save call needed as we have no fields that can be updated
return <%= entityInstance %>Repository.findById(<%= instanceName %>.get<%= primaryKey.nameCapitalized %>())
<%_ } _%>
<%_ if (searchEngineElasticsearch) { %>
.flatMap(<%= entityInstance %>SearchRepository::save)
<%_ } -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ _%>
return existing<%= entityClass %>;
})
<%_ } %>
<% if (!updatableEntity) { %>// <%} %>.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save)
.<%= mapOrFlatMap %>(<%= entityInstance %>Repository::save)
<%_ if (searchEngineElasticsearch) { _%>
.<%= mapOrFlatMap %>(saved<%= entityClass %> -> {
<%_ if(reactive) { %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,38 +113,40 @@ public class <%= serviceClassName %><% if (serviceImpl) { %> implements <%= enti
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}); -%>
}
<%_ if (updatableEntity) { _%>
<%_ if (!serviceImpl) { _%>
<%_ if (!serviceImpl) { _%>
/**
* Update a <%= entityInstance %>.
*
* @param <%= instanceName %> the entity to save.
* @return the persisted entity.
*/
<%_ } _%>
<%_ if (serviceImpl) { _%>
<%_ } _%>
<%_ if (serviceImpl) { _%>
@Override
<%_ } _%>
<%_ } _%>
public <% if (reactive) { %>Mono<<% } %><%= instanceType %><% if (reactive) { %>><% } %> update(<%= instanceType %> <%= instanceName %>) {
log.debug("Request to update <%= entityClass %> : {}", <%= instanceName %>);
<%- include('/_global_partials_entity_/update_template', {updatableEntity, viaService: false, returnDirectly: true, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: false, isPersisted: requiresPersistableImplementation}); -%>
<%- include('/_global_partials_entity_/update_template', {viaService: false, returnDirectly: true, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: false, isPersisted: requiresPersistableImplementation}); -%>
}
<%_ if (!serviceImpl) { _%>
<%_ if (!serviceImpl) { _%>
/**
* Partially update a <%= entityInstance %>.
*
* @param <%= instanceName %> the entity to update partially.
* @return the persisted entity.
*/
<%_ } _%>
<%_ if (serviceImpl) { _%>
<%_ } _%>
<%_ if (serviceImpl) { _%>
@Override
<%_ } _%>
<%_ } _%>
public <% if (reactive) { %>Mono<% } else { %>Optional<% } %><<%= instanceType %>> partialUpdate(<%= instanceType %> <%= instanceName %>) {
log.debug("Request to partially update <%= entityClass %> : {}", <%= instanceName %>);
<%- include('../../_partials_entity_/patch_template', {isService: true, viaService: false}); -%>
}
<%_ } _%>
<%_ if (!serviceImpl) { _%>
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ public class <%= entityClass %>Resource {
.body(result);
<%_ } _%>
}
<%_ } _%>
<%_ if (!readOnly && updatableEntity) { _%>
/**
* {@code PUT /<%= entityApiUrl %>/:<%= primaryKey.name %>} : Updates an existing <%= entityInstance %>.
Expand Down Expand Up @@ -256,7 +258,7 @@ public class <%= entityClass %>Resource {
<%_ } _%>
<% } %>
<%_ } _%>
<%- include('/_global_partials_entity_/update_template', {updatableEntity, viaService: viaService, returnDirectly: false, isUsingMapsId: false, mapsIdAssoc: mapsIdAssoc, isController: true, isPersisted: requiresPersistableImplementation}); -%>
<%- include('/_global_partials_entity_/update_template', {viaService: viaService, returnDirectly: false, isUsingMapsId: false, mapsIdAssoc: mapsIdAssoc, isController: true, isPersisted: requiresPersistableImplementation}); -%>
<%_ if (reactive) { _%>
.switchIfEmpty(Mono.error(new ResponseStatusException(HttpStatus.NOT_FOUND)))
.map(result -> ResponseEntity.ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ _%>
.andExpect(status().isNotFound());
<%_ } _%>
}
<%_ if (!readOnly) { _%>
<%_ if (!readOnly && updatableEntity) { _%>

@Test<%= transactionalAnnotation %>
void putExisting<%= entityClass %>() throws Exception {
Expand Down Expand Up @@ -1752,6 +1752,8 @@ _%>
<%_ } _%>

}
<%_ } _%>
<%_ if (!readOnly) { _%>

@Test<%= transactionalAnnotation %>
void delete<%= entityClass %>() <% if (!reactive) { %>throws Exception <% } %>{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@ChangelogDate(20230100000000)
entity NonUpdatableEntity

0 comments on commit 4a3a03e

Please sign in to comment.