From e0b12800f4ce2f2eff6fd8df96d19ef0a9ac245e Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sat, 6 Apr 2024 14:43:16 -0400 Subject: [PATCH] Define the player controller --- .../mc/controller/PlayerController.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main/java/me/braydon/mc/controller/PlayerController.java diff --git a/src/main/java/me/braydon/mc/controller/PlayerController.java b/src/main/java/me/braydon/mc/controller/PlayerController.java new file mode 100644 index 0000000..8a4ffb1 --- /dev/null +++ b/src/main/java/me/braydon/mc/controller/PlayerController.java @@ -0,0 +1,47 @@ +package me.braydon.mc.controller; + +import lombok.NonNull; +import lombok.extern.log4j.Log4j2; +import me.braydon.mc.exception.impl.BadRequestException; +import me.braydon.mc.exception.impl.ResourceNotFoundException; +import me.braydon.mc.model.Player; +import me.braydon.mc.service.MojangService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +/** + * The controller for handling + * player related requests. + * + * @author Braydon + */ +@RestController +@RequestMapping(value = "/player", produces = MediaType.APPLICATION_JSON_VALUE) +@Log4j2(topic = "Player Controller") +public final class PlayerController { + /** + * The Mojang service to use for player information. + */ + @NonNull private final MojangService mojangService; + + @Autowired + public PlayerController(@NonNull MojangService mojangService) { + this.mojangService = mojangService; + } + + /** + * A GET route to get a player by their username or UUID. + * + * @param query the query to search for the player by + * @return the player response + * @throws BadRequestException if the UUID is malformed + * @throws ResourceNotFoundException if the player is not found + */ + @GetMapping("/{query}") + @ResponseBody + public ResponseEntity getPlayer(@PathVariable @NonNull String query) throws BadRequestException, ResourceNotFoundException { + return ResponseEntity.ofNullable(mojangService.getPlayer(query)); + } +} \ No newline at end of file