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
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
@SpringBootApplication
public class CandidateChallengeApplication
{

public static void main(String[] args)
{
SpringApplication.run(CandidateChallengeApplication.class, args);
}

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

import java.util.List;
import java.util.Map;

public class Employee {
private long id;
private List<Map<String,String>> properties;
private List<List<Map<String,String>>> bySupervisorList;

public Employee (){
}

public Employee(long id, List<Map<String, String>> properties) {
this.id = id;
this.properties = properties;
}

public long getId() {
return id;
}

public List<Map<String, String>> getProperties() {
return properties;
}

public List<List<Map<String, String>>> getBySupervisorList() {
return bySupervisorList;
}

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

public void setProperties(List<Map<String, String>> properties) {
this.properties = properties;
}

public void setBySupervisorList(List<List<Map<String, String>>> bySupervisorList) {
this.bySupervisorList = bySupervisorList;
}

@Override
public String toString() {
return "Employee [id=" + id + ", properties=" + properties + ", bySupervisorList=" + bySupervisorList + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.zoomcare.candidatechallenge.repositories;

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.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.zoomcare.candidatechallenge.models.Employee;

@Repository
public class EmployeeRepository {
@Autowired
JdbcTemplate jdbcTemplate;

//Returns a List of Employees at top level (Haven't a supervisor)
public List<Employee> getTopLevelEmployees(){
List<Employee> topLevelEmpList = new ArrayList<>();
List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT id FROM Employee WHERE supervisor_id IS null");

for (int i = 0; i < list.size(); i++){
Map<String,Object> topEmployee= list.get(i);
Employee employee = getEmployeeById((Long)topEmployee.get("id")).get(0);
topLevelEmpList.add(employee);
}
return topLevelEmpList;
}

//Returns a specific list of Employees searching by id
public List<Employee> getEmployeeById(long id){
List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT * FROM Employee WHERE id = ?", id);
List<Employee> empList = new ArrayList<>();

list.forEach(item -> {
List<Map<String,String>> propertyList = new ArrayList<>();
List<List<Map<String,String>>> bySupervisorList;
Employee employee = new Employee();

employee.setId((Long)item.get("id"));
propertyList = getProperties(id);
bySupervisorList = getEmpBySupervisor(id);

if (bySupervisorList.size() > 0){
employee.setBySupervisorList(bySupervisorList);
}
if (propertyList.size() > 0){
employee.setProperties(propertyList);
}
empList.add(employee);
});

return empList;
}

//Returns a specific list of Properties containing a map<key,value> elements, searching by employee_id
private List<Map<String,String>> getProperties (long id){
List<Map<String,String>> propertiesList = new ArrayList<>();
List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT * FROM Property WHERE employee_id = ?", id);

list.forEach(property ->{
Map <String,String> map = new HashMap<String,String>();
map.put("key", (String) property.get("key"));
map.put("value", (String) property.get("value"));
propertiesList.add(map);
});

return propertiesList;
}

// Returns a list of Employees containing a map<key,value> elements, searching by supervisor_id
private List<List<Map<String,String>>> getEmpBySupervisor (long id){
List<List<Map<String,String>>> bySupervisorList = new ArrayList<List<Map<String,String>>>();
List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT id FROM Employee WHERE supervisor_id = ?", id);

if (list.size()>0){
list.forEach(item ->{
List<List<Map<String,String>>> innerbySupervisorList;
List<Map<String,String>> nestedReport = new ArrayList<Map<String,String>>();
Map <String,String> map = new HashMap<String,String>();
map.put("supervisor_id", String.valueOf(id));
map.put("employee_id", String.valueOf(item.get("id")));
nestedReport.add(map);
bySupervisorList.add(nestedReport);
innerbySupervisorList = getEmpBySupervisor ((Long)item.get("id"));

if (innerbySupervisorList.size()>0){
innerbySupervisorList.forEach(sub ->{
for (int i=0; i<sub.size();i++){
nestedReport.add(sub.get(i));
}
});
}

});
}
return bySupervisorList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zoomcare.candidatechallenge.resources;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zoomcare.candidatechallenge.models.Employee;
import com.zoomcare.candidatechallenge.repositories.EmployeeRepository;

@RestController
@RequestMapping("/employee")
public class EmployeeResource {

@Autowired
EmployeeRepository employeeRepository;

@RequestMapping("/{id}")
public List<Employee> getEmployeeById (@PathVariable final long id){
return employeeRepository.getEmployeeById(id);
}

@RequestMapping("/employees")
public List<Employee> getTopLevelEmployees (){
return employeeRepository.getTopLevelEmployees();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ spring:
h2:
console:
enabled: true
datasource:
url: jdbc:h2:mem:testdb
management:
endpoints:
web:
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/db/migration/V1__init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ create table property (
value varchar(256),
primary key (employee_id, key),
foreign key (employee_id) references employee(id)
);
);
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ insert into property (employee_id, key, value) values (@namarketing, 'region', '
insert into employee(supervisor_id) values (@vpm);
select @eumarketing := scope_identity();
insert into property (employee_id, key, value) values (@eumarketing, 'title', 'Regional Director of Marketing');
insert into property (employee_id, key, value) values (@eumarketing, 'region', 'Europe');
insert into property (employee_id, key, value) values (@eumarketing, 'region', 'Europe');
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zoomcare.candidatechallenge;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CandidateChallengeApplicationTest {

@Test
public void contextLoads() {
}

}
39 changes: 39 additions & 0 deletions src/test/java/com/zoomcare/candidatechallenge/TestWebApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zoomcare.candidatechallenge;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


public class TestWebApp extends CandidateChallengeApplicationTest {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testGetEmployeeById() throws Exception {
mockMvc.perform(get("/employee/7")).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"));
}

@Test
public void testGetTopLevelEmployees() throws Exception {
mockMvc.perform(get("/employee/employees")).andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"));
}

}