use of org.c4sg.exception.UserOrganizationException in project c4sg-services by Code4SocialGood.
the class OrganizationServiceImpl method saveUserOrganization.
@Override
public OrganizationDTO saveUserOrganization(Integer userId, Integer organizationId) throws UserOrganizationException {
User user = userDAO.findById(userId);
requireNonNull(user, "Invalid User Id");
Organization organization = organizationDAO.findOne(organizationId);
requireNonNull(organization, "Invalid organization Id");
UserOrganization userOrganization = userOrganizationDAO.findByUser_IdAndOrganization_Id(userId, organizationId);
if (nonNull(userOrganization)) {
throw new UserOrganizationException("The user organization relationship already exists.");
} else {
userOrganization = new UserOrganization();
userOrganization.setUser(user);
userOrganization.setOrganization(organization);
userOrganizationDAO.save(userOrganization);
}
return organizationMapper.getOrganizationDtoFromEntity(organization);
}
use of org.c4sg.exception.UserOrganizationException in project c4sg-services by Code4SocialGood.
the class OrganizationController method createUserOrganization.
@CrossOrigin
@RequestMapping(value = "/{id}/users/{userId}", method = RequestMethod.POST)
@ApiOperation(value = "Create a relation between user and organization")
@ApiResponses(value = { @ApiResponse(code = 404, message = "ID of organization or user invalid") })
public // TODO: Replace explicit user{id} with AuthN user id.
ResponseEntity<?> createUserOrganization(@ApiParam(value = "ID of user", required = true) @PathVariable("userId") Integer userId, @ApiParam(value = "ID of organization", required = true) @PathVariable("id") Integer organizationId) {
try {
organizationService.saveUserOrganization(userId, organizationId);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}/users/{userId}").buildAndExpand(organizationId, userId).toUri();
return ResponseEntity.created(location).build();
} catch (NullPointerException | UserOrganizationException e) {
throw new NotFoundException("ID of organization or user invalid, or relationship already exist");
}
}
Aggregations