Skip to main content

Endpoint

Changes the name of an existing player. The new name must be valid and follow validation constraints.

Path Parameters

playerId
string
required
The unique identifier (UUID) of the player whose name you want to change

Request Body

newName
string
required
The new name for the player. Must be between 1-30 characters (after trimming whitespace) and not blank.

Validation Rules

The player name must satisfy the following constraints:
The name cannot be empty or consist only of whitespace. Leading and trailing whitespace is automatically trimmed.
PlayerName.java
if (v.isEmpty()) throw new IllegalArgumentException("PlayerName cannot be blank");
After trimming whitespace, the name must not exceed 30 characters.
PlayerName.java
if (v.length() > 30) throw new IllegalArgumentException("PlayerName too long (max 30)");
Leading and trailing whitespace is automatically removed from the name.
PlayerName.java
String v = value.trim();

Response

playerId
string
The unique identifier of the player
name
string
The player’s new name (after trimming)
wins
int
Number of games won by the player
losses
int
Number of games lost by the player

Examples

Successful Name Change

curl -X PUT http://localhost:8080/player/7c9e6679-7425-40de-944b-e07fc1f90ae7 \
  -H "Content-Type: application/json" \
  -d '{"newName": "Alice Supreme"}'

Trimming Whitespace

curl -X PUT http://localhost:8080/player/7c9e6679-7425-40de-944b-e07fc1f90ae7 \
  -H "Content-Type: application/json" \
  -d '{"newName": "  Bob  "}'

Error Responses

400 Bad Request - Blank Name

curl -X PUT http://localhost:8080/player/7c9e6679-7425-40de-944b-e07fc1f90ae7 \
  -H "Content-Type: application/json" \
  -d '{"newName": "   "}'

400 Bad Request - Name Too Long

curl -X PUT http://localhost:8080/player/7c9e6679-7425-40de-944b-e07fc1f90ae7 \
  -H "Content-Type: application/json" \
  -d '{"newName": "This name is definitely way too long for a player"}'

404 Not Found - Player Doesn’t Exist

curl -X PUT http://localhost:8080/player/00000000-0000-0000-0000-000000000000 \
  -H "Content-Type: application/json" \
  -d '{"newName": "Alice"}'

Implementation Details

The endpoint follows a clean architecture pattern with domain-driven design:
1

Validate Request

The request is validated at the controller level using Jakarta Bean Validation annotations:
ChangePlayerNameRequest.java
public record ChangePlayerNameRequest(
    @NotBlank(message = "Player name must not be blank") 
    @Size(max = 30, message = "Player name length must be <= 30") 
    String newName
) {}
2

Create Domain Object

The use case creates a PlayerName value object, which performs domain-level validation:
ChangePlayerNameUseCase.java
try {
    newName = new PlayerName(cmd.newName());
} catch (IllegalArgumentException e) {
    return Mono.error(new InvalidPlayerNameException(e.getMessage()));
}
3

Retrieve Player

The player is retrieved from the repository:
return playerRepo.findById(pId)
    .switchIfEmpty(Mono.error(new PlayerNotFoundException(cmd.playerId())))
4

Apply Rename

The immutable rename() method returns a new player instance:
.map(player -> player.rename(newName))
5

Persist and Return

The updated player is saved and returned as a DTO:
.flatMap(playerRepo::save)
.map(this::toResult);
The Player aggregate is immutable, so the rename() method returns a new Player instance rather than modifying the existing one.

Player Overview

Learn about the Player aggregate

View Rankings

See all players ranked by score

Build docs developers (and LLMs) love