Browse Source

added query to find games by user

Rachelle 6 years ago
parent
commit
045c9ae99d

+ 6
- 1
MyPassionProject/src/main/java/com/example/demo/Controller/GameController.java View File

2
 
2
 
3
 import com.example.demo.Entity.Game;
3
 import com.example.demo.Entity.Game;
4
 import com.example.demo.Service.GameService;
4
 import com.example.demo.Service.GameService;
5
+import com.example.demo.Service.UserService;
5
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.HttpStatus;
7
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
22
     @PostMapping(value="/games")
23
     @PostMapping(value="/games")
23
     public ResponseEntity<Game> addGame(@RequestBody Game game) {
24
     public ResponseEntity<Game> addGame(@RequestBody Game game) {
24
         return gameService.addGame(game);
25
         return gameService.addGame(game);
25
-
26
     }
26
     }
27
 
27
 
28
     @GetMapping(value="/games")
28
     @GetMapping(value="/games")
33
 
33
 
34
     @GetMapping(value="/games/name/{gameName}")
34
     @GetMapping(value="/games/name/{gameName}")
35
     public Long getGameByName(@PathVariable String gameName){return gameService.getGameByName(gameName);}
35
     public Long getGameByName(@PathVariable String gameName){return gameService.getGameByName(gameName);}
36
+
37
+    @GetMapping(value="/games/{userId}")
38
+    public Collection<Game> getGamesByUserId(@PathVariable Long userId){
39
+        return gameService.getGamesByUserId(userId);
40
+    }
36
 }
41
 }

+ 1
- 1
MyPassionProject/src/main/java/com/example/demo/Entity/User.java View File

19
     @Column(unique = true)
19
     @Column(unique = true)
20
     private String userName;
20
     private String userName;
21
 
21
 
22
-    @ManyToMany(fetch = FetchType.LAZY,
22
+    @ManyToMany(
23
             cascade = {
23
             cascade = {
24
                     CascadeType.PERSIST,
24
                     CascadeType.PERSIST,
25
                     CascadeType.MERGE
25
                     CascadeType.MERGE

+ 6
- 0
MyPassionProject/src/main/java/com/example/demo/Repository/GameRepo.java View File

7
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
7
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
8
 import org.springframework.http.ResponseEntity;
8
 import org.springframework.http.ResponseEntity;
9
 
9
 
10
+import java.util.Collection;
11
+import java.util.Set;
12
+
10
 @RepositoryRestResource
13
 @RepositoryRestResource
11
 public interface GameRepo extends JpaRepository<Game,Long> {
14
 public interface GameRepo extends JpaRepository<Game,Long> {
12
 
15
 
13
     @Query("select id from Game where name = :name")
16
     @Query("select id from Game where name = :name")
14
     Long findByName(@Param("name")String name);
17
     Long findByName(@Param("name")String name);
18
+
19
+    @Query("select g.id from User u INNER JOIN u.gameLibrary g where u.id in :userId")
20
+    Collection<Long> findUserGames(@Param("userId")Long userId);
15
 }
21
 }

+ 0
- 3
MyPassionProject/src/main/java/com/example/demo/Repository/UserRepo.java View File

12
 
12
 
13
 @RepositoryRestResource
13
 @RepositoryRestResource
14
 public interface UserRepo extends JpaRepository<User, Long> {
14
 public interface UserRepo extends JpaRepository<User, Long> {
15
-//    @Query("select id from user_game where user_id = :userId")
16
-//    Set<Long> findUserGames(@Param("userId")Long userId);
17
-
18
 }
15
 }

+ 11
- 2
MyPassionProject/src/main/java/com/example/demo/Service/GameService.java View File

9
 import org.springframework.web.bind.annotation.CrossOrigin;
9
 import org.springframework.web.bind.annotation.CrossOrigin;
10
 
10
 
11
 import javax.validation.ConstraintViolationException;
11
 import javax.validation.ConstraintViolationException;
12
-import java.util.Collection;
12
+import java.util.*;
13
 
13
 
14
 @Service
14
 @Service
15
 public class GameService {
15
 public class GameService {
16
     private GameRepo gameRepo;
16
     private GameRepo gameRepo;
17
 
17
 
18
     @Autowired
18
     @Autowired
19
-
20
     public GameService(GameRepo gameRepo) {
19
     public GameService(GameRepo gameRepo) {
21
         this.gameRepo = gameRepo;
20
         this.gameRepo = gameRepo;
22
     }
21
     }
36
     public Collection<Game> getAllGames() {
35
     public Collection<Game> getAllGames() {
37
         return (gameRepo.findAll());
36
         return (gameRepo.findAll());
38
     }
37
     }
38
+
39
+    public Set<Game> getGamesByUserId(Long userId) {
40
+        Collection<Long> gameIds = gameRepo.findUserGames(userId);
41
+        Set<Game> games = new HashSet<>();
42
+        for (Long id : gameIds) {
43
+            games.add(getGameById(id).getBody());
44
+        }
45
+        return games;
46
+    }
39
 }
47
 }
48
+

+ 1
- 0
MyPassionProject/src/main/java/com/example/demo/Service/UserService.java View File

54
     user.addGames(game);
54
     user.addGames(game);
55
     return new ResponseEntity<>(userRepo.save(user), HttpStatus.OK);
55
     return new ResponseEntity<>(userRepo.save(user), HttpStatus.OK);
56
   }
56
   }
57
+
57
 }
58
 }