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

feat(repository): rework *ById methods to throw if id not found #1723

Merged
merged 2 commits into from
Sep 18, 2018

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented Sep 18, 2018

This is a follow-up for #1658 related to #1469.

Rework Repository interface and implementations to throw EntityNotFound error from updateById, replaceById and deleteById when there is no entity with the provided id.

Update related controller methods to return 204 No Content on success, because there is no boolean flag to indicate success anymore. Clients should check the response status code instead (204 vs 404).

Improve our REST layer to set the response status code to 204 when body is undefined.

This patch is reverting the implementation details of #1621 while keeping the observed REST API behavior.

See the discussion in #1658 and #1658 (comment) in particular to better understand the reasoning for this change. Also note that soon we will add a new Repository implementation that will provide *ById variant using a a special return value to indicate "not found" case - see #1735.

Checklist

  • npm test passes on your machine
  • New tests added or existing tests modified to cover all changes
  • Code conforms with the style guide
  • API Documentation in code was updated
  • Documentation in /docs/site was updated
  • Affected artifact templates in packages/cli were updated
  • Affected example projects in examples/* were updated

@bajtos bajtos added this to the September Milestone milestone Sep 18, 2018
@bajtos bajtos requested a review from raymondfeng as a code owner September 18, 2018 11:28
@bajtos bajtos force-pushed the fix/update-delete-not-found branch from 9bffc20 to ef8b9f1 Compare September 18, 2018 11:31
@bajtos bajtos self-assigned this Sep 18, 2018
@bajtos bajtos requested review from hacksparrow, virkt25 and a team September 18, 2018 11:37
@bajtos bajtos force-pushed the fix/update-delete-not-found branch from ef8b9f1 to 08a8553 Compare September 18, 2018 12:03
Rework Repository interface and implementations to throw EntityNotFound
error from `updateById`, `replaceById` and `deleteById` when there is
no entity with the provided id.

Update related controller methods to return 204 No Content on success,
because there is no boolean flag to indicate success anymore. Clients
should check the response status code instead (204 vs 404).

This also reverts commit f68a45c.
@bajtos bajtos force-pushed the fix/update-delete-not-found branch from 08a8553 to c5888ee Compare September 18, 2018 12:04
const idProp = this.modelClass.definition.idName();
const where = {} as Where;
where[idProp] = id;
return this.updateAll(data, where, options).then(count => count > 0);
const count = await this.updateAll(data, where, options);
Copy link
Contributor

@jannyHou jannyHou Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't return count anymore? Oops never mind. I just realize we returned a boolean instead of the count itself.

@@ -216,11 +221,11 @@ export class DefaultCrudRepository<T extends Entity, ID>
return this.toEntity(model);
}

update(entity: T, options?: Options): Promise<boolean> {
update(entity: T, options?: Options): Promise<void> {
Copy link
Contributor

@jannyHou jannyHou Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should call

return await this.updateById(entity.getId(), entity, options)

same for the next function delete.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return await is actually slightly less performant than return because of promise re-wrapping.

See http://exploringjs.com/es2016-es2017/ch_async-functions.html#_returned-promises-are-not-wrapped

return this.updateById(entity.getId(), entity, options);
}

delete(entity: T, options?: Options): Promise<boolean> {
delete(entity: T, options?: Options): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return await this.deleteById(entity.getId(), options);

@bajtos bajtos merged commit 264f231 into master Sep 18, 2018
@bajtos bajtos deleted the fix/update-delete-not-found branch September 18, 2018 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants