33import com .okta .developer .jugtours .model .Group ;
44import com .okta .developer .jugtours .model .GroupRepository ;
55import com .okta .developer .jugtours .model .User ;
6+ import com .okta .developer .jugtours .model .UserRepository ;
67import org .slf4j .Logger ;
78import org .slf4j .LoggerFactory ;
89import org .springframework .http .HttpStatus ;
910import org .springframework .http .ResponseEntity ;
10- import org .springframework .security .core .context . SecurityContextHolder ;
11- import org .springframework .security .oauth2 .provider . OAuth2Authentication ;
11+ import org .springframework .security .core .annotation . AuthenticationPrincipal ;
12+ import org .springframework .security .oauth2 .core . user . OAuth2User ;
1213import org .springframework .web .bind .annotation .*;
1314
1415import javax .validation .Valid ;
2425class GroupController {
2526
2627 private final Logger log = LoggerFactory .getLogger (GroupController .class );
27- private GroupRepository repository ;
28+ private GroupRepository groupRepository ;
29+ private UserRepository userRepository ;
2830
29- public GroupController (GroupRepository repository ) {
30- this .repository = repository ;
31+ public GroupController (GroupRepository groupRepository , UserRepository userRepository ) {
32+ this .groupRepository = groupRepository ;
33+ this .userRepository = userRepository ;
3134 }
3235
3336 @ GetMapping ("/groups" )
3437 Collection <Group > groups (Principal principal ) {
35- return repository . findAllByUserName (principal .getName ());
38+ return groupRepository . findAllByUserId (principal .getName ());
3639 }
3740
3841 @ GetMapping ("/group/{id}" )
3942 ResponseEntity <?> getGroup (@ PathVariable Long id ) {
40- Optional <Group > group = repository .findById (id );
43+ Optional <Group > group = groupRepository .findById (id );
4144 return group .map (response -> ResponseEntity .ok ().body (response ))
4245 .orElse (new ResponseEntity <>(HttpStatus .NOT_FOUND ));
4346 }
4447
4548 @ PostMapping ("/group" )
46- ResponseEntity <Group > createGroup (@ Valid @ RequestBody Group group , Principal principal ) throws URISyntaxException {
49+ ResponseEntity <Group > createGroup (@ Valid @ RequestBody Group group ,
50+ @ AuthenticationPrincipal OAuth2User principal ) throws URISyntaxException {
4751 log .info ("Request to create group: {}" , group );
48- OAuth2Authentication authentication = (OAuth2Authentication ) principal ;
49- Map <String , Object > details = (Map <String , Object >) authentication .getUserAuthentication ().getDetails ();
50- User user = new User (details .get ("sub" ).toString (),
51- details .get ("name" ).toString (), details .get ("email" ).toString ());
52- group .setUser (user );
53- Group result = repository .save (group );
52+ Map <String , Object > details = principal .getAttributes ();
53+ String userId = details .get ("sub" ).toString ();
54+
55+ // check to see if user already exists
56+ Optional <User > user = userRepository .findById (userId );
57+ group .setUser (user .orElse (new User (userId ,
58+ details .get ("name" ).toString (), details .get ("email" ).toString ())));
59+
60+ Group result = groupRepository .save (group );
5461 return ResponseEntity .created (new URI ("/api/group/" + result .getId ()))
5562 .body (result );
5663 }
5764
5865 @ PutMapping ("/group" )
5966 ResponseEntity <Group > updateGroup (@ Valid @ RequestBody Group group ) {
6067 log .info ("Request to update group: {}" , group );
61- Group result = repository .save (group );
68+ Group result = groupRepository .save (group );
6269 return ResponseEntity .ok ().body (result );
6370 }
6471
6572 @ DeleteMapping ("/group/{id}" )
6673 public ResponseEntity <?> deleteGroup (@ PathVariable Long id ) {
6774 log .info ("Request to delete group: {}" , id );
68- repository .deleteById (id );
75+ groupRepository .deleteById (id );
6976 return ResponseEntity .ok ().build ();
7077 }
7178}
0 commit comments