Skip to content

Commit ffccebf

Browse files
authored
Merge pull request #1 from smallintro/jpa_hateoas
added jpa mapping and HEATOAS support
2 parents d244926 + fc46791 commit ffccebf

File tree

17 files changed

+152
-47
lines changed

17 files changed

+152
-47
lines changed

README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

employee-mgr/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
<artifactId>springfox-bean-validators</artifactId>
7070
<version>3.0.0</version>
7171
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-hateoas</artifactId>
75+
</dependency>
7276
</dependencies>
7377

7478
<build>

employee-mgr/src/main/java/com/smallintro/springboot/entity/Employee.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import javax.persistence.ManyToMany;
1010
import javax.persistence.Table;
1111

12-
import com.fasterxml.jackson.annotation.JsonIgnore;
12+
import org.springframework.hateoas.RepresentationModel;
1313

1414
import io.swagger.annotations.ApiModel;
1515
import io.swagger.annotations.ApiModelProperty;
1616

1717
@ApiModel("This model is for employee information")
1818
@Entity
1919
@Table(name = "tb_employee")
20-
public class Employee {
20+
public class Employee extends RepresentationModel<Employee>{
2121

2222
@ApiModelProperty(notes = "Employee ID should have first name initial+ Gender initial + YYYYMMDDHHMM", required = true, example = "SM201104180925", position = 1)
2323
@Id
@@ -27,9 +27,8 @@ public class Employee {
2727
private String eMailId;
2828
private Date joiningDate;
2929

30-
@ManyToMany
31-
@JsonIgnore
32-
private List<Technology> technology = new ArrayList<Technology>();
30+
@ManyToMany(mappedBy = "employees")
31+
private List<Technology> technologies = new ArrayList<Technology>();
3332

3433
public String getEmpId() {
3534
return empId;
@@ -47,11 +46,11 @@ public void setEmpName(String empName) {
4746
this.empName = empName;
4847
}
4948

50-
public Integer getprojectCode() {
49+
public Integer getProjectCode() {
5150
return projectCode;
5251
}
5352

54-
public void setprojectCode(Integer projectCode) {
53+
public void setProjectCode(Integer projectCode) {
5554
this.projectCode = projectCode;
5655
}
5756

@@ -71,12 +70,12 @@ public void setJoiningDate(Date joiningDate) {
7170
this.joiningDate = joiningDate;
7271
}
7372

74-
public List<Technology> getTechnology() {
75-
return technology;
73+
public List<Technology> getTechnologies() {
74+
return technologies;
7675
}
7776

78-
public void setTechnology(List<Technology> technology) {
79-
this.technology = technology;
77+
public void setTechnologies(List<Technology> technologies) {
78+
this.technologies = technologies;
8079
}
8180

8281
}

employee-mgr/src/main/java/com/smallintro/springboot/entity/Technology.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import javax.persistence.ManyToMany;
1010
import javax.persistence.Table;
1111

12+
import org.springframework.hateoas.RepresentationModel;
13+
1214
import io.swagger.annotations.ApiModel;
1315
import io.swagger.annotations.ApiModelProperty;
1416

1517
@ApiModel("This model is for technology information")
1618
@Entity
1719
@Table(name = "tb_technology")
18-
public class Technology {
20+
public class Technology extends RepresentationModel<Technology>{
1921

2022
@ApiModelProperty(notes="Autogenerated unique Identity",position=1)
2123
@Id
@@ -24,7 +26,7 @@ public class Technology {
2426
private String techName;
2527
private String catagoty;
2628

27-
@ManyToMany(mappedBy = "technology")
29+
@ManyToMany
2830
private List<Employee> employee = new ArrayList<Employee>();
2931

3032
public Integer getTechId() {

employee-mgr/src/main/java/com/smallintro/springboot/service/EmployeeService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import javax.validation.constraints.Min;
66

77
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
89

910
import com.smallintro.springboot.entity.Employee;
1011
import com.smallintro.springboot.repository.EmployeeRepo;
1112
import com.smallintro.springboot.utils.ProjectConstants;
1213

14+
@Service
1315
public class EmployeeService {
1416

1517
@Autowired

employee-mgr/src/main/java/com/smallintro/springboot/service/TechService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.http.HttpStatus;
7+
import org.springframework.stereotype.Service;
78
import org.springframework.web.bind.annotation.RequestBody;
89
import org.springframework.web.server.ResponseStatusException;
910

1011
import com.smallintro.springboot.entity.Technology;
1112
import com.smallintro.springboot.repository.TechRepo;
1213
import com.smallintro.springboot.utils.ProjectConstants;
1314

15+
@Service
1416
public class TechService {
1517

1618
@Autowired
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1) Entity and request validation
2+
2) Define column name and property
3+
3) Test ManyToMany + Add Support for HATEOAS
4+
4) Add i18n
5+
5) Add DTOs and filters
6+
6) Implement versioning and content negotiation
7+

project-mgr/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
<artifactId>springfox-bean-validators</artifactId>
7070
<version>3.0.0</version>
7171
</dependency>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-starter-hateoas</artifactId>
75+
</dependency>
7276
</dependencies>
7377

7478
<build>

project-mgr/src/main/java/com/smallintro/springboot/controller/DepartmentController.java

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import java.util.List;
44

55
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.hateoas.CollectionModel;
7+
import org.springframework.hateoas.EntityModel;
8+
import org.springframework.hateoas.Link;
9+
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
610
import org.springframework.http.MediaType;
711
import org.springframework.web.bind.annotation.DeleteMapping;
812
import org.springframework.web.bind.annotation.GetMapping;
@@ -13,42 +17,72 @@
1317
import org.springframework.web.bind.annotation.RestController;
1418

1519
import com.smallintro.springboot.entity.Department;
20+
import com.smallintro.springboot.entity.Project;
1621
import com.smallintro.springboot.service.DepartmentService;
1722

1823
import io.swagger.annotations.Api;
1924

20-
21-
@Api(tags="Department management APIs",value="Project Controller")
25+
@Api(tags = "Department management APIs", value = "Department Controller")
2226
@RestController
2327
@RequestMapping("department")
2428
public class DepartmentController {
25-
29+
2630
@Autowired
2731
DepartmentService deptService;
28-
29-
@GetMapping(value="/{deptName}",produces = MediaType.APPLICATION_JSON_VALUE)
30-
public Department getDepartmentByName(@PathVariable String deptName) {
31-
32-
return deptService.getDepartmentByName(deptName);
32+
33+
@GetMapping(value = "/{deptName}", produces = MediaType.APPLICATION_JSON_VALUE)
34+
public EntityModel<Department> getDepartmentByName(@PathVariable String deptName) {
35+
36+
Department dept = deptService.getDepartmentByName(deptName);
37+
38+
Link selfLink = WebMvcLinkBuilder.linkTo(this.getClass()).slash(dept.getDeptName()).withSelfRel();
39+
dept.add(selfLink);
40+
EntityModel<Department> deptModel = EntityModel.of(dept);
41+
return deptModel;
3342
}
3443

3544
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
36-
public List<Department> getAllDepartments() {
37-
38-
return deptService.getAllDepartments();
45+
public CollectionModel<Department> getAllDepartments() {
46+
47+
List<Department> departments = deptService.getAllDepartments();
48+
49+
for (Department dept : departments) {
50+
51+
Link selfLink = WebMvcLinkBuilder.linkTo(this.getClass()).slash(dept.getDeptName()).withSelfRel();
52+
dept.add(selfLink);
53+
54+
CollectionModel<Project> projects = WebMvcLinkBuilder.methodOn(this.getClass())
55+
.getProjects(dept.getDeptName());
56+
57+
Link projLink = WebMvcLinkBuilder.linkTo(projects).withRel("all-projects");
58+
59+
dept.add(projLink);
60+
61+
}
62+
Link selfLink = WebMvcLinkBuilder.linkTo(this.getClass()).withSelfRel();
63+
64+
CollectionModel<Department> deptModels = CollectionModel.of(departments, selfLink);
65+
return deptModels;
3966
}
40-
67+
4168
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
4269
public Department createDepartment(@RequestBody Department dept) {
43-
70+
4471
return deptService.createDepartment(dept);
4572
}
46-
73+
4774
@DeleteMapping("/{deptId}")
4875
public void deleteDepartmentById(@PathVariable Long deptId) {
49-
76+
5077
deptService.deleteDepartmentById(deptId);
5178
}
5279

80+
@GetMapping("/{deptName}/projects")
81+
public CollectionModel<Project> getProjects(@PathVariable String deptName) {
82+
List<Project> projects = deptService.getProjects(deptName);
83+
84+
CollectionModel<Project> projectsModel = CollectionModel.of(projects);
85+
return projectsModel;
86+
}
5387

5488
}

project-mgr/src/main/java/com/smallintro/springboot/controller/ProjectController.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import org.springframework.web.bind.annotation.RestController;
1515
import org.springframework.web.server.ResponseStatusException;
1616

17+
import com.smallintro.springboot.entity.Department;
1718
import com.smallintro.springboot.entity.Project;
1819
import com.smallintro.springboot.exception.RecordNotFoundException;
20+
import com.smallintro.springboot.service.DepartmentService;
1921
import com.smallintro.springboot.service.ProjectService;
2022
import com.smallintro.springboot.utils.ProjectConstants;
2123

@@ -29,6 +31,9 @@ public class ProjectController {
2931
@Autowired
3032
ProjectService projectService;
3133

34+
@Autowired
35+
DepartmentService deptService;
36+
3237
@GetMapping
3338
public List<Project> getProjects() {
3439
return projectService.findAllProjects();
@@ -43,14 +48,20 @@ public Project getProjectInfo(@PathVariable String projectName) {
4348
}
4449
}
4550

46-
@PostMapping
47-
public String addProject(@RequestBody Project proj) {
51+
@PostMapping(value = "/{deptId}/project")
52+
public Project addProject(@PathVariable Long deptId, @RequestBody Project proj) {
53+
Department dept = deptService.getDepartmentById(deptId);
54+
if (null == dept) {
55+
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
56+
"No department found with department Id: " + deptId);
57+
}
4858
if (projectService.isProjectExistsByName(proj.getProjectName())) {
4959
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
5060
"Project with name " + proj.getProjectName() + " already exists");
5161
}
52-
projectService.saveProject(proj);
53-
return ProjectConstants.OPERATION_SUCCESS;
62+
proj.setDepartment(dept);
63+
return projectService.saveProject(proj);
64+
5465
}
5566

5667
@PutMapping(value = "/{projectCode}")

0 commit comments

Comments
 (0)