pt. 4 - AngularJS POST and @RepositoryEventHandler
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
package com.bayer.burp.repository;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
|
||||||
|
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.bayer.burp.domain.Burp;
|
||||||
|
import com.bayer.burp.domain.Tag;
|
||||||
|
|
||||||
|
// see https://spring.io/blog/2015/10/28/react-js-and-spring-data-rest-part-5-security
|
||||||
|
@Component
|
||||||
|
@RepositoryEventHandler(Burp.class)
|
||||||
|
public class SpringDataRestEventHandler {
|
||||||
|
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
private final TagRepository tagRepository;
|
||||||
|
|
||||||
|
private static final Pattern TAG_PATTERN = Pattern.compile("(?:\\s|\\A|^)[#]+([A-Za-z0-9-_]+)");
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public SpringDataRestEventHandler(UserRepository userRepository, TagRepository tagRepository) {
|
||||||
|
this.userRepository = userRepository;
|
||||||
|
this.tagRepository = tagRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@HandleBeforeCreate
|
||||||
|
public void applyUserInformationUsingSecurityContext(Burp burp) {
|
||||||
|
|
||||||
|
String username = SecurityContextHolder.getContext().getAuthentication().getName();
|
||||||
|
burp.setUser(userRepository.findByName(username));
|
||||||
|
|
||||||
|
Matcher matcher = TAG_PATTERN.matcher(burp.getText());
|
||||||
|
List<Tag> tags = new ArrayList<Tag>();
|
||||||
|
while (matcher.find()) {
|
||||||
|
Tag tag = tagRepository.findByName(matcher.group().replace('#', ' ').trim());
|
||||||
|
// TODO create tag if doesn't exist
|
||||||
|
tags.add(tag) ;
|
||||||
|
}
|
||||||
|
burp.setTags(tags);
|
||||||
|
|
||||||
|
burp.setTimestamp(new Date());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.bayer.burp.repository;
|
package com.bayer.burp.repository;
|
||||||
|
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
import org.springframework.data.rest.core.annotation.RestResource;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
|
||||||
import com.bayer.burp.domain.Tag;
|
import com.bayer.burp.domain.Tag;
|
||||||
|
|
||||||
@PreAuthorize("hasRole('ROLE_USER')")
|
@PreAuthorize("hasRole('ROLE_USER')")
|
||||||
public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
|
public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
|
||||||
|
|
||||||
|
@RestResource(exported = false)
|
||||||
|
Tag findByName(String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bayer.burp.repository;
|
package com.bayer.burp.repository;
|
||||||
|
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
import org.springframework.data.rest.core.annotation.RestResource;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
|
||||||
import com.bayer.burp.domain.User;
|
import com.bayer.burp.domain.User;
|
||||||
@@ -16,4 +17,7 @@ public interface UserRepository extends PagingAndSortingRepository<User, Long> {
|
|||||||
@Override
|
@Override
|
||||||
void delete(User user);
|
void delete(User user);
|
||||||
|
|
||||||
|
@RestResource(exported = false)
|
||||||
|
User findByName(String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
var burpControllers = angular.module('burpControllers', ['ngResource']);
|
var burpControllers = angular.module('burpControllers', ['ngResource']);
|
||||||
|
|
||||||
burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', function($scope, $resource) {
|
burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', '$http', function($scope, $resource, $http) {
|
||||||
|
|
||||||
|
$scope.initList = function() {
|
||||||
var burps = $resource('/burps/search/findFirst8ByOrderByTimestampDesc').get();
|
var burps = $resource('/burps/search/findFirst8ByOrderByTimestampDesc').get();
|
||||||
|
|
||||||
burps.$promise.then(function(data) {
|
burps.$promise.then(function(data) {
|
||||||
@@ -12,6 +13,27 @@ burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', function($sc
|
|||||||
$scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get();
|
$scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$scope.doBurp = function() {
|
||||||
|
|
||||||
|
// simple HTTP POST
|
||||||
|
$http.post('/burps', { "text" : $scope.text }).success(function() {
|
||||||
|
console.log('REFRESH');
|
||||||
|
$scope.initList();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Resource (might be moved into a service)
|
||||||
|
// var Burp = $resource('/burps');
|
||||||
|
//
|
||||||
|
// var newBurp = new Burp({
|
||||||
|
// "text" : $scope.text
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// newBurp.$save();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
} ]);
|
} ]);
|
||||||
|
|
||||||
burpControllers.controller('TagCtrl', [ '$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) {
|
burpControllers.controller('TagCtrl', [ '$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) {
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
<div>
|
<div ng-init="initList()">
|
||||||
<div>
|
<div>
|
||||||
<div>
|
<div>
|
||||||
|
<form role="form" ng-submit="doBurp()">
|
||||||
|
<div>
|
||||||
|
<input type="text" ng-model="text" placeholder="Your thoughts on today's menu" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="submit" value="Send" />
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
<div class="burp" ng-repeat="burp in burps">
|
<div class="burp" ng-repeat="burp in burps">
|
||||||
<div class="user">{{burp.user.name}}</div>
|
<div class="user">{{burp.user.name}}</div>
|
||||||
<div class="date">{{burp.timestamp | date:'HH:mm:ss dd.MM.yyyy'}}</div>
|
<div class="date">{{burp.timestamp | date:'HH:mm:ss dd.MM.yyyy'}}</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user