Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.sakadream.controllers;
import com.sakadream.models.Employee;
import com.sakadream.services.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static org.springframework.http.HttpStatus.*;
import static org.springframework.web.bind.annotation.RequestMethod.*;
/**
* Created by Phan Ba Hai on 25/08/2017.
*/
@RestController
public class EmployeeController {
@Autowired
EmployeeService service;
@RequestMapping(value = "/employees", method = GET)
public ResponseEntity<List<Employee>> getAllEmployees() {
List<Employee> employees = service.getAllEmployees();
if(employees.isEmpty()) {
System.out.println("Employees Not Found!");
return new ResponseEntity<List<Employee>>(NO_CONTENT);
} else {
System.out.println("Employees Found!");
return new ResponseEntity<List<Employee>>(employees, OK);
}
}
@RequestMapping(value = "/employee", params = "id", method = GET)
public ResponseEntity<Employee> getById(@RequestParam int id) {
Employee employee = service.findById(id);
if(employee == null) {
System.out.println("Employee ID " + id + " Not Found!");
return new ResponseEntity<Employee>(NOT_FOUND);
} else {
System.out.println("Employee ID " + id + " Found!");
return new ResponseEntity<Employee>(employee, OK);
}
}
@RequestMapping(value = "/employee", params = "name", method = GET)
public ResponseEntity<Employee> getByName(@RequestParam String name) {
Employee employee = service.findByName(name);
if(employee.getId() == 0) {
System.out.println("Employee Name " + name + " Not Found!");
return new ResponseEntity<Employee>(NOT_FOUND);
} else {
System.out.println("Employee Name " + name + " Found!");
return new ResponseEntity<Employee>(employee, OK);
}
}
@RequestMapping(value = "/addEmployee", method = POST)
public ResponseEntity<Void> addEmployee(@RequestBody Employee employee) {
if(service.addEmployee(employee)) {
System.out.println("Created New Employee!");
return new ResponseEntity<Void>(OK);
} else {
System.out.println("Can not create new employee!");
return new ResponseEntity<Void>(BAD_REQUEST);
}
}
@RequestMapping(value = "/updateEmployee", method = PUT)
public ResponseEntity<Void> updateEmployee(@RequestParam("id") int id, @RequestBody Employee employee) {
if(service.updateEmployee(id, employee)) {
System.out.println("Updated Employee with ID " + id);
return new ResponseEntity<Void>(OK);
} else {
System.out.println("Employee ID " + id + " Not Found!");
return new ResponseEntity<Void>(NOT_FOUND);
}
}
@RequestMapping(value = "/deleteEmployee", method = DELETE)
public ResponseEntity<Void> deleteEmployee(@RequestParam("id") int id) {
if(service.deleteEmployee(id)) {
System.out.println("Deleted Employee with ID " + id);
return new ResponseEntity<Void>(OK);
} else {
System.out.println("Employee ID " + id + " Not Found!");
return new ResponseEntity<Void>(NOT_FOUND);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.