Skip to content

Commit

Permalink
Part 4 half way
Browse files Browse the repository at this point in the history
  • Loading branch information
JennGoldring committed Jun 12, 2023
1 parent cc4846c commit 53a9186
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import org.launchcode.techjobs.persistent.models.Employer;
import org.launchcode.techjobs.persistent.models.Job;
import org.launchcode.techjobs.persistent.models.Skill;
import org.launchcode.techjobs.persistent.models.data.EmployerRepository;
import org.launchcode.techjobs.persistent.models.data.JobRepository;
import org.launchcode.techjobs.persistent.models.data.SkillRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* Created by LaunchCode
Expand All @@ -20,6 +24,11 @@ public class HomeController {
@Autowired
private EmployerRepository employerRepository;

@Autowired
private SkillRepository skillRepository;
@Autowired
private JobRepository jobRepository;

public HomeController() {
}

Expand All @@ -34,30 +43,37 @@ public String displayAddJobForm(Model model) {
model.addAttribute("job", new Job());
// List<Employer> employers = (List<Employer>) employerRepository.findAll();
model.addAttribute("employers", employerRepository.findAll());
model.addAttribute("skills", skillRepository.findAll());
// model.addAttribute(new Job());
return "add";
}

@PostMapping("add")
public String processAddJobForm(@ModelAttribute @Valid Job newJob,
Errors errors, Model model, @RequestParam int employerId) {
Errors errors, Model model, @RequestParam int employerId, @RequestParam List<Integer> skills) {

if (errors.hasErrors()) {
model.addAttribute("title", "Add Job");
model.addAttribute("employers", employerRepository.findAll());
model.addAttribute("skills", skillRepository.findAll());
return "add";
}

Employer employer = employerRepository.findById(employerId).orElse(null);
newJob.setEmployer(employer);
// Process skills

if (skills != null && !skills.isEmpty()) {
List<Skill> skillObjs = (List<Skill>) skillRepository.findAllById(skills);
newJob.setSkills(skillObjs);
}
return "redirect:/";
}

@GetMapping("view/{jobId}")
@GetMapping("/view/{jobId}")
public String displayViewJob(Model model, @PathVariable int jobId) {
Job job = jobRepository.findById(jobId).orElse(null);

model.addAttribute("job", job);
return "view";

}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/launchcode/techjobs/persistent/models/Job.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.*;
import java.util.List;

@Entity
Expand All @@ -16,10 +14,15 @@ public class Job extends AbstractEntity {
private Employer employer;

@ManyToMany
@JoinTable(
name = "skill",
joinColumns = @JoinColumn(name = "job_id"),
inverseJoinColumns = @JoinColumn(name = "skill_id")
)
private List<Skill> skills;

public Job() {
super();
// super();
// no-arg constructor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@Entity
Expand All @@ -18,9 +19,9 @@ public class Skill extends AbstractEntity {
private String description;

@ManyToMany(mappedBy = "skills")
private List<Job> jobs;
private List<Job> jobs = new ArrayList<>();

public Skill () {
public Skill() {
}

public List<Job> getJobs() {
Expand Down
72 changes: 39 additions & 33 deletions src/test/java/org/launchcode/techjobs/persistent/TestTaskFour.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@
import mockit.Expectations;
import mockit.Mocked;
import org.junit.jupiter.api.Test;
import org.launchcode.techjobs.persistent.controllers.HomeController;
import org.launchcode.techjobs.persistent.controllers.ListController;
import org.launchcode.techjobs.persistent.models.Employer;
import org.launchcode.techjobs.persistent.models.Job;
import org.launchcode.techjobs.persistent.models.Skill;
import org.launchcode.techjobs.persistent.models.data.EmployerRepository;
import org.launchcode.techjobs.persistent.models.data.JobRepository;
import org.launchcode.techjobs.persistent.models.data.SkillRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;

import javax.persistence.ManyToMany;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -121,39 +127,39 @@ public void testHomeControllerHasSkillRepository () throws ClassNotFoundExceptio
/*
* Verifies that HomeController.processAddJobForm queries skillRepository and sets skills properly
* */
// @Test
// public void testProcessAddJobFormHandlesSkillsProperly (
// @Mocked SkillRepository skillRepository,
// @Mocked EmployerRepository employerRepository,
// @Mocked JobRepository jobRepository,
// @Mocked Job job,
// @Mocked Errors errors)
// throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
// Class homeControllerClass = getClassByName("controllers.HomeController");
// Method processAddJobFormMethod = homeControllerClass.getMethod("processAddJobForm", Job.class, Errors.class, Model.class, int.class, List.class);
//
// new Expectations() {{
// skillRepository.findAllById((Iterable<Integer>) any);
// job.setSkills((List<Skill>) any);
// }};
//
// Model model = new ExtendedModelMap();
// HomeController homeController = new HomeController();
//
// Field skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository");
// skillRepositoryField.setAccessible(true);
// skillRepositoryField.set(homeController, skillRepository);
//
// Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository");
// employerRepositoryField.setAccessible(true);
// employerRepositoryField.set(homeController, employerRepository);
//
// Field jobRepositoryField = homeControllerClass.getDeclaredField("jobRepository");
// jobRepositoryField.setAccessible(true);
// jobRepositoryField.set(homeController, jobRepository);
//
// processAddJobFormMethod.invoke(homeController, job, errors, model, 0, new ArrayList<Skill>());
// }
@Test
public void testProcessAddJobFormHandlesSkillsProperly (
@Mocked SkillRepository skillRepository,
@Mocked EmployerRepository employerRepository,
@Mocked JobRepository jobRepository,
@Mocked Job job,
@Mocked Errors errors)
throws ClassNotFoundException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException, InvocationTargetException {
Class homeControllerClass = getClassByName("controllers.HomeController");
Method processAddJobFormMethod = homeControllerClass.getMethod("processAddJobForm", Job.class, Errors.class, Model.class, int.class, List.class);

new Expectations() {{
skillRepository.findAllById((Iterable<Integer>) any);
job.setSkills((List<Skill>) any);
}};

Model model = new ExtendedModelMap();
HomeController homeController = new HomeController();

Field skillRepositoryField = homeControllerClass.getDeclaredField("skillRepository");
skillRepositoryField.setAccessible(true);
skillRepositoryField.set(homeController, skillRepository);

Field employerRepositoryField = homeControllerClass.getDeclaredField("employerRepository");
employerRepositoryField.setAccessible(true);
employerRepositoryField.set(homeController, employerRepository);

Field jobRepositoryField = homeControllerClass.getDeclaredField("jobRepository");
jobRepositoryField.setAccessible(true);
jobRepositoryField.set(homeController, jobRepository);

processAddJobFormMethod.invoke(homeController, job, errors, model, 0, new ArrayList<Skill>());
}

/*
* Verifies that skillRepository and employerRepository fields have been added to ListController
Expand Down

0 comments on commit 53a9186

Please sign in to comment.