I have an endpoint which assign a list of Roles to a single user.
@PutMapping(value = "/roles/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
public UserDto modifyRoles(
@ApiParam(value = "User ID", required = true) @PathVariable("id") Long id,
@ApiParam(value = "List of ID roles", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.modifyUserRoles(id, roles);
}
I want to receive a list of Users aswell a list of Roles so I can assign every role to each user, but i'm not sure how to do it and if I should send the list of users via @PathVariable, or if I there's a better way to do it. This is what I've tried:
@PutMapping(value = "/roles/{usersId}", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<UserDto> modifyMultipleRoles(
@ApiParam(value = "List of User ID", required = true) @PathVariable("id") List<Long> ids,
@ApiParam(value = "List of ID roles", required = true) @RequestBody List<Long> roles)
throws someExceptions {
return serviceClass.newMethodThatAssignEachRoleToEachUser(ids, roles);
}
Is this the best way to do it? Would this even work?
Thank you
Update response
This is what I've tried right now
List<Long> usersId=objectDto.getUsersId();
List<Long> rolesToAdd = objectDto.getRolesToAdd();
List<Long> rolesToDelete = objectDto.getRolesToRemove();
List<Usuario> users = usersId.stream()
.map(userId -> this.findById(userId))
.collect(Collectors.toList());
return users.stream().
// ????
Is that the way to proceed?