Skip to content

Commit 5b097c3

Browse files
committed
Adds Response mapping
1 parent 7975120 commit 5b097c3

File tree

17 files changed

+103
-53
lines changed

17 files changed

+103
-53
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ nb-configuration.xml
3838

3939
# Local environment
4040
.env
41+
ignore

src/main/java/com/lunatech/leaderboard/controller/GameController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class GameController {
4141
)))
4242
public Response list() {
4343
Collection<Game> games = gameService.findAll();
44-
List<GameDto> dtos = games.stream().map(GameDto::new).toList();
44+
Collection<GameDto> dtos = gameDtoMapper.toDtos(games);
4545
return Response.ok(dtos).build();
4646
}
4747

@@ -53,7 +53,7 @@ public Response add(@Valid GameDto body) {
5353
Game game = gameDtoMapper.toEntity(body);
5454
gameService.add(game);
5555
return Response.created(URI.create("/games/"+game.id))
56-
.entity(new GameDto(game))
56+
.entity(gameDtoMapper.toDto(game))
5757
.build();
5858
}
5959

@@ -63,6 +63,6 @@ public Response add(@Valid GameDto body) {
6363
@APIResponseSchema(GameDto.class)
6464
public Response delete(Long gameId) {
6565
Game game = gameService.delete(gameId);
66-
return Response.ok(game).build();
66+
return Response.ok(gameDtoMapper.toDto(game)).build();
6767
}
6868
}

src/main/java/com/lunatech/leaderboard/controller/GameModeController.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.lunatech.leaderboard.controller;
22

33
import com.lunatech.leaderboard.dto.gamemode.GameModeDto;
4-
import com.lunatech.leaderboard.dto.gamemode.GameModeDetailDto;
54
import com.lunatech.leaderboard.dto.gamemode.GameModePostDto;
6-
import com.lunatech.leaderboard.entity.Game;
75
import com.lunatech.leaderboard.entity.GameMode;
86
import com.lunatech.leaderboard.mapper.gamemode.GameModeDtoMapper;
97
import com.lunatech.leaderboard.service.GameModeService;
@@ -22,6 +20,7 @@
2220
import javax.ws.rs.core.MediaType;
2321
import javax.ws.rs.core.Response;
2422
import java.net.URI;
23+
import java.util.Collection;
2524
import java.util.List;
2625
import java.util.Objects;
2726

@@ -47,8 +46,7 @@ public class GameModeController {
4746
)))
4847
public Response list() {
4948
List<GameMode> gameModes = gameModeService.findAllByGame(gameId);
50-
List<GameModeDto> response = gameModes.stream().map(GameModeDto::new).toList();
51-
49+
Collection<GameModeDto> response = gameModeDtoMapper.toDtos(gameModes);
5250
return Response.ok(response).build();
5351
}
5452

@@ -57,7 +55,7 @@ public Response list() {
5755
@APIResponseSchema(GameModeDto.class)
5856
public Response leaderboard(Long gameModeId) {
5957
GameMode gameMode = GameMode.findByIdWithLeaderboard(gameModeId);
60-
return Response.ok(new GameModeDetailDto(gameMode)).build();
58+
return Response.ok(gameModeDtoMapper.toDto(gameMode)).build();
6159
}
6260

6361
@POST
@@ -71,7 +69,7 @@ public Response add(@Valid GameModePostDto body) {
7169
gameModeService.add(gameMode);
7270

7371
return Response.created(URI.create("/games/"+gameId+"/gamemodes/"+gameMode.id))
74-
.entity(new GameModeDto(gameMode))
72+
.entity(gameModeDtoMapper.toDto(gameMode))
7573
.build();
7674
}
7775

@@ -86,6 +84,6 @@ public Response delete(Long gameModeId) {
8684

8785
gameModeService.delete(gameMode);
8886

89-
return Response.ok(new GameModeDto(gameMode)).build();
87+
return Response.ok(gameModeDtoMapper.toDto(gameMode)).build();
9088
}
9189
}

src/main/java/com/lunatech/leaderboard/controller/MatchController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MatchController {
4949
)))
5050
public Response list() {
5151
Collection<Match> matches = matchService.findByGameMode(gameModeId);
52-
List<MatchDto> matchesDto = matches.stream().map(MatchDto::new).toList();
52+
Collection<MatchDto> matchesDto = matchDtoMapper.toDtos(matches);
5353
return Response.ok(matchesDto).build();
5454
}
5555

@@ -63,7 +63,7 @@ public Response add(MatchPostDto body) {
6363
matchService.save(match);
6464

6565
return Response.created(URI.create("/games/"+gameId+"/gamemodes/"+gameModeId+"/matches/"+match.id))
66-
.entity(new MatchDto(match))
66+
.entity(matchDtoMapper.toDto(match))
6767
.build();
6868
}
6969

src/main/java/com/lunatech/leaderboard/controller/UserController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javax.ws.rs.core.MediaType;
2020
import javax.ws.rs.core.Response;
2121
import java.net.URI;
22+
import java.util.Collection;
2223
import java.util.List;
2324

2425
@Path("/users")
@@ -39,7 +40,7 @@ public class UserController {
3940
@APIResponseSchema(UserDto.class)
4041
public Response me() {
4142
return User.<User>find("email", email).singleResultOptional()
42-
.map(user -> Response.ok(new UserDto(user)).build())
43+
.map(user -> Response.ok(userDtoMapper.toDto(user)).build())
4344
.orElseThrow(() -> new NotFoundException("User with email " + email + " not in leaderboards"));
4445
}
4546

@@ -50,7 +51,7 @@ public Response me() {
5051
)))
5152
public Response list() {
5253
List<User> users = User.listAll();
53-
List<UserDto> dtos = users.stream().map(UserDto::new).toList();
54+
Collection<UserDto> dtos = userDtoMapper.toDtos(users);
5455
return Response.ok(dtos).build();
5556
}
5657

@@ -59,7 +60,7 @@ public Response list() {
5960
@APIResponseSchema(UserDto.class)
6061
public Response get(Long userId) {
6162
User user = User.findById(userId);
62-
UserDto dto = new UserDto(user);
63+
UserDto dto = userDtoMapper.toDto(user);
6364
return Response.ok(dto).build();
6465
}
6566

@@ -71,7 +72,7 @@ public Response add(UserPostDto body) {
7172
User user = userDtoMapper.toEntity(body);
7273
user.persist();
7374
return Response.created(URI.create("/users/" + user.id))
74-
.entity(new UserDto(user))
75+
.entity(userDtoMapper.toDto(user))
7576
.build();
7677
}
7778
}

src/main/java/com/lunatech/leaderboard/dto/game/GameDto.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,4 @@ public record GameDto(
99
@NotBlank String name,
1010
String imageUrl
1111
) {
12-
public GameDto(Game game) {
13-
this(game.id, game.name, game.imageUrl);
14-
}
1512
}

src/main/java/com/lunatech/leaderboard/dto/gamemode/GameModeDetailDto.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,4 @@
1010
import java.util.List;
1111

1212
public record GameModeDetailDto(Long id, String name, String rules, GameDto game, List<LeaderboardUserDto> leaderboard) {
13-
public GameModeDetailDto(GameMode gameMode) {
14-
this(gameMode.id, gameMode.name, gameMode.rules, new GameDto(gameMode.game), leaderboardToDto(gameMode.leaderboard));
15-
}
16-
17-
private static List<LeaderboardUserDto> leaderboardToDto(Collection<LeaderboardUser> leaderboard) {
18-
return leaderboard.stream()
19-
.map(LeaderboardUserDto::new)
20-
.sorted(Comparator.comparing(LeaderboardUserDto::score).reversed())
21-
.toList();
22-
}
2313
}

src/main/java/com/lunatech/leaderboard/dto/gamemode/GameModeDto.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,4 @@
33
import com.lunatech.leaderboard.entity.GameMode;
44

55
public record GameModeDto(Long id, Long gameId, String name, String rules) {
6-
public GameModeDto(GameMode gameMode) {
7-
this(gameMode.id, gameMode.game.id, gameMode.name, gameMode.rules);
8-
}
96
}

src/main/java/com/lunatech/leaderboard/dto/match/MatchDto.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,4 @@
77
import java.util.List;
88

99
public record MatchDto(Long id, Long gameModeId, Match.Outcome outcome, Boolean confirmed, List<MatchUserDto> teamA, List<MatchUserDto> teamB) {
10-
public MatchDto(Match match) {
11-
this(match.id, match.gameMode.id, match.outcome, match.confirmed, teamToDto(match.teamA), teamToDto(match.teamB));
12-
}
13-
14-
private static List<MatchUserDto> teamToDto(List<MatchUser> team) {
15-
return team.stream().map(MatchUserDto::matchDto).toList();
16-
}
1710
}

src/main/java/com/lunatech/leaderboard/dto/matchuser/MatchUserDto.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@
55
import com.lunatech.leaderboard.entity.MatchUser;
66

77
public record MatchUserDto(UserDto user, MatchDto match, MatchUser.Team team, boolean outcomeConfirmed) {
8-
public static MatchUserDto matchDto(MatchUser matchUser) {
9-
return new MatchUserDto(new UserDto(matchUser.user), null, matchUser.team, matchUser.outcomeConfirmed);
10-
}
118
}

0 commit comments

Comments
 (0)