move rest-angular app to existing rest project (#1293)

* move rest-angular app to existing rest project

* upgrade boot version
This commit is contained in:
lor6 2017-03-05 22:01:51 +02:00 committed by KevinGilmore
parent f0c4486cb1
commit 6cc10132e1
6 changed files with 205 additions and 5 deletions

View File

@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<version>1.5.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

View File

@ -0,0 +1,25 @@
package com.baeldung.config;
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
public class MvcConfig extends WebMvcConfigurerAdapter{
public MvcConfig(){
super();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}

View File

@ -2,9 +2,10 @@ package com.baeldung.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.web.bind.annotation.CrossOrigin;
import com.baeldung.models.WebsiteUser;
@CrossOrigin
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
public interface UserRepository extends CrudRepository<WebsiteUser, Long> {

View File

@ -0,0 +1,51 @@
<!DOCTYPE html>
<html ng-app="app">
<head>
<meta charset="ISO-8859-1">
<title>User CRUD</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="view/app.js"></script>
<style>
a {
cursor: pointer;
background-color: lightblue;
}
</style>
</head>
<body>
<div ng-controller="UserCRUDCtrl">
<table>
<tr>
<td width="100">ID:</td>
<td><input type="text" id="id" ng-model="user.id" /></td>
</tr>
<tr>
<td width="100">Name:</td>
<td><input type="text" id="name" ng-model="user.name" /></td>
</tr>
<tr>
<td width="100">Email:</td>
<td><input type="text" id="email" ng-model="user.email" /></td>
</tr>
</table>
<br /> <br />
<a ng-click="getUser(user.id)">Get User</a>
<a ng-click="updateUser(user.id,user.name,user.email)">Update User</a>
<a ng-click="addUser(user.name,user.email)">Add User</a>
<a ng-click="deleteUser(user.id)">Delete User</a>
<br /> <br />
<p style="color: green">{{message}}</p>
<p style="color: red">{{errorMessage}}</p>
<br />
<br />
<a ng-click="getAllUsers()">Get all Users</a><br />
<br /> <br />
<div ng-repeat="usr in users">
{{usr.name}} {{usr.email}}
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,122 @@
var app = angular.module('app',[]);
app.controller('UserCRUDCtrl', ['$scope','UserCRUDService', function ($scope,UserCRUDService) {
$scope.updateUser = function () {
UserCRUDService.updateUser($scope.user.id,$scope.user.name,$scope.user.email)
.then(function success(response){
$scope.message = 'User data updated!';
$scope.errorMessage = '';
},
function error(response){
$scope.errorMessage = 'Error updating user!';
$scope.message = '';
});
}
$scope.getUser = function () {
var id = $scope.user.id;
UserCRUDService.getUser($scope.user.id)
.then(function success(response){
$scope.user = response.data;
$scope.user.id = id;
$scope.message='';
$scope.errorMessage = '';
},
function error (response ){
$scope.message = '';
if (response.status === 404){
$scope.errorMessage = 'User not found!';
}
else {
$scope.errorMessage = "Error getting user!";
}
});
}
$scope.addUser = function () {
if ($scope.user != null && $scope.user.name) {
UserCRUDService.addUser($scope.user.name, $scope.user.email)
.then (function success(response){
$scope.message = 'User added!';
$scope.errorMessage = '';
},
function error(response){
$scope.errorMessage = 'Error adding user!';
$scope.message = '';
});
}
else {
$scope.errorMessage = 'Please enter a name!';
$scope.message = '';
}
}
$scope.deleteUser = function () {
UserCRUDService.deleteUser($scope.user.id)
.then (function success(response){
$scope.message = 'User deleted!';
$scope.user = null;
$scope.errorMessage='';
},
function error(response){
$scope.errorMessage = 'Error deleting user!';
$scope.message='';
})
}
$scope.getAllUsers = function () {
UserCRUDService.getAllUsers()
.then(function success(response){
$scope.users = response.data._embedded.users;
$scope.message='';
$scope.errorMessage = '';
},
function error (response ){
$scope.message='';
$scope.errorMessage = 'Error getting users!';
});
}
}]);
app.service('UserCRUDService',['$http', function ($http) {
this.getUser = function getUser(userId){
return $http({
method: 'GET',
url: 'users/'+userId
});
}
this.addUser = function addUser(name, email){
return $http({
method: 'POST',
url: 'users',
data: {name:name, email:email}
});
}
this.deleteUser = function deleteUser(id){
return $http({
method: 'DELETE',
url: 'users/'+id
})
}
this.updateUser = function updateUser(id,name,email){
return $http({
method: 'PATCH',
url: 'users/'+id,
data: {name:name, email:email}
})
}
this.getAllUsers = function getAllUsers(){
return $http({
method: 'GET',
url: 'users'
});
}
}]);

View File

@ -18,6 +18,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.json.JSONException;
import static org.junit.Assert.assertEquals;
@ -37,7 +38,7 @@ public class SpringDataRelationshipsTest {
private static final String AUTHOR_NAME = "George Orwell";
@Test
public void whenSaveOneToOneRelationship_thenCorrect() {
public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException {
Library library = new Library(LIBRARY_NAME);
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class);
@ -55,7 +56,7 @@ public class SpringDataRelationshipsTest {
}
@Test
public void whenSaveOneToManyRelationship_thenCorrect() {
public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{
Library library = new Library(LIBRARY_NAME);
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class);
@ -77,7 +78,7 @@ public class SpringDataRelationshipsTest {
}
@Test
public void whenSaveManyToManyRelationship_thenCorrect() {
public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{
Author author1 = new Author(AUTHOR_NAME);
template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class);