I want to implement sorting for a list of filters assigned for a terminal in Spring endpoint when one filter is dragged and the order of all filters if calculated again. I tried this:
DTO:
public class FilterNewDTO {
private Integer id;
private Integer terminal_id;
private Integer position;
private Integer parent_id;
}
Endpoint:
@PostMapping("filter/updated_position/{id}")
public ResponseEntity<?> saveFilterPosition(@PathVariable Integer id, @RequestBody FilterNewDTO dto) {
List<Filters> list = filterService.findFiltersByTerminalId(dto.getTerminal_id());
for (int i=0; i<list.size(); i++) {
Filters filter = list.get(i);
int current_position = 0;
current_position = filter.getPosition();
filter.setPosition(current_position + 1);
filter.update(risk_mapper.toFilter(dto));
filterService.save(filter);
}
return ok().build();
}
But as you can see I just iterate over the list of filters by terminal id. What will happen if I change the filter position from 3 to 5? I suppose that portions 1-3 should not be incremented. Can you give me some guidance how to implement this?