Image 1
Note - Model and Repository layer completed
Service Class Updates
springboot-backendd--> src/main/java--> net.javaguides.springboot.service--> EmployeeService.java
package net.javaguides.springboot.service;
import java.util.List;
import net.javaguides.springboot.model.Employee;
public interface EmployeeService {
Employee saveEmployee(Employee employee);
List<Employee> getAllEmployees();
Employee getEmployeeById(long id);
Employee updateEmployee(Employee employee, long id);
}
ServiceImpl Class Updates
springboot-backendd--> src/main/java--> net.javaguides.springboot.service.impl--> EmployeeServiceImpl.java
package net.javaguides.springboot.service.impl;
import java.util.List;
import org.springframework.stereotype.Service;
import net.javaguides.springboot.exception.ResourceNotFound;
import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.repository.EmployeeRepository;
import net.javaguides.springboot.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
super();
this.employeeRepository = employeeRepository;
}
@Override
public Employee saveEmployee(Employee employee) {
return employeeRepository.save(employee);
}
@Override
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
@Override
public Employee getEmployeeById(long id) {
// Optional<Employee> employee = employeeRepository.findById(id);
// if(employee.isPresent()) {
// return employee.get();
// }else {
// throw new ResourceNotFoundException("Employee", "Id", id);
// }
return employeeRepository.findById(id).orElseThrow(() ->
new ResourceNotFound("Employee", "Id", id));
}
@Override
public Employee updateEmployee(Employee employee, long id) {
// we need to check whether employee with given id is exist in DataBase or not
Employee existingEmployee = employeeRepository.findById(id).orElseThrow(
() -> new ResourceNotFound("Employee", "Id", id));
//Make changes to the employee
existingEmployee.setFirstName(employee.getFirstName());
existingEmployee.setLastName(employee.getLastName());
existingEmployee.setEmail(employee.getEmail());
// save existing(updated) employee to DataBase
employeeRepository.save(existingEmployee);
return existingEmployee;
}
}
Controller Class Updates
springboot-backendd--> src/main/java--> net.javaguides.springboot.controller--> EmployeeController.java
package net.javaguides.springboot.controller;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.javaguides.springboot.model.Employee;
import net.javaguides.springboot.service.EmployeeService;
@RestController
@RequestMapping("/api/employees")
public class EmployeeController {
private EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
super();
this.employeeService = employeeService;
}
// build create employee REST API
// http://localhost:8080/api/employees------- POST request on Postman App
@PostMapping()
public ResponseEntity<Employee> saveEmployee(@RequestBody Employee employee){
return new ResponseEntity<Employee>(employeeService.saveEmployee(employee),HttpStatus.CREATED);
}
// build get all employees REST API
// http://localhost:8080/api/employees------- GET request on Postman App
@GetMapping
public List<Employee> getAllEmployees(){
return employeeService.getAllEmployees();
}
// build get employee by id REST API
// http://localhost:8080/api/employees/1---------- GET request on Postman App
@GetMapping("{id}")
public ResponseEntity<Employee> getEmployeeById(@PathVariable("id") long employeeId){
return new ResponseEntity<Employee>(employeeService.getEmployeeById(employeeId), HttpStatus.OK);
}
// build update employee REST API
// http://localhost:8080/api/employees/1---------- PUT request on Postman App
@PutMapping("{id}")
public ResponseEntity<Employee> updateEmployee(@PathVariable("id") long id,@RequestBody Employee employee){
return new ResponseEntity<Employee>(employeeService.updateEmployee(employee, id), HttpStatus.OK);
}
Comments
Post a Comment