Quellcode durchsuchen

added query to find games by user

Rachelle vor 6 Jahren
Ursprung
Commit
045c9ae99d

+ 6
- 1
MyPassionProject/src/main/java/com/example/demo/Controller/GameController.java Datei anzeigen

@@ -2,6 +2,7 @@ package com.example.demo.Controller;
2 2
 
3 3
 import com.example.demo.Entity.Game;
4 4
 import com.example.demo.Service.GameService;
5
+import com.example.demo.Service.UserService;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
6 7
 import org.springframework.http.HttpStatus;
7 8
 import org.springframework.http.ResponseEntity;
@@ -22,7 +23,6 @@ public class GameController {
22 23
     @PostMapping(value="/games")
23 24
     public ResponseEntity<Game> addGame(@RequestBody Game game) {
24 25
         return gameService.addGame(game);
25
-
26 26
     }
27 27
 
28 28
     @GetMapping(value="/games")
@@ -33,4 +33,9 @@ public class GameController {
33 33
 
34 34
     @GetMapping(value="/games/name/{gameName}")
35 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 Datei anzeigen

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

+ 6
- 0
MyPassionProject/src/main/java/com/example/demo/Repository/GameRepo.java Datei anzeigen

@@ -7,9 +7,15 @@ import org.springframework.data.repository.query.Param;
7 7
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
8 8
 import org.springframework.http.ResponseEntity;
9 9
 
10
+import java.util.Collection;
11
+import java.util.Set;
12
+
10 13
 @RepositoryRestResource
11 14
 public interface GameRepo extends JpaRepository<Game,Long> {
12 15
 
13 16
     @Query("select id from Game where name = :name")
14 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 Datei anzeigen

@@ -12,7 +12,4 @@ import java.util.Set;
12 12
 
13 13
 @RepositoryRestResource
14 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 Datei anzeigen

@@ -9,14 +9,13 @@ import org.springframework.stereotype.Service;
9 9
 import org.springframework.web.bind.annotation.CrossOrigin;
10 10
 
11 11
 import javax.validation.ConstraintViolationException;
12
-import java.util.Collection;
12
+import java.util.*;
13 13
 
14 14
 @Service
15 15
 public class GameService {
16 16
     private GameRepo gameRepo;
17 17
 
18 18
     @Autowired
19
-
20 19
     public GameService(GameRepo gameRepo) {
21 20
         this.gameRepo = gameRepo;
22 21
     }
@@ -36,4 +35,14 @@ public class GameService {
36 35
     public Collection<Game> getAllGames() {
37 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 Datei anzeigen

@@ -54,4 +54,5 @@ public class UserService {
54 54
     user.addGames(game);
55 55
     return new ResponseEntity<>(userRepo.save(user), HttpStatus.OK);
56 56
   }
57
+
57 58
 }