I have an application where users can display and edit items in my database via a website. Now, to be sure there are no upcoming problems when two users edit the same item, I would like to prevent them from doing so. That means, the first user accessing the item can edit it and the rest could only view the item. But sadly, I have not clue how to properly implement this. Let's have a look at the setup first:
I have a controller that could look like this:
@RestController
@RequestMapping(value="/item")
public class ItemController {
@Autowired
ItemService itemService;
@GetMapping(value="/getItem/{item_id}")
public ResponseEntity getItem(@PathVariable String item_id) {
this.itemService.getItemById(item_id);
return ResponseEntity.ok(HttpStatus.OK);
}
}
Now when getItem
is called, I know that a user accessed the item with the given item_id
from the frontend. So I could simply build a List of "blocked" items in my Controller
, that contain the elements and check if the getItem
-method is called: "If item_id is in the list, return Access denied
& redirect to a "view-only" component, else ok
". But this seems to be quite a prone workaround and I cannot inmagine this is the way it is intended to be solved. Also I'd need to somehow remove the items from the list on a reliable way.
Is there a pattern or something like that that shows how this is to be solved?
I am using Spring-Boot
and Angular 7
, if that helps.