Search in sources :

Example 1 with UserOrganizationException

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);
}
Also used : User(org.c4sg.entity.User) Organization(org.c4sg.entity.Organization) UserOrganization(org.c4sg.entity.UserOrganization) UserOrganizationException(org.c4sg.exception.UserOrganizationException) UserOrganization(org.c4sg.entity.UserOrganization)

Example 2 with UserOrganizationException

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");
    }
}
Also used : UserOrganizationException(org.c4sg.exception.UserOrganizationException) NotFoundException(org.c4sg.exception.NotFoundException) URI(java.net.URI) CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserOrganizationException (org.c4sg.exception.UserOrganizationException)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 URI (java.net.URI)1 Organization (org.c4sg.entity.Organization)1 User (org.c4sg.entity.User)1 UserOrganization (org.c4sg.entity.UserOrganization)1 NotFoundException (org.c4sg.exception.NotFoundException)1 CrossOrigin (org.springframework.web.bind.annotation.CrossOrigin)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1