diff --git a/src/main/java/com/appdev/allin/gameData/GameDataController.java b/src/main/java/com/appdev/allin/gameData/GameDataController.java new file mode 100644 index 0000000..022f140 --- /dev/null +++ b/src/main/java/com/appdev/allin/gameData/GameDataController.java @@ -0,0 +1,68 @@ +package com.appdev.allin.gameData; + +import com.appdev.allin.utils.Pagination; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/gamedata") +public class GameDataController { + + private final GameDataService gameDataService; + + public GameDataController(GameDataService gameDataService) { + this.gameDataService = gameDataService; + } + + @GetMapping("/") + public ResponseEntity> getAllGameData( + @RequestParam(defaultValue = "0") Integer page, + @RequestParam(defaultValue = "10") Integer size, + @RequestParam(defaultValue = "gameDateTime") String sortBy, + @RequestParam(defaultValue = "desc") String direction) { + + Pageable pageable = Pagination.generatePageable(page, size, sortBy, direction); + Page paginated = gameDataService.getAllGameData(pageable); + return ResponseEntity.ok(paginated); + } + + @GetMapping("/{gameData_id}/") + public ResponseEntity getGameDataById(@PathVariable final Integer id) { + GameData gameData = gameDataService.getGameDataById(id); + return ResponseEntity.ok(gameData); + } + + @PostMapping("/") + public ResponseEntity createGameData(@RequestBody final GameData gameData) { + GameData created = gameDataService.createGameData(gameData); + return ResponseEntity.status(201).body(created); + } + + @PatchMapping("/{gameData_id}/") + public ResponseEntity updateGameData( + @PathVariable final Integer id, @RequestBody final GameData updatedData) { + GameData updated = gameDataService.updateGameData(id, updatedData); + return ResponseEntity.ok(updated); + } + + @DeleteMapping("/{gameData_id}/") + public ResponseEntity deleteGameData(@PathVariable final Integer id) { + gameDataService.deleteGameData(id); + return ResponseEntity.noContent().build(); + } + + @GetMapping("/gameData/find/") + public ResponseEntity findByOpposingTeamAndGameDateTime( + @RequestParam String opposingTeam, @RequestParam String gameDateTime) { + GameData gameData = gameDataService.findByOpposingTeamAndGameDateTime(opposingTeam, gameDateTime); + return ResponseEntity.ok(gameData); + } + + @PostMapping("/gameData/save/") + public ResponseEntity saveGameData(@RequestBody GameData gameData) { + GameData savedGameData = gameDataService.saveGameData(gameData); + return ResponseEntity.status(201).body(savedGameData); + } +} \ No newline at end of file diff --git a/src/main/java/com/appdev/allin/gameData/GameDataService.java b/src/main/java/com/appdev/allin/gameData/GameDataService.java index 7c9a02d..4caf763 100644 --- a/src/main/java/com/appdev/allin/gameData/GameDataService.java +++ b/src/main/java/com/appdev/allin/gameData/GameDataService.java @@ -1,14 +1,47 @@ package com.appdev.allin.gameData; + +import com.appdev.allin.exceptions.NotFoundException; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service public class GameDataService { - private final GameDataRepo gameDataRepo; + private final GameDataRepo gameDataRepo; public GameDataService(GameDataRepo gameDataRepo) { this.gameDataRepo = gameDataRepo; } + public Page getAllGameData(final Pageable pageable) { + return gameDataRepo.findAll(pageable); + } + + public GameData getGameDataById(final Integer id) { + return gameDataRepo.findById(id) + .orElseThrow(() -> new NotFoundException("GameData with id " + id + " not found.")); + } + + public GameData createGameData(final GameData data) { + return gameDataRepo.save(data); + } + + public GameData updateGameData(final Integer id, final GameData newData) { + GameData existing = gameDataRepo.findById(id) + .orElseThrow(() -> new NotFoundException("GameData with id " + id + " not found.")); + existing.setOpposingTeam(newData.getOpposingTeam()); + existing.setGameDateTime(newData.getGameDateTime()); + existing.setFullLocation(newData.getFullLocation()); + existing.setLogoUrl(newData.getLogoUrl()); + return gameDataRepo.save(existing); + } + + public void deleteGameData(final Integer id) { + GameData existing = gameDataRepo.findById(id) + .orElseThrow(() -> new NotFoundException("GameData with id " + id + " not found.")); + gameDataRepo.delete(existing); + } + public GameData findByOpposingTeamAndGameDateTime(String opposingTeam, String gameDateTime) { return gameDataRepo.findByOpposingTeamAndGameDateTime(opposingTeam, gameDateTime); } @@ -16,4 +49,4 @@ public GameData findByOpposingTeamAndGameDateTime(String opposingTeam, String ga public GameData saveGameData(GameData gameData) { return gameDataRepo.save(gameData); } -} +} \ No newline at end of file diff --git a/src/main/java/com/appdev/allin/user/UserRepo.java b/src/main/java/com/appdev/allin/user/UserRepo.java index ac209fd..229c3b8 100644 --- a/src/main/java/com/appdev/allin/user/UserRepo.java +++ b/src/main/java/com/appdev/allin/user/UserRepo.java @@ -2,12 +2,13 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; @Repository public interface UserRepo extends JpaRepository { - User findByUsername(String username); + Optional findByUsername(String username); - User findByEmail(String email); + Optional findByEmail(String email); Integer countByBalanceGreaterThan(Integer balance); } diff --git a/src/main/java/com/appdev/allin/user/UserService.java b/src/main/java/com/appdev/allin/user/UserService.java index c82eb34..c0d6602 100644 --- a/src/main/java/com/appdev/allin/user/UserService.java +++ b/src/main/java/com/appdev/allin/user/UserService.java @@ -30,15 +30,12 @@ public Integer getUserRank(final User user) { } public User createUser(final User user) { - User existingUsernameUser = userRepo.findByUsername(user.getUsername()); - if (existingUsernameUser != null) { - throw new ForbiddenException("Username " + user.getUsername() + " already exists."); - } - - User existingEmailUser = userRepo.findByEmail(user.getEmail()); - if (existingEmailUser != null) { - throw new ForbiddenException("Email " + user.getEmail() + " already exists."); - } + userRepo.findByUsername(user.getUsername()) + .orElseThrow(() -> new ForbiddenException("Username " + user.getUsername() + " already exists.")); + + userRepo.findByEmail(user.getEmail()) + .orElseThrow(() -> new ForbiddenException("Email " + user.getEmail() + " already exists.")); + return userRepo.save(user); }