TD-34: Solved comments made by reviewer
All checks were successful
Quality Check / Validate OAS (push) Successful in 33s
Quality Check / Validate OAS (pull_request) Successful in 34s
Quality Check / Testing (push) Successful in 1m13s
Quality Check / Linting (push) Successful in 1m16s
Quality Check / Static Analysis (push) Successful in 1m22s
Quality Check / Static Analysis (pull_request) Successful in 1m15s
Quality Check / Linting (pull_request) Successful in 1m30s
Quality Check / Testing (pull_request) Successful in 50s

This commit is contained in:
mehdiboudjoudi 2025-02-26 11:51:45 +01:00
parent 2ccc9f14c5
commit 2740cc53be
5 changed files with 48 additions and 31 deletions

View file

@ -59,9 +59,9 @@ components:
required: required:
- username - username
############################################# #############################################
# PlayerApiModel # # AdministratablePlayer #
############################################# #############################################
PlayerApiModel: AdministratablePlayer:
description: a Player description: a Player
type: object type: object
properties: properties:
@ -205,7 +205,7 @@ paths:
schema: schema:
type: array type: array
items: items:
$ref: "#/components/schemas/PlayerApiModel" $ref: "#/components/schemas/AdministratablePlayer"
401: 401:
$ref: "#/components/responses/401Unauthorized" $ref: "#/components/responses/401Unauthorized"
500: 500:

View file

@ -4,8 +4,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import de.towerdefence.server.auth.UserSession; import de.towerdefence.server.auth.UserSession;
import de.towerdefence.server.oas.AdminApi; import de.towerdefence.server.oas.AdminApi;
import de.towerdefence.server.oas.models.AdminAuthInfo; import de.towerdefence.server.oas.models.AdminAuthInfo;
import de.towerdefence.server.oas.models.AdministratablePlayer;
import de.towerdefence.server.oas.models.GetAllPlayersConfigurationData; import de.towerdefence.server.oas.models.GetAllPlayersConfigurationData;
import de.towerdefence.server.oas.models.PlayerApiModel;
import de.towerdefence.server.player.Player; import de.towerdefence.server.player.Player;
import de.towerdefence.server.player.PlayerRepository; import de.towerdefence.server.player.PlayerRepository;
import de.towerdefence.server.utils.PlayerMapperService; import de.towerdefence.server.utils.PlayerMapperService;
@ -54,7 +54,7 @@ public class AdminApiController implements AdminApi {
} }
@Override @Override
public ResponseEntity<List<PlayerApiModel>> getAllPlayers(GetAllPlayersConfigurationData body) { public ResponseEntity<List<AdministratablePlayer>> getAllPlayers(GetAllPlayersConfigurationData body) {
var order = body.getOrder(); var order = body.getOrder();
var page = body.getPage(); var page = body.getPage();
@ -65,7 +65,14 @@ public class AdminApiController implements AdminApi {
if (order == null) { if (order == null) {
order = GetAllPlayersConfigurationData.OrderEnum.DESCENDING; order = GetAllPlayersConfigurationData.OrderEnum.DESCENDING;
} }
Sort.Direction direction = order == GetAllPlayersConfigurationData.OrderEnum.ASCENDING ? Sort.Direction.ASC : Sort.Direction.DESC; Sort.Direction direction;
if (order == GetAllPlayersConfigurationData.OrderEnum.ASCENDING) {
direction = Sort.Direction.ASC;
} else {
direction = Sort.Direction.DESC;
}
Pageable pageable = PageRequest.of(page, pageSize, Sort.by(direction, sortBy)); Pageable pageable = PageRequest.of(page, pageSize, Sort.by(direction, sortBy));
Page<Player> playerPage; Page<Player> playerPage;
@ -76,8 +83,13 @@ public class AdminApiController implements AdminApi {
playerPage = playerRepository.findAll(pageable); playerPage = playerRepository.findAll(pageable);
} }
List<PlayerApiModel> playersMapped = playerMapperService.mapPlayerToApiPlayers(playerPage.getContent()); List<AdministratablePlayer> playersMapped =
playerMapperService.mapPlayersToAdministratablePlayers(playerPage.getContent());
return playersMapped.isEmpty() ? ResponseEntity.noContent().build() : ResponseEntity.ok(playersMapped); if (playersMapped.isEmpty()) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.ok(playersMapped);
}
} }
} }

View file

@ -1,6 +1,6 @@
package de.towerdefence.server.utils; package de.towerdefence.server.utils;
import de.towerdefence.server.oas.models.PlayerApiModel; import de.towerdefence.server.oas.models.AdministratablePlayer;
import de.towerdefence.server.player.Player; import de.towerdefence.server.player.Player;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -10,13 +10,19 @@ import java.util.List;
@Component @Component
public class PlayerMapperService { public class PlayerMapperService {
public List<PlayerApiModel> mapPlayerToApiPlayers(List<Player> players) { public List<AdministratablePlayer> mapPlayersToAdministratablePlayers(List<Player> players) {
List<PlayerApiModel> apiPlayers = new ArrayList<>(); List<AdministratablePlayer> apiPlayers = new ArrayList<>();
for (Player player : players) { for (Player player : players) {
PlayerApiModel apiPlayer = new PlayerApiModel(); AdministratablePlayer apiPlayer = new AdministratablePlayer();
apiPlayer.setUsername(player.getUsername()); apiPlayer.setUsername(player.getUsername());
apiPlayers.add(apiPlayer); apiPlayers.add(apiPlayer);
} }
return apiPlayers; return apiPlayers;
} }
public AdministratablePlayer mapPlayerToAdministratablePlayer(Player player) {
AdministratablePlayer apiPlayer = new AdministratablePlayer();
apiPlayer.setUsername(player.getUsername());
return apiPlayer;
}
} }

View file

@ -13,6 +13,8 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest @SpringBootTest
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
@ -34,23 +36,20 @@ public abstract class IntegrationTest {
void setUp() { void setUp() {
playerRepository.deleteAll(); playerRepository.deleteAll();
Player player1 = new Player(); Map<String, String> players = new HashMap<>();
player1.setUsername("Alex"); players.put("Alex", "1234");
try { players.put("Zorro", "1234");
playerService.setPassword(player1, "1234");
} catch (NoSuchAlgorithmException e) {return;}
this.playerRepository.save(player1); players.forEach((username, password) -> {
Player player = new Player();
Player player2 = new Player(); player.setUsername(username);
player2.setUsername("Zorro"); try {
try { playerService.setPassword(player, password);
playerService.setPassword(player2, "1234"); playerRepository.save(player);
} catch (NoSuchAlgorithmException e) {return;} } catch (NoSuchAlgorithmException e) {
System.err.println("Error setting password for player: " + username);
this.playerRepository.save(player2); }
});
System.out.println("LISTE: " + playerRepository.findAll());
} }
@AfterEach @AfterEach
@ -58,5 +57,5 @@ public abstract class IntegrationTest {
playerRepository.deleteAll(); playerRepository.deleteAll();
} }
} }

View file

@ -9,7 +9,7 @@ import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilde
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
class GetAllPlayersPaginatedAndSortedTest extends IntegrationTest { class GetAllPlayersTest extends IntegrationTest {
private MockHttpServletRequestBuilder createGetAllPlayersRequest(String requestBody) { private MockHttpServletRequestBuilder createGetAllPlayersRequest(String requestBody) {
return MockMvcRequestBuilders.get(baseUri + "/admin/players") return MockMvcRequestBuilders.get(baseUri + "/admin/players")
@ -80,4 +80,4 @@ class GetAllPlayersPaginatedAndSortedTest extends IntegrationTest {
.andExpect(jsonPath("$[0].username").value("Alex")) .andExpect(jsonPath("$[0].username").value("Alex"))
.andExpect(jsonPath("$").isNotEmpty()); .andExpect(jsonPath("$").isNotEmpty());
} }
} }