File tree Expand file tree Collapse file tree
src/main/java/com/okta/developer/jugtours Expand file tree Collapse file tree Original file line number Diff line number Diff line change 6464 <artifactId >spring-boot-starter-test</artifactId >
6565 <scope >test</scope >
6666 </dependency >
67+ <dependency >
68+ <groupId >org.springframework.data</groupId >
69+ <artifactId >spring-data-rest-core</artifactId >
70+ <version >3.0.8.RELEASE</version >
71+ </dependency >
6772 </dependencies >
6873
6974 <build >
Original file line number Diff line number Diff line change 11package com .okta .developer .jugtours .model ;
22
3- import lombok .*;
3+ import lombok .Data ;
4+ import lombok .NoArgsConstructor ;
5+ import lombok .NonNull ;
6+ import lombok .RequiredArgsConstructor ;
47
58import javax .persistence .*;
6- import javax .validation .constraints .NotBlank ;
7- import javax .validation .constraints .NotNull ;
89import java .util .Set ;
910
1011@ Data
@@ -24,6 +25,8 @@ public class Group {
2425 private String stateOrProvince ;
2526 private String country ;
2627 private String postalCode ;
28+ @ ManyToOne
29+ private User user ;
2730
2831 @ OneToMany (fetch = FetchType .EAGER , cascade =CascadeType .ALL )
2932 private Set <Event > events ;
Original file line number Diff line number Diff line change 1+ package com .okta .developer .jugtours .model ;
2+
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+ import org .springframework .data .rest .core .annotation .HandleBeforeSave ;
6+ import org .springframework .data .rest .core .annotation .RepositoryEventHandler ;
7+ import org .springframework .security .core .context .SecurityContextHolder ;
8+
9+ import java .util .Map ;
10+
11+ @ RepositoryEventHandler (Group .class )
12+ public class GroupEventHandler {
13+
14+ private final Logger log = LoggerFactory .getLogger (GroupEventHandler .class );
15+
16+ @ HandleBeforeSave
17+ @ SuppressWarnings ("unchecked" )
18+ public void handleBeforeSave (Group group ) {
19+ Map <String , Object > details = (Map <String , Object >) SecurityContextHolder .getContext ()
20+ .getAuthentication ().getDetails ();
21+ User user = new User (null , details .get ("name" ).toString (), details .get ("email" ).toString ());
22+ log .info ("Creating group: {} with user: {}" , group .getName ());
23+ group .setUser (user );
24+ }
25+ }
Original file line number Diff line number Diff line change 22
33import org .springframework .data .jpa .repository .JpaRepository ;
44
5+ import java .util .List ;
6+
57public interface GroupRepository extends JpaRepository <Group , Long > {
68 Group findByName (String name );
9+ List <Group > findAllByUserName (String name );
710}
Original file line number Diff line number Diff line change 1111import javax .validation .Valid ;
1212import java .net .URI ;
1313import java .net .URISyntaxException ;
14+ import java .security .Principal ;
1415import java .util .Collection ;
1516import java .util .Optional ;
1617
@@ -26,8 +27,8 @@ public GroupController(GroupRepository repository) {
2627 }
2728
2829 @ GetMapping ("/groups" )
29- Collection <Group > groups () {
30- return repository .findAll ( );
30+ Collection <Group > groups (Principal principal ) {
31+ return repository .findAllByUserName ( principal . getName () );
3132 }
3233
3334 @ GetMapping ("/group/{id}" )
You can’t perform that action at this time.
0 commit comments