Search in sources :

Example 1 with IncorrectIdException

use of exceptions.IncorrectIdException in project server by sopra-fs22-group-01.

the class UserController method updateReadyStatus.

// Maps data from ready-status changes
// used to flip the ready-status
@PutMapping("/lobbies/{lobbyId}/users/{userId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
@ResponseBody
public void updateReadyStatus(@PathVariable long lobbyId, @PathVariable long userId) {
    String baseErrorMessage1 = "No lobby with this id could be found.";
    try {
        // throws exception if userid doesnt exist
        User user = userService.findUserById(userId);
        // doesn't update the user in the array of the lobby, only the user in the database
        userService.updateUserReadyStatus(user);
        // updates the user in the array of the lobby, not the perfect solution
        gameService.updateUserReadyStatus(lobbyId, userId);
    } catch (IncorrectIdException e1) {
        throw new ResponseStatusException(HttpStatus.CONFLICT, baseErrorMessage1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : User(ch.uzh.ifi.hase.soprafs22.entity.User) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException)

Example 2 with IncorrectIdException

use of exceptions.IncorrectIdException in project server by sopra-fs22-group-01.

the class UserController method addUserToLobby.

// ----------------------------MOVED FROM GAMECONTROLLER TO USERCONTROLLER-------------------------------------
// Adds a user to the list of all current players in the lobby
@PostMapping("/lobbies/{lobbyId}/lists/players/{userId}")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResponseEntity<ArrayList<User>> addUserToLobby(@PathVariable long lobbyId, @PathVariable long userId) {
    String baseErrorMessage1 = "No lobby with this id could be found.";
    String baseErrorMessage2 = "The same user is already existing in the lobby ";
    // gets user from correct user repository
    User user = userService.findUserById(userId);
    // User userInput = DTOMapper.INSTANCE.convertUserPostDTOtoEntity(userPostDTO);
    try {
        gameService.addPlayerToLobby(lobbyId, user);
        ArrayList<User> allUsers = gameManager.getLobby(lobbyId).getCurrentPlayers();
        return ResponseEntity.ok(allUsers);
    } catch (IncorrectIdException e1) {
        throw new ResponseStatusException(HttpStatus.CONFLICT, baseErrorMessage1);
    } catch (Exception e2) {
        throw new ResponseStatusException(HttpStatus.CONFLICT, baseErrorMessage2);
    }
}
Also used : User(ch.uzh.ifi.hase.soprafs22.entity.User) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException)

Example 3 with IncorrectIdException

use of exceptions.IncorrectIdException in project server by sopra-fs22-group-01.

the class GameController method deleteUserFromLobby.

// Removes a user from the list of all current players in the lobby
@DeleteMapping("/lobbies/{lobbyId}/players")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public void deleteUserFromLobby(@PathVariable long lobbyId, @RequestBody UserPostDTO userPostDTO) {
    String baseErrorMessage1 = "No lobby with this id could be found.";
    String baseErrorMessage2 = "No such user exists in the lobby";
    User userInput = DTOMapper.INSTANCE.convertUserPostDTOtoEntity(userPostDTO);
    try {
        gameService.removePlayerFromLobby(lobbyId, userInput);
    } catch (IncorrectIdException e1) {
        throw new ResponseStatusException(HttpStatus.CONFLICT, baseErrorMessage1);
    } catch (Exception e2) {
        throw new ResponseStatusException(HttpStatus.NOT_FOUND, baseErrorMessage2);
    }
}
Also used : User(ch.uzh.ifi.hase.soprafs22.entity.User) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with IncorrectIdException

use of exceptions.IncorrectIdException in project server by sopra-fs22-group-01.

the class GameController method checkIfMatchCanGetStarted.

// Retrieves if all current players in the lobby are ready or not and if the minimum number of player was reached
@GetMapping("/lobbies/{lobbyId}/status")
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public ResponseEntity<LobbyStatus> checkIfMatchCanGetStarted(@PathVariable long lobbyId) {
    String baseErrorMessage1 = "No lobby with this id could be found.";
    try {
        gameService.checkIfLobbyStatusChanged(lobbyId);
        LobbyStatus lobbyStatus = gameService.getLobbyStatus(lobbyId);
        return ResponseEntity.ok(lobbyStatus);
    } catch (IncorrectIdException e1) {
        throw new ResponseStatusException(HttpStatus.CONFLICT, baseErrorMessage1);
    }
}
Also used : IncorrectIdException(ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) LobbyStatus(ch.uzh.ifi.hase.soprafs22.game.helpers.LobbyStatus) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 5 with IncorrectIdException

use of exceptions.IncorrectIdException in project Lab5 by Arslanka.

the class RemoveByIdCommand method execute.

@Override
public boolean execute(Object... args) {
    try {
        collection.removeById((Integer) args[0]);
        printer.println("An element whose id field value is equivalent to the specified " + args[0] + " has been successfully removed from the collection", HELP);
        printer.println(SEPARATOR, ERROR);
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new ExecutionException("You have not entered an id to remove from the collection.");
    } catch (IncorrectIdException e) {
        throw new ExecutionException("Deleting an element by id error.\n" + e.getMessage());
    }
    return true;
}
Also used : IncorrectIdException(exceptions.IncorrectIdException) ExecutionException(exceptions.ExecutionException)

Aggregations

IncorrectIdException (ch.uzh.ifi.hase.soprafs22.exceptions.IncorrectIdException)4 ResponseStatusException (org.springframework.web.server.ResponseStatusException)4 User (ch.uzh.ifi.hase.soprafs22.entity.User)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 LobbyStatus (ch.uzh.ifi.hase.soprafs22.game.helpers.LobbyStatus)1 ExecutionException (exceptions.ExecutionException)1 IncorrectIdException (exceptions.IncorrectIdException)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1