Bladeren bron

added games classes, entity, repo, service, controller

Rachelle 6 jaren geleden
bovenliggende
commit
443df5d475

+ 23
- 0
MyPassionProject/src/main/java/com/example/demo/Controller/GameController.java Bestand weergeven

@@ -1,4 +1,27 @@
1 1
 package com.example.demo.Controller;
2 2
 
3
+import com.example.demo.Entity.Game;
4
+import com.example.demo.Service.GameService;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.ResponseEntity;
7
+import org.springframework.web.bind.annotation.*;
8
+
9
+@RestController
3 10
 public class GameController {
11
+
12
+    private GameService gameService;
13
+
14
+    @Autowired
15
+    public GameController(GameService gameService) {
16
+        this.gameService = gameService;
17
+    }
18
+
19
+    @PostMapping(value="/games")
20
+    public ResponseEntity<Game> addGame(@RequestBody Game game){return gameService.addGame(game);}
21
+
22
+    @GetMapping(value="/games/id/{gameId}")
23
+    public ResponseEntity<Game> getGameById(@PathVariable Long gameId){return gameService.getGameById(gameId);}
24
+
25
+    @GetMapping(value="/games/name/{gameName}")
26
+    public Long getGameByName(@PathVariable String gameName){return gameService.getGameByName(gameName);}
4 27
 }

+ 4
- 4
MyPassionProject/src/main/java/com/example/demo/Controller/UserController.java Bestand weergeven

@@ -27,13 +27,13 @@ public class UserController {
27 27
   }
28 28
 
29 29
   @PutMapping(value = "/users/{userId}/summary")
30
-  public ResponseEntity<User> updateSummary(@RequestBody User user, @PathVariable Long userId){
31
-    return userService.updateUserSummary(user, userId);
30
+  public ResponseEntity<User> updateSummary(@RequestBody String newSummary, @PathVariable Long userId){
31
+    return userService.updateUserSummary(newSummary, userId);
32 32
   }
33 33
 
34 34
   @PutMapping(value="/users/{userId}/profileName")
35
-  public ResponseEntity<User> updateProfileName(@RequestBody User user, @PathVariable Long userId){
36
-      return userService.updateUserProfileName(user,userId);
35
+  public ResponseEntity<User> updateProfileName(@RequestBody String newName, @PathVariable Long userId){
36
+      return userService.updateUserProfileName(newName,userId);
37 37
   }
38 38
 
39 39
   @DeleteMapping(value="/users/{userId}")

+ 6
- 0
MyPassionProject/src/main/java/com/example/demo/Repository/GameRepo.java Bestand weergeven

@@ -2,8 +2,14 @@ package com.example.demo.Repository;
2 2
 
3 3
 import com.example.demo.Entity.Game;
4 4
 import org.springframework.data.jpa.repository.JpaRepository;
5
+import org.springframework.data.jpa.repository.Query;
6
+import org.springframework.data.repository.query.Param;
5 7
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
8
+import org.springframework.http.ResponseEntity;
6 9
 
7 10
 @RepositoryRestResource
8 11
 public interface GameRepo extends JpaRepository<Game,Long> {
12
+
13
+    @Query("select id from Game where name = :name")
14
+    Long findByName(@Param("name")String name);
9 15
 }

+ 27
- 0
MyPassionProject/src/main/java/com/example/demo/Service/GameService.java Bestand weergeven

@@ -1,4 +1,31 @@
1 1
 package com.example.demo.Service;
2 2
 
3
+import com.example.demo.Entity.Game;
4
+import com.example.demo.Repository.GameRepo;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.http.HttpStatus;
7
+import org.springframework.http.ResponseEntity;
8
+import org.springframework.stereotype.Service;
9
+
10
+@Service
3 11
 public class GameService {
12
+    private GameRepo gameRepo;
13
+
14
+    @Autowired
15
+
16
+    public GameService(GameRepo gameRepo) {
17
+        this.gameRepo = gameRepo;
18
+    }
19
+
20
+    public ResponseEntity<Game> addGame(Game game){
21
+        return new ResponseEntity<>(gameRepo.save(game),HttpStatus.CREATED);
22
+    }
23
+
24
+    public ResponseEntity<Game> getGameById(Long id){
25
+        return new ResponseEntity<>(gameRepo.findById(id).get(), HttpStatus.OK);
26
+    }
27
+
28
+    public Long getGameByName(String name){
29
+        return gameRepo.findByName(name);
30
+    }
4 31
 }

+ 4
- 4
MyPassionProject/src/main/java/com/example/demo/Service/UserService.java Bestand weergeven

@@ -25,15 +25,15 @@ public class UserService {
25 25
     return new ResponseEntity<>(userRepo.findById(id).get(), HttpStatus.CREATED);
26 26
   }
27 27
 
28
-  public ResponseEntity<User> updateUserSummary (User updatedUser, Long userId){
28
+  public ResponseEntity<User> updateUserSummary (String newSummary, Long userId){
29 29
     User userToUpdate = userRepo.getOne(userId);
30
-    userToUpdate.setSummary(updatedUser.getSummary());
30
+    userToUpdate.setSummary(newSummary);
31 31
     return new ResponseEntity<>(userRepo.save(userToUpdate), HttpStatus.OK);
32 32
   }
33 33
 
34
-  public ResponseEntity<User> updateUserProfileName (User updatedUser, Long userId){
34
+  public ResponseEntity<User> updateUserProfileName (String newName, Long userId){
35 35
     User userToUpdate = userRepo.getOne(userId);
36
-    userToUpdate.setProfileName(updatedUser.getProfileName());
36
+    userToUpdate.setProfileName(newName);
37 37
     return new ResponseEntity<>(userRepo.save(userToUpdate), HttpStatus.OK);
38 38
   }
39 39