From bd5db650598a0fe4c7b9329cbe0194b984790376 Mon Sep 17 00:00:00 2001 From: Lopez Zavala <2246358@cognizant.com> Date: Wed, 11 Jan 2023 13:51:30 -0600 Subject: [PATCH] Solution from Pabel Lopez --- documents/Zoom-care.postman_collection.json | 38 ++++++ documents/development-notes.txt | 12 ++ .../controller/EmployeeController.java | 30 +++++ .../employee/EmployeeBean.java | 49 ++++++++ .../employee/EmployeeJdbc.java | 117 ++++++++++++++++++ src/main/resources/application.properties | 4 + .../migration/V2__initial_data_structures.sql | 15 ++- 7 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 documents/Zoom-care.postman_collection.json create mode 100644 documents/development-notes.txt create mode 100644 src/main/java/com/zoomcare/candidatechallenge/controller/EmployeeController.java create mode 100644 src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeBean.java create mode 100644 src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeJdbc.java create mode 100644 src/main/resources/application.properties diff --git a/documents/Zoom-care.postman_collection.json b/documents/Zoom-care.postman_collection.json new file mode 100644 index 0000000..3d40465 --- /dev/null +++ b/documents/Zoom-care.postman_collection.json @@ -0,0 +1,38 @@ +{ + "info": { + "_postman_id": "815913fd-1f1e-481c-aff4-02121dde4d02", + "name": "Zoom-care", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "24706689" + }, + "item": [ + { + "name": "Get Top Employees", + "request": { + "method": "GET", + "header": [] + }, + "response": [] + }, + { + "name": "Get Employee by Id", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:8080/employee/1", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "employee", + "1" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/documents/development-notes.txt b/documents/development-notes.txt new file mode 100644 index 0000000..0fac417 --- /dev/null +++ b/documents/development-notes.txt @@ -0,0 +1,12 @@ +Development notes: + +This challenge was solved by creating two GET API calls: + +This will be returning all top employee + {host}/employee +This will be returning a specific employee by providing the id into the url + {host}/employee/{id} +Please see on /documents folder, I did include a PostMan Collection with API calls examples +File: Zoom-care.postman_collection.json + +Notes: I did face some issues with browser cache data, in order to perform further test I did add some extra test data on /src/main/db/migration/ V2__initial_data_structures.sql \ No newline at end of file diff --git a/src/main/java/com/zoomcare/candidatechallenge/controller/EmployeeController.java b/src/main/java/com/zoomcare/candidatechallenge/controller/EmployeeController.java new file mode 100644 index 0000000..966716b --- /dev/null +++ b/src/main/java/com/zoomcare/candidatechallenge/controller/EmployeeController.java @@ -0,0 +1,30 @@ +package com.zoomcare.candidatechallenge.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +import com.zoomcare.candidatechallenge.employee.EmployeeJdbc; +/** + * @author Pabel Lopez + */ + +@RestController +public class EmployeeController { + + @Autowired + EmployeeJdbc repository; + + @GetMapping("employee/{id}") + public List getEmployeeById (@PathVariable final long id){ + return repository.getEmployeeById(id); + } + + @GetMapping("employee/") + public List getTopLevelEmployees (){ + return repository.getTopLevelEmployees(); + } +} \ No newline at end of file diff --git a/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeBean.java b/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeBean.java new file mode 100644 index 0000000..973aa93 --- /dev/null +++ b/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeBean.java @@ -0,0 +1,49 @@ +package com.zoomcare.candidatechallenge.employee; + +import java.util.List; +import java.util.Map; +/** + * @author Pabel Lopez + */ + +public class EmployeeBean { + private long id; + private List> properties; + private List>> reportList ; + public EmployeeBean (){ + } + + public EmployeeBean(long id, List> properties) { + this.id = id; + this.properties = properties; + } + + public long getId() { + return id; + } + + public List> getProperties() { + return properties; + } + + public List>> getReportList() { + return reportList; + } + + public void setId(long id) { + this.id = id; + } + + public void setProperties(List> properties) { + this.properties = properties; + } + + public void setReportList(List>> reportList) { + this.reportList = reportList; + } + + @Override + public String toString() { + return "EmployeeBean [id=" + id + ", properties=" + properties + ", reportList=" + reportList + "]"; + } +} \ No newline at end of file diff --git a/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeJdbc.java b/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeJdbc.java new file mode 100644 index 0000000..b08501d --- /dev/null +++ b/src/main/java/com/zoomcare/candidatechallenge/employee/EmployeeJdbc.java @@ -0,0 +1,117 @@ +package com.zoomcare.candidatechallenge.employee; + +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; +/** + * @author Pabel Lopez + */ + +@Repository +public class EmployeeJdbc { + @Autowired + JdbcTemplate jdbcTemplate; + + private String SELECT_EMPLOYEE_BY_ID ="select * from employee where id=?"; + private String SELECT_CATEGORIES = "select * from property where employee_id=?"; + private String SELECT_REPORT_STRUCTURE = "select id from employee where supervisor_id=?"; + private String SELECT_TOP_EMPLOYEES = "select id from employee where supervisor_id is null"; + + /** + *

Return all top level employees

+ @return A list wit all top level Employees + */ + public List getTopLevelEmployees (){ + List topEmpList = new ArrayList<>(); + List> list = jdbcTemplate.queryForList(SELECT_TOP_EMPLOYEES); + for (int i=0; i topEmployee= list.get(i); + EmployeeBean employee = getEmployeeById((Long)topEmployee.get("id")).get(0); + topEmpList.add(employee); + } + return topEmpList; + } + + /** + *

Return a specific Employee based on provided id

+ @param id The id of the employee to be retrieved + @return A list of employee properties and nested reporting structure + */ + public List getEmployeeById(long id){ + List> list = jdbcTemplate.queryForList(SELECT_EMPLOYEE_BY_ID, id); + List empList = new ArrayList<>(); + + list.forEach(item -> { + List> propertyList = new ArrayList<>(); + List>> reportLi; + EmployeeBean employee = new EmployeeBean(); + + employee.setId((Long)item.get("id")); + propertyList = getProperties(id); + reportLi = getNestedReport(id); + if (reportLi.size()>0){ + employee.setReportList(reportLi); + } + if (propertyList.size()>0){ + employee.setProperties(propertyList); + } + empList.add(employee); + }); + return empList; + } + + /** + *

Return all the properties of one employee based on provided id

+ @param id The id of the employee to be retrieved + @return A list containing the map of all properties of employee + */ + private List> getProperties (long id){ + List> properties = new ArrayList<>(); + List> propertiesList = jdbcTemplate.queryForList(SELECT_CATEGORIES, id); + + propertiesList.forEach(property ->{ + Map map = new HashMap(); + map.put("key", (String) property.get("key")); + map.put("value", (String) property.get("value")); + properties.add(map); + }); + + return properties; + } + + /** + *

Return a list with all nested employee related to the provided supervidor

+ @param id The id of the supervisor to get the nested organization + @return A list containing the map of all reported employees of the supervisor provided + */ + private List>> getNestedReport (long id){ + List>> reportList = new ArrayList>>(); + List> list = jdbcTemplate.queryForList(SELECT_REPORT_STRUCTURE, id); + if (list.size()>0){ + list.forEach(item ->{ + List>> innerReportList; + List> subReport = new ArrayList>(); + Map map = new HashMap(); + map.put("supervisor_id", String.valueOf(id)); + map.put("employee_id", String.valueOf(item.get("id"))); + subReport.add(map); + reportList.add(subReport); + innerReportList = getNestedReport ((Long)item.get("id")); + if (innerReportList.size()>0){ + innerReportList.forEach(sub ->{ + for (int i=0; i