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
@@ -0,0 +1,52 @@
package io.zipcoder.persistenceapp.controllers;

import io.zipcoder.persistenceapp.entities.Department;
import io.zipcoder.persistenceapp.services.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/department/")
public class DepartmentController {

DepartmentService departmentService;

@Autowired
public DepartmentController(DepartmentService departmentService){
this.departmentService = departmentService;
}

@GetMapping
public ResponseEntity<Iterable<Department>> getAllDepartments(){
return new ResponseEntity<>(departmentService.getAllDepartments(), HttpStatus.OK);
}

@GetMapping("{id}/name")
public String getDepartmentName (@PathVariable Long id){
return departmentService.getDepartmentName(id);
}

@GetMapping("{id}")
public ResponseEntity<Department> getDepartmentById(@PathVariable Long id){
return new ResponseEntity<Department>(departmentService.getDepartmentById(id),HttpStatus.OK);
}

@PutMapping("{id}")
public ResponseEntity<Department> updateDepartmentManager(@RequestBody Department newData, @PathVariable Long id){
return new ResponseEntity<Department>(departmentService.updateDepartmentManager(newData, id),HttpStatus.OK);
}

@PutMapping("{id}/name")
public ResponseEntity<Department> updateDepartmentName(@RequestBody Department newData, @PathVariable Long id){
return new ResponseEntity<Department>(departmentService.updateDepartmentName(newData, id),HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Department> createDepartment(@RequestBody Department newDepartment){
return new ResponseEntity<Department>(departmentService.createDepartment(newDepartment),HttpStatus.CREATED);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.zipcoder.persistenceapp.controllers;

import io.zipcoder.persistenceapp.entities.Employee;
import io.zipcoder.persistenceapp.services.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

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

EmployeeService employeeService;

@Autowired
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}

@GetMapping("allundermanager/{id}")
public ResponseEntity<Iterable<Employee>> getAllEmployeesUnderManager(@PathVariable Long id) {
return new ResponseEntity<>(employeeService.getAllEmployeesUnderManager(id), HttpStatus.OK);
}

@GetMapping("allindepartment/{id}")
public ResponseEntity<Iterable<Employee>> getAllEmployeesInDepartment(@PathVariable Long id) {
return new ResponseEntity<>(employeeService.getAllEmployeesInDepartment(id), HttpStatus.OK);
}

@GetMapping("allnomanager")
public ResponseEntity<Iterable<Employee>> getAllEmployeesNoManager() {
return new ResponseEntity<>(employeeService.getAllEmployeesNoManager(), HttpStatus.OK);
}

@GetMapping("hierarchy/{id}")
public ResponseEntity<Iterable<Employee>> getHierarchy(@PathVariable Long id) {
return new ResponseEntity<>(employeeService.getHierarchy(id), HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Employee> createEmployee (@RequestBody Employee newEmployee){
return new ResponseEntity<>(employeeService.createEmployee(newEmployee),HttpStatus.CREATED);
}

@PutMapping("{id}")
public ResponseEntity<Employee> updateManager (@RequestBody Employee updatedData, @PathVariable Long id){
return new ResponseEntity<>(employeeService.updatedData(updatedData, id),HttpStatus.OK);
}

@DeleteMapping("{id}")
public ResponseEntity<Boolean> deleteEmployee (@PathVariable Long id){
return new ResponseEntity<>(employeeService.deleteEmployee(id),HttpStatus.OK);
}

@DeleteMapping("manager/{id}")
public ResponseEntity<Boolean> deleteEmployeeByManager(@PathVariable Long id){
return new ResponseEntity<>(employeeService.deleteEmployeeByManager(id),HttpStatus.OK);
}

@DeleteMapping("department/{id}")
public ResponseEntity<Boolean> deleteEmployeesByDepartment(@PathVariable Long id){
return new ResponseEntity<>(employeeService.deleteEmployeesByDepartment(id), HttpStatus.OK);
}

@PutMapping("mergedepartment/{deptfrom}/{deptto}")
public ResponseEntity<Iterable<Employee>> mergeDepartments(@PathVariable Long deptfrom, @PathVariable Long deptto){
return new ResponseEntity<>(employeeService.mergeDepartments(deptfrom,deptto),HttpStatus.OK);
}

// TODO
/*

* remove all direct reports to a manager. Any employees previously managed by the deleted employees
should now be managed by the next manager up the hierarchy.

*/
}
39 changes: 39 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/entities/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.zipcoder.persistenceapp.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long DEPARTMENT_ID;

private String DEPARTMENT_NAME;
private Long DEPARTMENT_MANAGER;


public Long getDEPARTMENT_ID() {
return DEPARTMENT_ID;
}
public void setDEPARTMENT_ID (Long id){ this.DEPARTMENT_ID = id;}

public String getDEPARTMENT_NAME() {
return DEPARTMENT_NAME;
}

public void setDEPARTMENT_NAME(String DEPARTMENT_NAME) {
this.DEPARTMENT_NAME = DEPARTMENT_NAME;
}

public Long getDEPARTMENT_MANAGER() {
return DEPARTMENT_MANAGER;
}

public void setDEPARTMENT_MANAGER(Long DEPARTMENT_MANAGER) {
this.DEPARTMENT_MANAGER = DEPARTMENT_MANAGER;
}
}
84 changes: 84 additions & 0 deletions src/main/java/io/zipcoder/persistenceapp/entities/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.zipcoder.persistenceapp.entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long EMPLOYEE_ID;

private String FIRST_NAME;
private String LAST_NAME;
private String TITLE;
private String PHONE;
private Date HIRE_DATE;
private Long MANAGER_ID;
private Long DEPARTMENT_ID;


public Long getEMPLOYEE_ID() {
return EMPLOYEE_ID;
}

public String getFIRST_NAME() {
return FIRST_NAME;
}

public void setFIRST_NAME(String FIRST_NAME) {
this.FIRST_NAME = FIRST_NAME;
}

public String getLAST_NAME() {
return LAST_NAME;
}

public void setLAST_NAME(String LAST_NAME) {
this.LAST_NAME = LAST_NAME;
}

public String getTITLE() {
return TITLE;
}

public void setTITLE(String TITLE) {
this.TITLE = TITLE;
}

public String getPHONE() {
return PHONE;
}

public void setPHONE(String PHONE) {
this.PHONE = PHONE;
}

public Date getHIRE_DATE() {
return HIRE_DATE;
}

public void setHIRE_DATE(Date HIRE_DATE) {
this.HIRE_DATE = HIRE_DATE;
}

public Long getMANAGER_ID() {
return MANAGER_ID;
}

public void setMANAGER_ID(Long MANAGER_ID) {
this.MANAGER_ID = MANAGER_ID;
}

public Long getDEPARTMENT_ID() {
return DEPARTMENT_ID;
}

public void setDEPARTMENT_ID(Long DEPARTMENT_ID) {
this.DEPARTMENT_ID = DEPARTMENT_ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.zipcoder.persistenceapp.exceptions;

public class ResourceNotFoundException extends RuntimeException{
public ResourceNotFoundException() {
}

public ResourceNotFoundException(String message) {
super(message);
}

public ResourceNotFoundException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.zipcoder.persistenceapp.repositories;

import io.zipcoder.persistenceapp.entities.Department;
import io.zipcoder.persistenceapp.entities.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends CrudRepository<Department,Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.zipcoder.persistenceapp.repositories;

import io.zipcoder.persistenceapp.entities.Employee;
import org.hibernate.annotations.SQLDelete;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends CrudRepository<Employee,Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.zipcoder.persistenceapp.services;

import io.zipcoder.persistenceapp.entities.Department;
import io.zipcoder.persistenceapp.exceptions.ResourceNotFoundException;
import io.zipcoder.persistenceapp.repositories.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DepartmentService {

DepartmentRepository departmentRepository;

@Autowired
public DepartmentService(DepartmentRepository departmentRepository){
this.departmentRepository = departmentRepository;
}

public Iterable<Department> getAllDepartments(){
return departmentRepository.findAll();
}

public Department getDepartmentById(Long id){
verifyDepartment(id);
return departmentRepository.findOne(id);
}

public String getDepartmentName(Long id){
verifyDepartment(id);
return getDepartmentById(id).getDEPARTMENT_NAME();
}

public Department updateDepartmentManager(Department newData, Long ID){

verifyDepartment(ID);

Department existingData = departmentRepository.findOne(ID);
existingData.setDEPARTMENT_MANAGER(newData.getDEPARTMENT_MANAGER());
return departmentRepository.save(existingData);
}

public Department updateDepartmentName(Department newData, Long id){

verifyDepartment(id);

Department existingData = departmentRepository.findOne(id);
existingData.setDEPARTMENT_NAME(newData.getDEPARTMENT_NAME());
return departmentRepository.save(existingData);
}

public Department createDepartment (Department newDepartment){
return departmentRepository.save(newDepartment);
}

private void verifyDepartment(Long departmentId){
if (departmentRepository.exists(departmentId)){

} else {
throw new ResourceNotFoundException("Department not found!");
}
}

}
Loading