Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
28 lines (23 sloc) 927 Bytes
package com.juanpabloprado.review;
import com.juanpabloprado.user.User;
import com.juanpabloprado.user.UserRepository;
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;
@Component
@RepositoryEventHandler(Review.class)
public class ReviewEventHandler {
private final UserRepository users;
@Autowired
public ReviewEventHandler(UserRepository users) {
this.users = users;
}
@HandleBeforeCreate
public void addReviewerBasedOnLoggedInUser(Review review) {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
User user = users.findByUsername(username);
review.setReviewer(user);
}
}
You can’t perform that action at this time.