TD-3: Add Player Registration Endpoint
All checks were successful
Quality Check / Validate OAS (push) Successful in 29s
Quality Check / Testing (push) Successful in 46s
Quality Check / Linting (push) Successful in 55s
Quality Check / Static Analysis (push) Successful in 56s
Quality Check / Validate OAS (pull_request) Successful in 30s
Quality Check / Testing (pull_request) Successful in 47s
Quality Check / Static Analysis (pull_request) Successful in 58s
Quality Check / Linting (pull_request) Successful in 1m10s
All checks were successful
Quality Check / Validate OAS (push) Successful in 29s
Quality Check / Testing (push) Successful in 46s
Quality Check / Linting (push) Successful in 55s
Quality Check / Static Analysis (push) Successful in 56s
Quality Check / Validate OAS (pull_request) Successful in 30s
Quality Check / Testing (pull_request) Successful in 47s
Quality Check / Static Analysis (pull_request) Successful in 58s
Quality Check / Linting (pull_request) Successful in 1m10s
This commit is contained in:
parent
89aa262830
commit
721ad39cb9
3 changed files with 70 additions and 0 deletions
40
api/api.yml
40
api/api.yml
|
@ -25,6 +25,20 @@ components:
|
|||
format: uuid
|
||||
example: f0981749-f550-46cd-b9ce-b6ca7cd0251f
|
||||
#############################################
|
||||
# PlayerRegistrationData #
|
||||
#############################################
|
||||
PlayerRegistrationData:
|
||||
description: Data needed to create a new player
|
||||
type: object
|
||||
properties:
|
||||
username:
|
||||
type: string
|
||||
password:
|
||||
type: string
|
||||
required:
|
||||
- username
|
||||
- password
|
||||
#############################################
|
||||
# AdminAuthInfo #
|
||||
#############################################
|
||||
ServerHealth:
|
||||
|
@ -45,6 +59,8 @@ components:
|
|||
required:
|
||||
- username
|
||||
responses:
|
||||
201PlayerCreated:
|
||||
description: "201 - Player Created"
|
||||
401Unauthorized:
|
||||
description: "401 - Unauthorized"
|
||||
404NotFound:
|
||||
|
@ -53,6 +69,12 @@ components:
|
|||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
409UsernameTaken:
|
||||
description: "409 - Username Taken"
|
||||
content:
|
||||
text/plain:
|
||||
schema:
|
||||
type: string
|
||||
409Conflict:
|
||||
description: "409 - Conflict"
|
||||
content:
|
||||
|
@ -72,6 +94,24 @@ components:
|
|||
schema:
|
||||
type: string
|
||||
paths:
|
||||
/player/register:
|
||||
post:
|
||||
operationId: "PlayerRegister"
|
||||
tags:
|
||||
- server
|
||||
description: "Endpoint for registering a new Player"
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: "#/components/schemas/PlayerRegistrationData"
|
||||
responses:
|
||||
201:
|
||||
$ref: "#/components/responses/201PlayerCreated"
|
||||
409:
|
||||
$ref: "#/components/responses/409UsernameTaken"
|
||||
500:
|
||||
$ref: "#/components/responses/500InternalError"
|
||||
/server/health:
|
||||
get:
|
||||
operationId: "ServerGetHealthcheck"
|
||||
|
|
|
@ -4,4 +4,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||
|
||||
public interface PlayerRepository extends JpaRepository<Player, Long> {
|
||||
Player findByUsername(String username);
|
||||
boolean existsByUsername(String username);
|
||||
}
|
||||
|
|
|
@ -2,17 +2,30 @@ package de.towerdefence.server.server;
|
|||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.towerdefence.server.oas.ServerApi;
|
||||
import de.towerdefence.server.oas.models.PlayerRegistrationData;
|
||||
import de.towerdefence.server.oas.models.ServerHealth;
|
||||
import de.towerdefence.server.player.Player;
|
||||
import de.towerdefence.server.player.PlayerRepository;
|
||||
import de.towerdefence.server.player.PlayerService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Optional;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("${openapi.api.base-path:/api/v1}")
|
||||
public class ServerApiController implements ServerApi {
|
||||
|
||||
@Autowired
|
||||
private PlayerRepository playerRepository;
|
||||
@Autowired
|
||||
private PlayerService playerService;
|
||||
|
||||
@Override
|
||||
public Optional<ObjectMapper> getObjectMapper() {
|
||||
return Optional.empty();
|
||||
|
@ -23,6 +36,22 @@ public class ServerApiController implements ServerApi {
|
|||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<Void> playerRegister(PlayerRegistrationData body) {
|
||||
if(playerRepository.existsByUsername(body.getUsername())){
|
||||
return new ResponseEntity<>(HttpStatus.CONFLICT);
|
||||
}
|
||||
Player newPlayer = new Player();
|
||||
newPlayer.setUsername(body.getUsername());
|
||||
try{
|
||||
playerService.setPassword(newPlayer, body.getPassword());
|
||||
} catch (NoSuchAlgorithmException exception){
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
playerRepository.save(newPlayer);
|
||||
return new ResponseEntity<>(HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResponseEntity<ServerHealth> serverGetHealthcheck() {
|
||||
ServerHealth health = new ServerHealth();
|
||||
|
|
Loading…
Add table
Reference in a new issue