Skip to content

Commit

Permalink
Task 2 nearly done
Browse files Browse the repository at this point in the history
  • Loading branch information
JennGoldring committed Jun 11, 2023
1 parent 6f90dab commit f1ea656
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 204 deletions.
1 change: 1 addition & 0 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


-- Part 2: Test it with SQL
SELECT name FROM employers WHERE location = 'St. Louis City';

-- Part 3: Test it with SQL

Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
package org.launchcode.techjobs.persistent.controllers;

import org.launchcode.techjobs.persistent.models.Employer;
import org.launchcode.techjobs.persistent.models.data.EmployerRepository;
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;
import java.util.Optional;


@Controller
@RequestMapping("employers")
public class EmployerController {

@Autowired
private final EmployerRepository employerRepository;

@Autowired
public EmployerController(EmployerRepository employerRepository) {
this.employerRepository = employerRepository;
}

@GetMapping("add")
@GetMapping("")
public String index(Model model) {
List<Employer> employers = (List<Employer>) employerRepository.findAll();
model.addAttribute("employers", employers);
return "employers/index";
}
@GetMapping("/add")
public String displayAddEmployerForm(Model model) {
model.addAttribute(new Employer());
model.addAttribute("employer", new Employer());
return "employers/add";
}

@PostMapping("add")
@PostMapping("/add")
public String processAddEmployerForm(@ModelAttribute @Valid Employer newEmployer,
Errors errors, Model model) {
Errors errors, Model model) {

if (errors.hasErrors()) {
return "employers/add";
}

return "redirect:";
}

@GetMapping("view/{employerId}")
public String displayViewEmployer(Model model, @PathVariable int employerId) {

Optional optEmployer = null;
Optional<Employer> optEmployer = employerRepository.findById(employerId);

if (optEmployer.isPresent()) {
Employer employer = (Employer) optEmployer.get();
Employer employer = optEmployer.get();
model.addAttribute("employer", employer);
return "employers/view";
} else {
return "redirect:../";
return "redirect: /employers";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.launchcode.techjobs.persistent.controllers;

import org.launchcode.techjobs.persistent.models.Skill;
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.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;

@Controller
@RequestMapping("/skills")
public class SkillController {
@Autowired
private SkillRepository skillRepository;

@Autowired
public void setSkillRepository(SkillRepository skillRepository) {
this.skillRepository = skillRepository;
}

@GetMapping
public String index(Model model) {
Iterable<Skill> skills = skillRepository.findAll();
model.addAttribute("skills", skills);
return "skills/index";
}

@GetMapping("/add")
public String displayAddSkillForm(Model model) {
model.addAttribute("skill", new Skill());
return "skills/add";
}

@PostMapping("/add")
public String processAddSkillForm(@Valid Skill skill, BindingResult result, Model model) {
skillRepository.save(skill);
return "redirect:/skills";
}

@GetMapping("/{id}")
public String displayViewSkill(Model model, @PathVariable("id") int id) {
Skill skill = skillRepository.findById(id).orElse(null);
if (skill == null) {
return "error";
}
model.addAttribute("skill", skill);
return "skills/view";
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Objects;
@MappedSuperClass
public abstract class AbstractEntity {

@MappedSuperclass
public abstract class AbstractEntity {
@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

public int getID() {
return id;
}
@NotBlank
@Size(max = 100)
private String name;

public int getId() {
return id;
}

public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.Entity;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
@Entity
public class Employer extends AbstractEntity {
@Id
@GeneratedValue
private Integer id;

@NotBlank
@Size(max = 50)
private String location;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.GenerationType;

public @interface GeneratedValue {
GenerationType strategy();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.*;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Job{

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.launchcode.techjobs.persistent.models;

public @interface MappedSuperClass {
public @interface MappedSuperclass {
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package org.launchcode.techjobs.persistent.models;

import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.validation.constraints.NotBlank;

@Entity
public class Skill extends AbstractEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotBlank
private String id;

@NotBlank
private String description;

public Skill () {

}
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.launchcode.techjobs.persistent.models.data;

import org.springframework.data.repository.CrudRepository;
import org.launchcode.techjobs.persistent.models.Employer;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployerRepository extends CrudRepository<Employer, Long> {
public interface EmployerRepository extends CrudRepository<Employer, Integer> {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.launchcode.techjobs.persistent.models.data;

import org.launchcode.techjobs.persistent.models.Skill;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SkillRepository extends CrudRepository<Skill, Long> {
public interface SkillRepository extends CrudRepository<Skill, Integer> {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.launchcode.techjobs.persistent.models.data;

public interface CrudRepository<T, T1> {
public @interface Transactional {
}
Loading

0 comments on commit f1ea656

Please sign in to comment.