JAVA-84: Moved 1 article to spring-boot-testing

This commit is contained in:
sampadawagde
2020-08-28 17:57:38 +05:30
parent 52721a78d6
commit 5749d7ad23
27 changed files with 70 additions and 544 deletions
@@ -0,0 +1,46 @@
package com.baeldung.boot.testing;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import java.util.Date;
@Entity
@Table(name = "person")
public class Employee {
public Employee() {
}
public Employee(String name) {
this.name = name;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min = 3, max = 20)
private String name;
private Date birthday;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.boot.testing;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public Employee findByName(String name);
public List<Employee> findAll();
}
@@ -0,0 +1,33 @@
package com.baeldung.boot.testing;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class EmployeeRestController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/employees")
public ResponseEntity<Employee> createEmployee(@RequestBody Employee employee) {
HttpStatus status = HttpStatus.CREATED;
Employee saved = employeeService.save(employee);
return new ResponseEntity<>(saved, status);
}
@GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeService.getAllEmployees();
}
}
@@ -0,0 +1,16 @@
package com.baeldung.boot.testing;
import java.util.List;
public interface EmployeeService {
public Employee getEmployeeById(Long id);
public Employee getEmployeeByName(String name);
public List<Employee> getAllEmployees();
public boolean exists(String email);
public Employee save(Employee employee);
}
@@ -0,0 +1,43 @@
package com.baeldung.boot.testing;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public Employee getEmployeeById(Long id) {
return employeeRepository.findById(id).orElse(null);
}
@Override
public Employee getEmployeeByName(String name) {
return employeeRepository.findByName(name);
}
@Override
public boolean exists(String name) {
if (employeeRepository.findByName(name) != null) {
return true;
}
return false;
}
@Override
public Employee save(Employee employee) {
return employeeRepository.save(employee);
}
@Override
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
}