Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -57,6 +66,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.zoomcare.candidatechallenge.controllers;

import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zoomcare.candidatechallenge.model.EmployeeDto;

@RequestMapping("/employee")
public interface EmployeeController {

@GetMapping("/")
ResponseEntity<Object> getAll();

@GetMapping("/{id}")
ResponseEntity<Object> getById(@PathVariable Long id);

@PostMapping("/")
ResponseEntity<Object> addEmployee(@RequestBody EmployeeDto employee);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.zoomcare.candidatechallenge.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zoomcare.candidatechallenge.entity.Employee;
import com.zoomcare.candidatechallenge.entity.Property;
import com.zoomcare.candidatechallenge.model.EmployeeDto;
import com.zoomcare.candidatechallenge.service.EmployeeService;

@RestController
@RequestMapping("/employee")
public class EmployeeControllerImpl implements EmployeeController {

@Autowired
private EmployeeService employeeService;

@Override
public ResponseEntity<Object> getAll() {
List<Map<String, Object>> response = new ArrayList<Map<String,Object>>();

List<Employee> employees = employeeService.getAllEmployees();

employees.forEach(employee -> {
Map<String, Object> resp = new HashMap<String, Object>();
resp.putAll(getSupervisorData(employee));
response.add(resp);
});

return new ResponseEntity<>(response, HttpStatus.OK);
}

@Override
public ResponseEntity<Object> getById(Long id) {
//First fetch the employee properties
Employee employee = employeeService.findEmployeeById(id);

Map<String, Object> response = new HashMap<String, Object>();
response.putAll(getSupervisorData(employee));

return new ResponseEntity<>(response, HttpStatus.OK);
}

private Map<String, Object> getSupervisorData(Employee employee) {
Map<String, Object> resp = new HashMap<String, Object>();

//EmployeeResponse response = new EmployeeResponse();
resp.putAll(getEmployeeData(employee));
List<Map<String, Object>> listEmployees = new ArrayList<Map<String,Object>>();

//then fetch the employees under supervision of current employee
List<Employee> supervised = employeeService.findBySupervisorId(employee.getId());
if (!supervised.isEmpty()) {
supervised.forEach(emp -> {
listEmployees.add(getEmployeeData(emp));
});

resp.put("employees", listEmployees);
}
return resp;
}

private Map<String, Object> getEmployeeData(Employee employee) {
Map<String, Object> response = new HashMap<String, Object>();

response.put("id", employee.getId());
Map<String, String> properties = new HashMap<String, String>();
employee.getProperties().forEach(prop -> {
properties.put(prop.getKey(), prop.getValue());
});

response.putAll(properties);

return response;
}

@Override
public ResponseEntity<Object> addEmployee(EmployeeDto empl) {
System.out.println("Empl" + empl);
Employee employee = new Employee();
employee.setSupervisorId(empl.getSupervisorId());
List<Property> properties = new ArrayList<Property>();
empl.getProperties().forEach(prop -> {
Property property = new Property();
property.setKey(prop.getKey());
property.setValue(prop.getValue());
property.setEmployee(employee);
properties.add(property);
});
employee.setProperties(properties);

Employee added = employeeService.save(employee);

return new ResponseEntity<>("Added succesfully with ID: " + added.getId(), HttpStatus.OK);
}

}
58 changes: 58 additions & 0 deletions src/main/java/com/zoomcare/candidatechallenge/entity/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.zoomcare.candidatechallenge.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Employee {

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

@Column(name = "supervisor_id")
private Long supervisorId;

@JoinColumn(name = "employee_id")
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Property> properties;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getSupervisorId() {
return supervisorId;
}

public void setSupervisorId(Long supervisorId) {
this.supervisorId = supervisorId;
}

public List<Property> getProperties() {
return properties;
}

public void setProperties(List<Property> properties) {
this.properties = properties;
}

@Override
public String toString() {
return "Employee [id=" + id + ", supervisorId=" + supervisorId + ", properties=" + properties + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.zoomcare.candidatechallenge.entity;

import java.io.Serializable;

public class EmployeeId implements Serializable {

private static final long serialVersionUID = 1268563792164235735L;

private String key;

private Employee employee;

public EmployeeId() {
}

public Employee getEmployee() {
return employee;
}


public void setEmployee(Employee employee) {
this.employee = employee;
}


public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}

}
51 changes: 51 additions & 0 deletions src/main/java/com/zoomcare/candidatechallenge/entity/Property.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.zoomcare.candidatechallenge.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.fasterxml.jackson.annotation.JsonIgnore;

@Entity
@IdClass(value = EmployeeId.class)
public class Property {

@Id
@Column(name = "key")
private String key;

@ManyToOne
@JoinColumn(name = "employee_id")
@JsonIgnore
private Employee employee;

@Column(name = "value")
private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public Employee getEmployee() {
return employee;
}

public void setEmployee(Employee employee) {
this.employee = employee;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.zoomcare.candidatechallenge.exception;

public class EmployeeNotFoundException extends RuntimeException {

private static final long serialVersionUID = 8826523028693639120L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zoomcare.candidatechallenge.exception;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class EmployeeProductExceptionController {

@ExceptionHandler(value = EmployeeNotFoundException.class)
public ResponseEntity<Object> exception(EmployeeNotFoundException exception) {
return new ResponseEntity<>("Employee not found", HttpStatus.NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zoomcare.candidatechallenge.model;

import java.util.List;

public class EmployeeDto {

private Long id;
private Long supervisorId;
private List<PropertyDto> properties;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getSupervisorId() {
return supervisorId;
}

public void setSupervisorId(Long supervisorId) {
this.supervisorId = supervisorId;
}

public List<PropertyDto> getProperties() {
return properties;
}

public void setProperties(List<PropertyDto> properties) {
this.properties = properties;
}

@Override
public String toString() {
return "EmployeeDto [id=" + id + ", supervisorId=" + supervisorId + ", properties=" + properties + "]";
}

}
Loading