Skip to content

Commit 6cc1013

Browse files
lor6KevinGilmore
authored andcommitted
move rest-angular app to existing rest project (eugenp#1293)
* move rest-angular app to existing rest project * upgrade boot version
1 parent f0c4486 commit 6cc1013

6 files changed

Lines changed: 205 additions & 5 deletions

File tree

‎spring-data-rest/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.4.4.RELEASE</version>
17+
<version>1.5.1.RELEASE</version>
1818
<relativePath /> <!-- lookup parent from repository -->
1919
</parent>
2020

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
10+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
11+
12+
@Configuration
13+
@EnableWebMvc
14+
public class MvcConfig extends WebMvcConfigurerAdapter{
15+
16+
public MvcConfig(){
17+
super();
18+
}
19+
20+
@Override
21+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
22+
configurer.enable();
23+
}
24+
25+
}

‎spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import org.springframework.data.repository.CrudRepository;
44
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5-
5+
import org.springframework.web.bind.annotation.CrossOrigin;
66
import com.baeldung.models.WebsiteUser;
77

8+
@CrossOrigin
89
@RepositoryRestResource(collectionResourceRel = "users", path = "users")
910
public interface UserRepository extends CrudRepository<WebsiteUser, Long> {
1011

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html ng-app="app">
3+
<head>
4+
<meta charset="ISO-8859-1">
5+
<title>User CRUD</title>
6+
<script
7+
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
8+
<script src="view/app.js"></script>
9+
<style>
10+
a {
11+
cursor: pointer;
12+
background-color: lightblue;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
<div ng-controller="UserCRUDCtrl">
18+
<table>
19+
<tr>
20+
<td width="100">ID:</td>
21+
<td><input type="text" id="id" ng-model="user.id" /></td>
22+
</tr>
23+
<tr>
24+
<td width="100">Name:</td>
25+
<td><input type="text" id="name" ng-model="user.name" /></td>
26+
</tr>
27+
<tr>
28+
<td width="100">Email:</td>
29+
<td><input type="text" id="email" ng-model="user.email" /></td>
30+
</tr>
31+
</table>
32+
<br /> <br />
33+
<a ng-click="getUser(user.id)">Get User</a>
34+
<a ng-click="updateUser(user.id,user.name,user.email)">Update User</a>
35+
<a ng-click="addUser(user.name,user.email)">Add User</a>
36+
<a ng-click="deleteUser(user.id)">Delete User</a>
37+
38+
<br /> <br />
39+
<p style="color: green">{{message}}</p>
40+
<p style="color: red">{{errorMessage}}</p>
41+
42+
<br />
43+
<br />
44+
<a ng-click="getAllUsers()">Get all Users</a><br />
45+
<br /> <br />
46+
<div ng-repeat="usr in users">
47+
{{usr.name}} {{usr.email}}
48+
</div>
49+
</div>
50+
</body>
51+
</html>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
var app = angular.module('app',[]);
2+
3+
app.controller('UserCRUDCtrl', ['$scope','UserCRUDService', function ($scope,UserCRUDService) {
4+
5+
$scope.updateUser = function () {
6+
UserCRUDService.updateUser($scope.user.id,$scope.user.name,$scope.user.email)
7+
.then(function success(response){
8+
$scope.message = 'User data updated!';
9+
$scope.errorMessage = '';
10+
},
11+
function error(response){
12+
$scope.errorMessage = 'Error updating user!';
13+
$scope.message = '';
14+
});
15+
}
16+
17+
$scope.getUser = function () {
18+
var id = $scope.user.id;
19+
UserCRUDService.getUser($scope.user.id)
20+
.then(function success(response){
21+
$scope.user = response.data;
22+
$scope.user.id = id;
23+
$scope.message='';
24+
$scope.errorMessage = '';
25+
},
26+
function error (response ){
27+
$scope.message = '';
28+
if (response.status === 404){
29+
$scope.errorMessage = 'User not found!';
30+
}
31+
else {
32+
$scope.errorMessage = "Error getting user!";
33+
}
34+
});
35+
}
36+
37+
$scope.addUser = function () {
38+
if ($scope.user != null && $scope.user.name) {
39+
UserCRUDService.addUser($scope.user.name, $scope.user.email)
40+
.then (function success(response){
41+
$scope.message = 'User added!';
42+
$scope.errorMessage = '';
43+
},
44+
function error(response){
45+
$scope.errorMessage = 'Error adding user!';
46+
$scope.message = '';
47+
});
48+
}
49+
else {
50+
$scope.errorMessage = 'Please enter a name!';
51+
$scope.message = '';
52+
}
53+
}
54+
55+
$scope.deleteUser = function () {
56+
UserCRUDService.deleteUser($scope.user.id)
57+
.then (function success(response){
58+
$scope.message = 'User deleted!';
59+
$scope.user = null;
60+
$scope.errorMessage='';
61+
},
62+
function error(response){
63+
$scope.errorMessage = 'Error deleting user!';
64+
$scope.message='';
65+
})
66+
}
67+
68+
$scope.getAllUsers = function () {
69+
UserCRUDService.getAllUsers()
70+
.then(function success(response){
71+
$scope.users = response.data._embedded.users;
72+
$scope.message='';
73+
$scope.errorMessage = '';
74+
},
75+
function error (response ){
76+
$scope.message='';
77+
$scope.errorMessage = 'Error getting users!';
78+
});
79+
}
80+
81+
}]);
82+
83+
app.service('UserCRUDService',['$http', function ($http) {
84+
85+
this.getUser = function getUser(userId){
86+
return $http({
87+
method: 'GET',
88+
url: 'users/'+userId
89+
});
90+
}
91+
92+
this.addUser = function addUser(name, email){
93+
return $http({
94+
method: 'POST',
95+
url: 'users',
96+
data: {name:name, email:email}
97+
});
98+
}
99+
100+
this.deleteUser = function deleteUser(id){
101+
return $http({
102+
method: 'DELETE',
103+
url: 'users/'+id
104+
})
105+
}
106+
107+
this.updateUser = function updateUser(id,name,email){
108+
return $http({
109+
method: 'PATCH',
110+
url: 'users/'+id,
111+
data: {name:name, email:email}
112+
})
113+
}
114+
115+
this.getAllUsers = function getAllUsers(){
116+
return $http({
117+
method: 'GET',
118+
url: 'users'
119+
});
120+
}
121+
122+
}]);

‎spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.springframework.http.HttpMethod;
1919
import org.springframework.http.ResponseEntity;
2020
import org.springframework.test.context.junit4.SpringRunner;
21+
import org.json.JSONException;
2122

2223
import static org.junit.Assert.assertEquals;
2324

@@ -37,7 +38,7 @@ public class SpringDataRelationshipsTest {
3738
private static final String AUTHOR_NAME = "George Orwell";
3839

3940
@Test
40-
public void whenSaveOneToOneRelationship_thenCorrect() {
41+
public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException {
4142
Library library = new Library(LIBRARY_NAME);
4243
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class);
4344

@@ -55,7 +56,7 @@ public void whenSaveOneToOneRelationship_thenCorrect() {
5556
}
5657

5758
@Test
58-
public void whenSaveOneToManyRelationship_thenCorrect() {
59+
public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{
5960
Library library = new Library(LIBRARY_NAME);
6061
template.postForEntity(LIBRARY_ENDPOINT, library, Library.class);
6162

@@ -77,7 +78,7 @@ public void whenSaveOneToManyRelationship_thenCorrect() {
7778
}
7879

7980
@Test
80-
public void whenSaveManyToManyRelationship_thenCorrect() {
81+
public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{
8182
Author author1 = new Author(AUTHOR_NAME);
8283
template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class);
8384

0 commit comments

Comments
 (0)