Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/main/java/com/appdev/allin/gameData/GameDataController.java
Original file line number Diff line number Diff line change
@@ -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<Page<GameData>> 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<GameData> paginated = gameDataService.getAllGameData(pageable);
return ResponseEntity.ok(paginated);
}

@GetMapping("/{gameData_id}/")
public ResponseEntity<GameData> getGameDataById(@PathVariable final Integer id) {
GameData gameData = gameDataService.getGameDataById(id);
return ResponseEntity.ok(gameData);
}

@PostMapping("/")
public ResponseEntity<GameData> createGameData(@RequestBody final GameData gameData) {
GameData created = gameDataService.createGameData(gameData);
return ResponseEntity.status(201).body(created);
}

@PatchMapping("/{gameData_id}/")
public ResponseEntity<GameData> updateGameData(
@PathVariable final Integer id, @RequestBody final GameData updatedData) {
GameData updated = gameDataService.updateGameData(id, updatedData);
return ResponseEntity.ok(updated);
}

@DeleteMapping("/{gameData_id}/")
public ResponseEntity<Void> deleteGameData(@PathVariable final Integer id) {
gameDataService.deleteGameData(id);
return ResponseEntity.noContent().build();
}

@GetMapping("/gameData/find/")
public ResponseEntity<GameData> findByOpposingTeamAndGameDateTime(
@RequestParam String opposingTeam, @RequestParam String gameDateTime) {
GameData gameData = gameDataService.findByOpposingTeamAndGameDateTime(opposingTeam, gameDateTime);
return ResponseEntity.ok(gameData);
}

@PostMapping("/gameData/save/")
public ResponseEntity<GameData> saveGameData(@RequestBody GameData gameData) {
GameData savedGameData = gameDataService.saveGameData(gameData);
return ResponseEntity.status(201).body(savedGameData);
}
}
37 changes: 35 additions & 2 deletions src/main/java/com/appdev/allin/gameData/GameDataService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
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<GameData> 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);
}

public GameData saveGameData(GameData gameData) {
return gameDataRepo.save(gameData);
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/appdev/allin/user/UserRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, String> {
User findByUsername(String username);
Optional<User> findByUsername(String username);

User findByEmail(String email);
Optional<User> findByEmail(String email);

Integer countByBalanceGreaterThan(Integer balance);
}
15 changes: 6 additions & 9 deletions src/main/java/com/appdev/allin/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading