Skip to content

Commit 501a1c2

Browse files
committed
Adds dtomappers for dto -> entity conversion
1 parent 5d6003e commit 501a1c2

File tree

18 files changed

+189
-90
lines changed

18 files changed

+189
-90
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import com.lunatech.leaderboard.dto.game.GameDto;
44
import com.lunatech.leaderboard.entity.Game;
5+
import com.lunatech.leaderboard.mapper.game.GameDtoMapper;
56
import io.quarkus.security.Authenticated;
67
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
78
import org.eclipse.microprofile.openapi.annotations.media.Content;
89
import org.eclipse.microprofile.openapi.annotations.media.Schema;
910
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1011
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
1112

13+
import javax.annotation.security.RolesAllowed;
14+
import javax.inject.Inject;
1215
import javax.transaction.Transactional;
1316
import javax.validation.Valid;
1417
import javax.ws.rs.*;
@@ -23,6 +26,9 @@
2326
@Authenticated
2427
public class GameController {
2528

29+
@Inject
30+
GameDtoMapper gameDtoMapper;
31+
2632
@GET
2733
@APIResponse(content = @Content(schema = @Schema(
2834
type = SchemaType.ARRAY,
@@ -37,8 +43,9 @@ public Response list() {
3743
@POST
3844
@Transactional
3945
@APIResponseSchema(GameDto.class)
46+
@RolesAllowed("admin")
4047
public Response add(@Valid GameDto body) {
41-
Game game = body.toEntity();
48+
Game game = gameDtoMapper.toEntity(body);
4249
game.persist();
4350
return Response.created(URI.create("/games/"+game.id))
4451
.entity(new GameDto(game))

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

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

33
import com.lunatech.leaderboard.dto.gamemode.GameModeDto;
4-
import com.lunatech.leaderboard.dto.gamemode.GameModeLeaderboardDto;
4+
import com.lunatech.leaderboard.dto.gamemode.GameModeDetailDto;
55
import com.lunatech.leaderboard.dto.gamemode.GameModePostDto;
66
import com.lunatech.leaderboard.entity.Game;
77
import com.lunatech.leaderboard.entity.GameMode;
8+
import com.lunatech.leaderboard.mapper.gamemode.GameModeDtoMapper;
89
import io.quarkus.security.Authenticated;
910
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
1011
import org.eclipse.microprofile.openapi.annotations.media.Content;
1112
import org.eclipse.microprofile.openapi.annotations.media.Schema;
1213
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1314
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
1415

16+
import javax.annotation.security.RolesAllowed;
17+
import javax.inject.Inject;
1518
import javax.transaction.Transactional;
1619
import javax.validation.Valid;
1720
import javax.ws.rs.*;
@@ -27,6 +30,9 @@
2730
@Authenticated
2831
public class GameModeController {
2932

33+
@Inject
34+
GameModeDtoMapper gameModeDtoMapper;
35+
3036
@PathParam("gameId")
3137
private Long gameId;
3238

@@ -48,21 +54,24 @@ public Response list() {
4854
@APIResponseSchema(GameModeDto.class)
4955
public Response leaderboard(Long gameModeId) {
5056
GameMode gameMode = GameMode.findByIdWithLeaderboard(gameModeId);
51-
return Response.ok(new GameModeLeaderboardDto(gameMode)).build();
57+
return Response.ok(new GameModeDetailDto(gameMode)).build();
5258
}
5359

5460
@POST
5561
@Transactional
5662
@APIResponseSchema(GameModeDto.class)
63+
@RolesAllowed("admin")
5764
public Response add(@Valid GameModePostDto body) {
58-
GameMode gameMode = body.toEntity();
65+
gameModeDtoMapper.setGameId(gameId);
66+
GameMode gameMode = gameModeDtoMapper.toEntity(body);
67+
5968
if(!Objects.equals(gameMode.game.id, gameId))
6069
throw new BadRequestException("GameMode game does not match path");
6170

6271
gameMode.persist();
6372

6473
return Response.created(URI.create("/games/"+gameId+"/gamemodes/"+gameMode.id))
65-
.entity(new GameModePostDto(gameMode))
74+
.entity(new GameModeDto(gameMode))
6675
.build();
6776
}
6877

@@ -77,6 +86,6 @@ public Response delete(@Valid Long gameModeId) {
7786

7887
gameMode.delete();
7988

80-
return Response.ok(new GameModePostDto(gameMode)).build();
89+
return Response.ok(new GameModeDto(gameMode)).build();
8190
}
8291
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import com.lunatech.leaderboard.dto.match.MatchPostDto;
55
import com.lunatech.leaderboard.entity.Match;
66
import com.lunatech.leaderboard.entity.User;
7+
import com.lunatech.leaderboard.mapper.match.MatchDtoMapper;
78
import com.lunatech.leaderboard.service.MatchService;
8-
import com.lunatech.leaderboard.service.UserService;
99
import io.quarkus.security.Authenticated;
1010
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
1111
import org.eclipse.microprofile.openapi.annotations.media.Content;
@@ -14,6 +14,7 @@
1414
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1515
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
1616

17+
import javax.annotation.security.RolesAllowed;
1718
import javax.inject.Inject;
1819
import javax.transaction.Transactional;
1920
import javax.ws.rs.*;
@@ -33,7 +34,7 @@ public class MatchController {
3334
MatchService matchService;
3435

3536
@Inject
36-
UserService userService;
37+
MatchDtoMapper matchDtoMapper;
3738

3839
@PathParam("gameId")
3940
private Long gameId;
@@ -55,8 +56,10 @@ public Response list() {
5556
@POST
5657
@Transactional
5758
@APIResponseSchema(MatchDto.class)
59+
@RolesAllowed("admin")
5860
public Response add(MatchPostDto body) {
59-
Match match = body.toEntity(userService);
61+
matchDtoMapper.setGameModeId(gameModeId);
62+
Match match = matchDtoMapper.toEntity(body);
6063
matchService.save(match);
6164

6265
return Response.created(URI.create("/games/"+gameId+"/gamemodes/"+gameModeId+"/matches/"+match.id))

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.lunatech.leaderboard.dto.user.UserDto;
44
import com.lunatech.leaderboard.dto.user.UserPostDto;
55
import com.lunatech.leaderboard.entity.User;
6+
import com.lunatech.leaderboard.mapper.user.UserDtoMapper;
67
import io.quarkus.security.Authenticated;
78
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
89
import org.eclipse.microprofile.openapi.annotations.media.Content;
@@ -11,6 +12,8 @@
1112
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
1213
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
1314

15+
import javax.annotation.security.RolesAllowed;
16+
import javax.inject.Inject;
1417
import javax.transaction.Transactional;
1518
import javax.ws.rs.*;
1619
import javax.ws.rs.core.MediaType;
@@ -24,6 +27,9 @@
2427
@Authenticated
2528
public class UserController {
2629

30+
@Inject
31+
UserDtoMapper userDtoMapper;
32+
2733
@Parameter(hidden = true)
2834
@HeaderParam("email")
2935
private String email;
@@ -57,13 +63,12 @@ public Response get(Long userId) {
5763
return Response.ok(dto).build();
5864
}
5965

60-
@PUT
66+
@POST
6167
@Transactional
62-
@Authenticated
68+
@RolesAllowed("admin")
6369
@APIResponseSchema(UserDto.class)
64-
//To fix, should retrieve user
6570
public Response add(UserPostDto body) {
66-
User user = body.toEntity(email);
71+
User user = userDtoMapper.toEntity(body);
6772
user.persist();
6873
return Response.created(URI.create("/users/" + user.id))
6974
.entity(new UserDto(user))

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,4 @@ public record GameDto(
1212
public GameDto(Game game) {
1313
this(game.id, game.name, game.imageUrl);
1414
}
15-
16-
public Game toEntity() {
17-
Game game = new Game();
18-
game.id = id;
19-
game.name = name;
20-
game.imageUrl = imageUrl;
21-
return game;
22-
}
2315
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.lunatech.leaderboard.dto.gamemode;
22

33
import com.lunatech.leaderboard.dto.game.GameDto;
4-
import com.lunatech.leaderboard.dto.leaderboarduser.LeaderboardUserDto;
4+
import com.lunatech.leaderboard.model.leaderboarduser.LeaderboardUserDto;
55
import com.lunatech.leaderboard.entity.GameMode;
66
import com.lunatech.leaderboard.entity.LeaderboardUser;
77

88
import java.util.Collection;
99
import java.util.Comparator;
1010
import java.util.List;
1111

12-
public record GameModeLeaderboardDto(Long id, String name, String rules, GameDto game, List<LeaderboardUserDto> leaderboard) {
13-
public GameModeLeaderboardDto(GameMode gameMode) {
12+
public record GameModeDetailDto(Long id, String name, String rules, GameDto game, List<LeaderboardUserDto> leaderboard) {
13+
public GameModeDetailDto(GameMode gameMode) {
1414
this(gameMode.id, gameMode.name, gameMode.rules, new GameDto(gameMode.game), leaderboardToDto(gameMode.leaderboard));
1515
}
1616

leaderboards/src/main/java/com/lunatech/leaderboard/dto/gamemode/GameModePostDto.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,5 @@
99
public record GameModePostDto(
1010
Long id,
1111
@NotBlank String name,
12-
@NotBlank String rules,
13-
@NotNull Long gameId
14-
) {
15-
public GameModePostDto(GameMode gameMode) {
16-
this(gameMode.id, gameMode.name, gameMode.rules, gameMode.game.id);
17-
}
18-
19-
public GameMode toFetchedEntity() {
20-
GameMode gameMode = toEntity();
21-
gameMode.game = Game.findById(gameMode.game.id);
22-
return gameMode;
23-
}
24-
25-
public GameMode toEntity() {
26-
GameMode gameMode = new GameMode();
27-
gameMode.id = id;
28-
gameMode.name = name;
29-
gameMode.rules = rules;
30-
31-
gameMode.game = new Game();
32-
gameMode.game.id = gameId;
33-
return gameMode;
34-
}
35-
}
12+
@NotBlank String rules
13+
) { }

leaderboards/src/main/java/com/lunatech/leaderboard/dto/match/MatchPostDto.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@
66
import com.lunatech.leaderboard.entity.MatchUser;
77
import com.lunatech.leaderboard.service.UserService;
88

9+
import javax.validation.constraints.NotEmpty;
10+
import javax.validation.constraints.NotNull;
911
import java.util.List;
1012

11-
public record MatchPostDto(Match.Outcome outcome, Long gameModeId, List<MatchUserPostDto> teamA, List<MatchUserPostDto> teamB) {
12-
13-
public Match toEntity(UserService userService) {
14-
GameMode gameMode = new GameMode();
15-
gameMode.id = gameModeId;
16-
17-
Match match = new Match();
18-
match.gameMode = gameMode;
19-
match.outcome = outcome;
20-
match.teamA = teamA.stream().map(dto -> dto.toEntity(match, MatchUser.Team.TEAM_A, userService)).toList();
21-
match.teamB = teamB.stream().map(dto -> dto.toEntity(match, MatchUser.Team.TEAM_B, userService)).toList();
22-
return match;
23-
}
13+
public record MatchPostDto(
14+
@NotNull Match.Outcome outcome,
15+
@NotEmpty List<MatchUserPostDto> teamA,
16+
@NotEmpty List<MatchUserPostDto> teamB) {
2417
}
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
package com.lunatech.leaderboard.dto.matchuser;
22

3-
import com.lunatech.leaderboard.entity.Match;
4-
import com.lunatech.leaderboard.entity.MatchUser;
5-
import com.lunatech.leaderboard.entity.User;
6-
import com.lunatech.leaderboard.service.UserService;
3+
import javax.validation.constraints.Email;
4+
import javax.validation.constraints.NotBlank;
75

8-
public record MatchUserPostDto(String userEmail) {
9-
10-
public MatchUser toEntity(Match match, MatchUser.Team team, UserService userService) {
11-
MatchUser matchUser = new MatchUser();
12-
matchUser.match = match;
13-
matchUser.team = team;
14-
matchUser.outcomeConfirmed = false;
15-
16-
matchUser.user = User.findByEmail(userEmail)
17-
.orElseGet(() -> userService.add(userEmail));
18-
return matchUser;
19-
}
6+
public record MatchUserPostDto(@Email @NotBlank String userEmail) {
207
}
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
package com.lunatech.leaderboard.dto.user;
22

3-
import com.lunatech.leaderboard.entity.User;
3+
import javax.validation.constraints.NotBlank;
44

5-
public record UserPostDto(String displayName, String profilePicUrl) {
6-
7-
public User toEntity(String email) {
8-
User user = new User();
9-
user.email = email;
10-
user.displayName = displayName;
11-
user.profilePicUrl = profilePicUrl;
12-
return user;
13-
}
5+
public record UserPostDto(
6+
@NotBlank String email,
7+
@NotBlank String displayName,
8+
String profilePicUrl) {
149
}

0 commit comments

Comments
 (0)