Search in sources :

Example 1 with BadRequestException

use of org.c4sg.exception.BadRequestException in project c4sg-services by Code4SocialGood.

the class ProjectController method createUserProject.

@CrossOrigin
@RequestMapping(value = "/{id}/users/{userId}", method = RequestMethod.POST)
@ApiOperation(value = "Create a relation between user and project")
@ApiResponses(value = { @ApiResponse(code = 404, message = "ID of project or user invalid") })
public //TODO: Replace explicit user{id} with AuthN user id.
ResponseEntity<?> createUserProject(@ApiParam(value = "ID of user", required = true) @PathVariable("userId") Integer userId, @ApiParam(value = "ID of project", required = true) @PathVariable("id") Integer projectId, @ApiParam(value = "User project status, A-Applied, B-Bookmarked", allowableValues = "A, B", required = true) @RequestParam("userProjectStatus") String userProjectStatus) {
    try {
        projectService.saveUserProject(userId, projectId, userProjectStatus);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}/users/{userId}").buildAndExpand(projectId, userId, userProjectStatus).toUri();
        return ResponseEntity.created(location).build();
    } catch (NullPointerException e) {
        throw new NotFoundException("ID of project or user invalid");
    } catch (UserProjectException | BadRequestException e) {
        throw e;
    }
}
Also used : NotFoundException(org.c4sg.exception.NotFoundException) BadRequestException(org.c4sg.exception.BadRequestException) URI(java.net.URI) UserProjectException(org.c4sg.exception.UserProjectException)

Example 2 with BadRequestException

use of org.c4sg.exception.BadRequestException in project c4sg-services by Code4SocialGood.

the class ProjectServiceImpl method saveUserProject.

@Override
public ProjectDTO saveUserProject(Integer userId, Integer projectId, String userProjectStatus) {
    User user = userDAO.findById(userId);
    requireNonNull(user, "Invalid User Id");
    Project project = projectDAO.findById(projectId);
    requireNonNull(project, "Invalid Project Id");
    if (userProjectStatus == null || (!userProjectStatus.trim().equalsIgnoreCase("A") && !userProjectStatus.trim().equalsIgnoreCase("B"))) {
        throw new BadRequestException("Invalid Project Status");
    } else if (userProjectStatus.trim().equalsIgnoreCase("A")) {
        isUserAppliedPresent(userId, projectId);
        UserProject userProject = new UserProject();
        userProject.setUser(user);
        userProject.setProject(project);
        userProject.setStatus("A");
        apply(user, project);
        userProjectDAO.save(userProject);
    } else if (userProjectStatus.trim().equalsIgnoreCase("B")) {
        isBookmarkPresent(userId, projectId);
        UserProject userProject = new UserProject();
        userProject.setUser(user);
        userProject.setProject(project);
        userProject.setStatus("B");
        userProjectDAO.save(userProject);
    }
    return projectMapper.getProjectDtoFromEntity(project);
}
Also used : UserProject(org.c4sg.entity.UserProject) Project(org.c4sg.entity.Project) User(org.c4sg.entity.User) UserProject(org.c4sg.entity.UserProject) BadRequestException(org.c4sg.exception.BadRequestException)

Example 3 with BadRequestException

use of org.c4sg.exception.BadRequestException in project c4sg-services by Code4SocialGood.

the class ApplicationServiceImpl method validateApplication.

private void validateApplication(ApplicationDTO application) {
    User user = userDAO.findById(application.getUserId());
    requireNonNull(user, "Invalid User Id");
    Project project = projectDAO.findById(application.getProjectId());
    requireNonNull(project, "Invalid Project Id");
    if (application.getStatus() == null || (!application.getStatus().equals("A") && !application.getStatus().equals("C") && !application.getStatus().equals("D"))) {
        throw new BadRequestException("Invalid Project Status");
    }
}
Also used : Project(org.c4sg.entity.Project) User(org.c4sg.entity.User) BadRequestException(org.c4sg.exception.BadRequestException)

Aggregations

BadRequestException (org.c4sg.exception.BadRequestException)3 Project (org.c4sg.entity.Project)2 User (org.c4sg.entity.User)2 URI (java.net.URI)1 UserProject (org.c4sg.entity.UserProject)1 NotFoundException (org.c4sg.exception.NotFoundException)1 UserProjectException (org.c4sg.exception.UserProjectException)1