Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful design for create if absent use case

Tags:

java

rest

GET request on /template/settings would return the default setting for an Organization entity. I have a use case where all the organizations will have default settings. The first time an organization tries to access GET /template/settings if there are no settings, we need to create settings and then return them. This is a side effect. What would be a better design for this case?

Templates is a new feature which we would be turned on for organizations we choose. That specific event of turning on the feature would require a POST and it would be a good RESTful design. Once all the existing organizations are turned on for Templates, any new organizations added to the system will not have this event. So I would need create-if-absent construct.

Would this be the case for redirect to itself for creation?

like image 330
NaveenBabuE Avatar asked Jan 25 '26 10:01

NaveenBabuE


1 Answers

I would recommend the following.

  1. There will be one immutable resource representing the initial settings any new organization will get.

  2. When a new organization GETs its settings, it will receive the representation of this resource.

  3. When this organization PUTs or PATCHes an update to these settings, then a copy of the immutable resource is created for that particular organization and this copy is updated.

  4. When the organization GETs its settings again, it will receive the representation of its own updated resource.

like image 68
www.admiraalit.nl Avatar answered Jan 28 '26 00:01

www.admiraalit.nl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!