Untitled

 avatar
unknown
plain_text
4 years ago
2.9 kB
37
Indexable
//Old controller:
@GetMapping("/user/{userID}/projects/target-reached")
    private String getUsersContributedProjectsThatReachedTarget(@PathVariable String userID, Model model) {


        //Get contributions from userID.
        List<Contribution> contributionList = contributionRepo.findAllByContributorUserID(Long.parseLong(userID));

        System.out.println(contributionList.get(0).toString());

        //Get projects from contributions.
        ArrayList<Project> projectList = new ArrayList<Project>();

        //For each contribution add the corresponding project to the projectList.
        for (Contribution contribution : contributionList) {

            Project project = projectRepo.findByProjectID(contribution.getProjectID());

            System.out.println(project.toString());

            int amountContributed = project.getAmountContributed();
            int target = Integer.parseInt(project.getTargets());

            System.out.println(amountContributed + " " + target);

            //If the target has been reached add the project to the projectList.
            if (amountContributed >= target) {
                projectList.add(project);
            }
        }
        //Add projectList to model.
        model.addAttribute(projectList);

        return "target-reached-projects";
    }

//NEW
//NEW CONTROLLER:
@GetMapping("/user/{userID}/projects/target-reached")
    private String getUsersContributedProjectsThatReachedTarget(@PathVariable String userID, Model model) {


        ProjectService projectService = new ProjectService();
        projectService.listProjectsReachedTarget(userID);

        //Add projectList to model.
        model.addAttribute(projectList);

        return "target-reached-projects";
    }

//NEW SERVICE:

public ArrayList<Project> listProjectsReachedTarget(String userID) {

        //Get contributions from userID.
        List<Contribution> contributionList = contributionRepo.findAllByContributorUserID(Long.parseLong(userID));

        System.out.println(contributionList.get(0).toString());

        //Get projects from contributions.
        ArrayList<Project> projectList = new ArrayList<Project>();

        //For each contribution add the corresponding project to the projectList.
        for (Contribution contribution : contributionList) {

            Project project = projectRepo.findByProjectID(contribution.getProjectID());

            System.out.println(project.toString());

            int amountContributed = project.getAmountContributed();
            int target = Integer.parseInt(project.getTargets());

            System.out.println(amountContributed + " " + target);

            //If the target has been reached add the project to the projectList.
            if (amountContributed >= target) {
                projectList.add(project);
            }
        }
        return projectList;
    }
Editor is loading...