remove extra code (#1294)
This commit is contained in:
parent
6cc10132e1
commit
5392bdd8f3
@ -74,15 +74,6 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.boot</groupId>
|
|
||||||
<artifactId>spring-boot-starter-data-rest</artifactId>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>jstl</artifactId>
|
|
||||||
<version>1.2</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package org.baeldung.web.controller;
|
|
||||||
|
|
||||||
import org.springframework.stereotype.Controller;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
|
|
||||||
@Controller
|
|
||||||
public class EmployeeController {
|
|
||||||
|
|
||||||
@RequestMapping(value = "/employeePage")
|
|
||||||
public String getEmployeePage() {
|
|
||||||
return "employee";
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
package org.baeldung.web.dao;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.web.entity.Employee;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
|
|
||||||
|
|
||||||
@RepositoryRestResource(collectionResourceRel = "employee", path = "employees")
|
|
||||||
public interface EmployeeCRUDRepository extends CrudRepository<Employee, Long> {
|
|
||||||
List<Employee> findByName(@Param("name") String name);
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package org.baeldung.web.entity;
|
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Employee implements Serializable {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
|
||||||
|
|
||||||
@Id
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@Column(nullable = false)
|
|
||||||
private Integer age;
|
|
||||||
|
|
||||||
public Employee() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Employee(long id, String name, Integer age) {
|
|
||||||
super();
|
|
||||||
this.id = id;
|
|
||||||
this.name = name;
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getAge() {
|
|
||||||
return age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAge(Integer age) {
|
|
||||||
this.age = age;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
package org.baeldung.web.main;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
|
||||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
|
||||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@EnableWebMvc
|
|
||||||
@ComponentScan("org.baeldung.web.controller")
|
|
||||||
public class MvcConfig extends WebMvcConfigurerAdapter{
|
|
||||||
|
|
||||||
public MvcConfig(){
|
|
||||||
super();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
|
||||||
configurer.enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public ViewResolver viewResolver() {
|
|
||||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
|
||||||
|
|
||||||
bean.setPrefix("/WEB-INF/pages/");
|
|
||||||
bean.setSuffix(".html");
|
|
||||||
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -26,7 +26,7 @@ public class PersistenceConfig {
|
|||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() {
|
||||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
|
||||||
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").addScript("db/sql/employees.sql").build();
|
EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").build();
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
CREATE TABLE employee (
|
|
||||||
id INTEGER PRIMARY KEY,
|
|
||||||
name VARCHAR(30),
|
|
||||||
age INTEGER
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO employee (id,name,age)
|
|
||||||
VALUES (1,'Bryan',20);
|
|
||||||
INSERT INTO employee (id,name,age)
|
|
||||||
VALUES (2,'Lisa',30);
|
|
||||||
INSERT INTO employee (id,name,age)
|
|
||||||
VALUES (3,'Laura',40);
|
|
||||||
INSERT INTO employee (id,name,age)
|
|
||||||
VALUES (4,'Alex',35);
|
|
||||||
INSERT INTO employee (id,name,age)
|
|
||||||
VALUES (5,'John',47);
|
|
@ -1,55 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html ng-app="app">
|
|
||||||
<head>
|
|
||||||
<meta charset="ISO-8859-1">
|
|
||||||
<title>Employee CRUD</title>
|
|
||||||
<script
|
|
||||||
src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
|
|
||||||
<script
|
|
||||||
src="//cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
|
|
||||||
<script src="view/app.js"></script>
|
|
||||||
<style>
|
|
||||||
a {
|
|
||||||
cursor: pointer;
|
|
||||||
background-color: lightblue;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div ng-controller="EmployeeCRUDCtrl">
|
|
||||||
<table>
|
|
||||||
<tr>
|
|
||||||
<td width="100">ID:</td>
|
|
||||||
<td><input type="text" id="id" ng-model="employee.id" /></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td width="100">Name:</td>
|
|
||||||
<td><input type="text" id="name" ng-model="employee.name" /></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td width="100">Age:</td>
|
|
||||||
<td><input type="text" id="age" ng-model="employee.age" /></td>
|
|
||||||
</tr>
|
|
||||||
</table>
|
|
||||||
<br /> <br />
|
|
||||||
<a ng-click="getEmployee(employee.id)">Get employee</a>
|
|
||||||
<a ng-click="updateEmployee(employee.id,employee.name,employee.age)">Update employee</a>
|
|
||||||
<a ng-click="addEmployee(employee.id,employee.name,employee.age)">Add employee</a>
|
|
||||||
<a ng-click="deleteEmployee(employee.id)">Delete employee</a>
|
|
||||||
|
|
||||||
<br /> <br />
|
|
||||||
<p style="color: green">{{message}}</p>
|
|
||||||
<p style="color: red">{{errorMessage}}</p>
|
|
||||||
|
|
||||||
<br />
|
|
||||||
<br />
|
|
||||||
<a ng-click="getAllEmployees()">Get all Employees</a><br /> <br />
|
|
||||||
Name: <input type="text" id="name" ng-model="name" />
|
|
||||||
<a ng-click="getEmployeesByName()">Get employees by name</a>
|
|
||||||
<br /> <br />
|
|
||||||
<div ng-repeat="emp in employees">
|
|
||||||
{{emp.name}} {{emp.age}}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
@ -54,145 +54,3 @@ app.service('StudentService',['$http', function ($http) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
app.controller('EmployeeCRUDCtrl', ['$scope','EmployeeCRUDService', function ($scope,EmployeeCRUDService) {
|
|
||||||
|
|
||||||
$scope.updateEmployee = function () {
|
|
||||||
EmployeeCRUDService.updateEmployee($scope.employee.id,$scope.employee.name,$scope.employee.age)
|
|
||||||
.then(function success(response){
|
|
||||||
$scope.message = 'Employee data updated!';
|
|
||||||
$scope.errorMessage = '';
|
|
||||||
},
|
|
||||||
function error(response){
|
|
||||||
$scope.errorMessage = 'Error updating Employee!';
|
|
||||||
$scope.message = '';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.getEmployee = function () {
|
|
||||||
var id = $scope.employee.id;
|
|
||||||
EmployeeCRUDService.getEmployee($scope.employee.id)
|
|
||||||
.then(function success(response){
|
|
||||||
$scope.employee = response.data;
|
|
||||||
$scope.employee.id = id;
|
|
||||||
$scope.message='';
|
|
||||||
$scope.errorMessage = '';
|
|
||||||
},
|
|
||||||
function error (response ){
|
|
||||||
$scope.message = '';
|
|
||||||
if (response.status === 404){
|
|
||||||
$scope.errorMessage = 'Employee not found!';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$scope.errorMessage = "Error getting Employee!";
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.addEmployee = function () {
|
|
||||||
if ($scope.employee != null && $scope.employee.id) {
|
|
||||||
EmployeeCRUDService.addEmployee($scope.employee.id, $scope.employee.name, $scope.employee.age)
|
|
||||||
.then (function success(response){
|
|
||||||
$scope.message = 'Employee added!';
|
|
||||||
$scope.errorMessage = '';
|
|
||||||
},
|
|
||||||
function error(response){
|
|
||||||
$scope.errorMessage = 'Error adding Employee!';
|
|
||||||
$scope.message = '';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$scope.errorMessage = 'Please enter an id!';
|
|
||||||
$scope.message = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.deleteEmployee = function () {
|
|
||||||
EmployeeCRUDService.deleteEmployee($scope.employee.id)
|
|
||||||
.then (function success(response){
|
|
||||||
$scope.message = 'Employee deleted!';
|
|
||||||
$scope.employee = null;
|
|
||||||
$scope.errorMessage='';
|
|
||||||
},
|
|
||||||
function error(response){
|
|
||||||
$scope.errorMessage = 'Error deleting Employee!';
|
|
||||||
$scope.message='';
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.getAllEmployees = function () {
|
|
||||||
EmployeeCRUDService.getAllEmployees()
|
|
||||||
.then(function success(response){
|
|
||||||
$scope.employees = response.data._embedded.employee;
|
|
||||||
$scope.message='';
|
|
||||||
$scope.errorMessage = '';
|
|
||||||
},
|
|
||||||
function error (response ){
|
|
||||||
$scope.message='';
|
|
||||||
$scope.errorMessage = 'Error getting Employees!';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$scope.getEmployeesByName = function () {
|
|
||||||
EmployeeCRUDService.getEmployeesByName($scope.name)
|
|
||||||
.then(function success(response){
|
|
||||||
$scope.employees = response.data._embedded.employee;
|
|
||||||
$scope.message='';
|
|
||||||
$scope.errorMessage = '';
|
|
||||||
},
|
|
||||||
function error (response ){
|
|
||||||
$scope.message='';
|
|
||||||
$scope.errorMessage = 'Error getting Employees!';
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}]);
|
|
||||||
|
|
||||||
app.service('EmployeeCRUDService',['$http', function ($http) {
|
|
||||||
|
|
||||||
this.getEmployee = function getEmployee(employeeId){
|
|
||||||
return $http({
|
|
||||||
method: 'GET',
|
|
||||||
url:'employees/'+employeeId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.addEmployee = function addEmployee(id, name, age, gender){
|
|
||||||
return $http({
|
|
||||||
method: 'POST',
|
|
||||||
url:'employees',
|
|
||||||
data: {id:id, name:name, age:age}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.deleteEmployee = function deleteEmployee(id){
|
|
||||||
return $http({
|
|
||||||
method: 'DELETE',
|
|
||||||
url: 'employees/'+id
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.updateEmployee = function updateEmployee(id,name,age){
|
|
||||||
return $http({
|
|
||||||
method: 'PATCH',
|
|
||||||
url: 'employees/'+id,
|
|
||||||
data: {name:name, age:age}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getAllEmployees = function getAllEmployees(){
|
|
||||||
return $http({
|
|
||||||
method: 'GET',
|
|
||||||
url:'employees'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getEmployeesByName = function getEmployeesByName(name){
|
|
||||||
return $http({
|
|
||||||
method: 'GET',
|
|
||||||
url:'employees/search/findByName',
|
|
||||||
params:{name:name}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
}]);
|
|
@ -1,47 +0,0 @@
|
|||||||
package org.baeldung.web.service;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
import org.baeldung.web.entity.Employee;
|
|
||||||
import org.baeldung.web.main.Application;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)
|
|
||||||
public class EmployeeCRUDRepositoryIntegrationTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TestRestTemplate template;
|
|
||||||
|
|
||||||
private static final String EMPLOYEE_ENDPOINT = "http://localhost:8080/employees/";
|
|
||||||
private static int EMPLOYEE_ID = 1;
|
|
||||||
private static int EMPLOYEE_AGE = 25;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenEmployeeCRUDOperations_thenCorrect() {
|
|
||||||
Employee Employee = new Employee(EMPLOYEE_ID, "Bryan", 20);
|
|
||||||
ResponseEntity<Employee> postResponse = template.postForEntity(EMPLOYEE_ENDPOINT, Employee, Employee.class, "");
|
|
||||||
assertEquals("status is not 201", HttpStatus.CREATED, postResponse.getStatusCode());
|
|
||||||
|
|
||||||
Employee.setAge(EMPLOYEE_AGE);
|
|
||||||
Employee patchResponse = template.patchForObject(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee, Employee.class);
|
|
||||||
assertEquals("age is not 25", Integer.valueOf(EMPLOYEE_AGE), patchResponse.getAge());
|
|
||||||
|
|
||||||
ResponseEntity<Employee> getResponse = template.getForEntity(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee.class, "");
|
|
||||||
assertEquals("status is not 200", HttpStatus.OK, getResponse.getStatusCode());
|
|
||||||
|
|
||||||
template.delete(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID);
|
|
||||||
|
|
||||||
getResponse = template.getForEntity(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee.class, "");
|
|
||||||
assertEquals("status is not 404", HttpStatus.NOT_FOUND, getResponse.getStatusCode());
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user