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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
46 changes: 32 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,63 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>io.zipcoder</groupId>
<artifactId>persistence-starter</artifactId>
<artifactId>employee-directory</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>persistence-starter</name>
<description>Spring Boot Starter project for persistence projects</description>
<name>Employee Directory</name>
<description>Spring Boot Persistence Project: Employee Directory Application</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
<version>2.7.18</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Spring Boot JPA / Hibernate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- Spring Web (REST Controllers) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- H2 Database (in-memory) -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>

<!-- Validation (for @NotEmpty, @Size, etc.) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<!-- Spring Boot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Swagger / OpenAPI UI -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>

</dependencies>

<build>
<plugins>
<!-- Spring Boot Plugin -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.zipcoder.persistenceapp;

import org.h2.server.web.WebServlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;


@SpringBootApplication
public class PersistenceStarterApplication {
Expand All @@ -13,10 +12,4 @@ public static void main(String[] args) {
SpringApplication.run(PersistenceStarterApplication.class, args);
}

@Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.zipcoder.persistenceapp.controller;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.zipcoder.persistenceapp.domain.Department;
import io.zipcoder.persistenceapp.domain.Employee;
import io.zipcoder.persistenceapp.services.DepartmentService;
import io.zipcoder.persistenceapp.services.EmployeeService;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/API/departments")
public class DepartmentController {

private final DepartmentService departments;
private final EmployeeService employees;

public DepartmentController(DepartmentService departments, EmployeeService employees) {
this.departments = departments;
this.employees = employees;
}

// Create a Department
@PostMapping
public ResponseEntity<Department> create(@Valid @RequestBody Department d) {
return new ResponseEntity<>(departments.create(d), HttpStatus.CREATED);
}

// Set a new department manager
@PutMapping("/{id}/manager/{managerEmpId}")
public ResponseEntity<Department> setManager(@PathVariable Long id, @PathVariable Long managerEmpId) {
return ResponseEntity.ok(departments.setManager(id, managerEmpId));
}

// Change department name
@PutMapping("/{id}/name")
public ResponseEntity<Department> rename(@PathVariable Long id, @RequestParam("value") String newName) {
return ResponseEntity.ok(departments.rename(id, newName));
}

// Get all employees of a department
@GetMapping("/{id}/employees")
public ResponseEntity<List<Employee>> listEmployees(@PathVariable Long id) {
return ResponseEntity.ok(employees.getByDepartment(id));
}

// Remove all employees from a department
@DeleteMapping("/{id}/employees")
public ResponseEntity<Void> deleteAllInDepartment(@PathVariable Long id) {
employees.deleteByDepartment(id);
return ResponseEntity.ok().build();
}

// Merge departments by names: /API/departments/merge?from=B&to=A
@PostMapping("/merge")
public ResponseEntity<Void> mergeByNames(@RequestParam("from") String fromName,
@RequestParam("to") String toName) {
departments.mergeByNames(fromName, toName);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package io.zipcoder.persistenceapp.controller;


import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.zipcoder.persistenceapp.domain.Employee;
import io.zipcoder.persistenceapp.services.EmployeeService;

import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("/API/employees")
public class EmployeeController {

private final EmployeeService employees;

public EmployeeController(EmployeeService employees) {
this.employees = employees;
}

// Create employee
@PostMapping
public ResponseEntity<Employee> createEmployee(@Valid @RequestBody Employee e) {
Employee saved = employees.create(e);
return new ResponseEntity<>(saved, HttpStatus.CREATED);
}

// Update employee fields (partial)
@PutMapping("/{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable Long id, @RequestBody Employee patch) {
Employee updated = employees.update(id, patch);
return new ResponseEntity<>(updated, HttpStatus.OK);
}

// Set manager
@PutMapping("/{id}/manager/{managerId}")
public ResponseEntity<Employee> setManager(@PathVariable Long id, @PathVariable Long managerId) {
Employee updated = employees.setManager(id, managerId);
return new ResponseEntity<>(updated, HttpStatus.OK);
}

// Get attributes of a particular employee (full object)
@GetMapping("/{id}")
public ResponseEntity<Employee> getEmployee(@PathVariable Long id) {
return ResponseEntity.ok(employees.getEmployee(id));
}

// Get hierarchy upwards (manager, manager's manager, ...)
@GetMapping("/{id}/manager-chain")
public ResponseEntity<List<Employee>> getManagerChain(@PathVariable Long id) {
return ResponseEntity.ok(employees.getManagerChain(id));
}

// List employees with no assigned manager
@GetMapping("/unmanaged")
public ResponseEntity<List<Employee>> unmanaged() {
return ResponseEntity.ok(employees.getUnmanaged());
}

// GET /api/employees/{id}/hierarchy
@GetMapping("/{id}/hierarchy")
public List<Employee> getHierarchy(@PathVariable Long id) {
return employees.getHierarchy(id);
}

// Remove a particular employee or list via query param: /API/employees?ids=1,2,3
@DeleteMapping
public ResponseEntity<Void> deleteByIds(@RequestParam("ids") List<Long> ids) {
employees.deleteEmployeesByIds(ids);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.zipcoder.persistenceapp.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.zipcoder.persistenceapp.domain.Employee;
import io.zipcoder.persistenceapp.services.EmployeeService;

import java.util.List;

@RestController
@RequestMapping("/API/managers")
public class ManagerController {

private final EmployeeService employees;

public ManagerController(EmployeeService employees) {
this.employees = employees;
}

// Get direct reports
@GetMapping("/{id}/reports")
public ResponseEntity<List<Employee>> directReports(@PathVariable Long id) {
return ResponseEntity.ok(employees.getDirectReports(id));
}

// Get direct+indirect reports: /API/managers/{id}/reports/all
@GetMapping("/{id}/reports/all")
public ResponseEntity<List<Employee>> allReports(@PathVariable Long id) {
return ResponseEntity.ok(employees.getAllReportsRecursive(id));
}

// Remove all employees under a manager (including indirect)
@DeleteMapping("/{id}/reports/all")
public ResponseEntity<Void> deleteAllUnder(@PathVariable Long id) {
employees.deleteAllUnderManagerRecursive(id);
return ResponseEntity.ok().build();
}

// Remove only direct reports and reassign their reports upwards
@DeleteMapping("/{id}/reports")
public ResponseEntity<Void> deleteDirectReports(@PathVariable Long id) {
employees.deleteDirectReports(id);
return ResponseEntity.ok().build();
}
}
Loading