Skip to content

Commit c37702e

Browse files
committed
Adds cache and mock LunagraphApi
1 parent 501a1c2 commit c37702e

File tree

8 files changed

+97
-15
lines changed

8 files changed

+97
-15
lines changed

leaderboards/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
<artifactId>quarkus-config-yaml</artifactId>
9999
</dependency>
100100

101+
<dependency>
102+
<groupId>io.quarkus</groupId>
103+
<artifactId>quarkus-cache</artifactId>
104+
</dependency>
105+
101106
<!-- Quarkus Test -->
102107
<dependency>
103108
<groupId>io.quarkus</groupId>
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
package com.lunatech.leaderboard.client.graphql.lunagraph;
22

3+
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphPersonModel;
4+
import io.quarkus.arc.DefaultBean;
5+
import io.quarkus.arc.profile.IfBuildProfile;
36
import io.smallrye.graphql.client.typesafe.api.TypesafeGraphQLClientBuilder;
47
import org.eclipse.microprofile.jwt.JsonWebToken;
58

69
import javax.enterprise.context.RequestScoped;
10+
import javax.enterprise.inject.Produces;
711

12+
@RequestScoped
813
public class GraphQLClients {
914

10-
@RequestScoped
15+
@Produces
16+
@IfBuildProfile("prod")
1117
public LunagraphApi lunagraphApi(JsonWebToken jwt) {
1218
return TypesafeGraphQLClientBuilder.newBuilder()
1319
.header("Authorization", "Bearer " + jwt.getRawToken())
1420
.configKey("lunagraph")
1521
.build(LunagraphApi.class);
1622
}
23+
24+
@RequestScoped
25+
@DefaultBean
26+
public LunagraphApi mockLunagraphApi() {
27+
return email -> new LunagraphPersonModel("Mock user: " + email, email);
28+
}
1729
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.lunatech.leaderboard.dto.game.GameDto;
44
import com.lunatech.leaderboard.entity.Game;
55
import com.lunatech.leaderboard.mapper.game.GameDtoMapper;
6+
import com.lunatech.leaderboard.service.GameService;
67
import io.quarkus.security.Authenticated;
78
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
89
import org.eclipse.microprofile.openapi.annotations.media.Content;
@@ -18,6 +19,7 @@
1819
import javax.ws.rs.core.MediaType;
1920
import javax.ws.rs.core.Response;
2021
import java.net.URI;
22+
import java.util.Collection;
2123
import java.util.List;
2224

2325
@Path("/games")
@@ -29,13 +31,16 @@ public class GameController {
2931
@Inject
3032
GameDtoMapper gameDtoMapper;
3133

34+
@Inject
35+
GameService gameService;
36+
3237
@GET
3338
@APIResponse(content = @Content(schema = @Schema(
3439
type = SchemaType.ARRAY,
3540
implementation = GameDto.class
3641
)))
3742
public Response list() {
38-
List<Game> games = Game.findAll().list();
43+
Collection<Game> games = gameService.findAll();
3944
List<GameDto> dtos = games.stream().map(GameDto::new).toList();
4045
return Response.ok(dtos).build();
4146
}
@@ -46,7 +51,7 @@ public Response list() {
4651
@RolesAllowed("admin")
4752
public Response add(@Valid GameDto body) {
4853
Game game = gameDtoMapper.toEntity(body);
49-
game.persist();
54+
gameService.add(game);
5055
return Response.created(URI.create("/games/"+game.id))
5156
.entity(new GameDto(game))
5257
.build();
@@ -57,8 +62,7 @@ public Response add(@Valid GameDto body) {
5762
@Transactional
5863
@APIResponseSchema(GameDto.class)
5964
public Response delete(Long gameId) {
60-
Game game = Game.findById(gameId);
61-
game.delete();
65+
Game game = gameService.delete(gameId);
6266
return Response.ok(game).build();
6367
}
6468
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.lunatech.leaderboard.entity.Game;
77
import com.lunatech.leaderboard.entity.GameMode;
88
import com.lunatech.leaderboard.mapper.gamemode.GameModeDtoMapper;
9+
import com.lunatech.leaderboard.service.GameModeService;
910
import io.quarkus.security.Authenticated;
1011
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
1112
import org.eclipse.microprofile.openapi.annotations.media.Content;
@@ -33,6 +34,9 @@ public class GameModeController {
3334
@Inject
3435
GameModeDtoMapper gameModeDtoMapper;
3536

37+
@Inject
38+
GameModeService gameModeService;
39+
3640
@PathParam("gameId")
3741
private Long gameId;
3842

@@ -42,8 +46,7 @@ public class GameModeController {
4246
implementation = GameModeDto.class
4347
)))
4448
public Response list() {
45-
Game game = Game.findById(gameId);
46-
List<GameMode> gameModes = game.gameModes;
49+
List<GameMode> gameModes = gameModeService.findAllByGame(gameId);
4750
List<GameModeDto> response = gameModes.stream().map(GameModeDto::new).toList();
4851

4952
return Response.ok(response).build();
@@ -65,10 +68,7 @@ public Response add(@Valid GameModePostDto body) {
6568
gameModeDtoMapper.setGameId(gameId);
6669
GameMode gameMode = gameModeDtoMapper.toEntity(body);
6770

68-
if(!Objects.equals(gameMode.game.id, gameId))
69-
throw new BadRequestException("GameMode game does not match path");
70-
71-
gameMode.persist();
71+
gameModeService.add(gameMode);
7272

7373
return Response.created(URI.create("/games/"+gameId+"/gamemodes/"+gameMode.id))
7474
.entity(new GameModeDto(gameMode))
@@ -79,12 +79,12 @@ public Response add(@Valid GameModePostDto body) {
7979
@Path("/{gameModeId}")
8080
@Transactional
8181
@APIResponseSchema(GameModeDto.class)
82-
public Response delete(@Valid Long gameModeId) {
82+
public Response delete(Long gameModeId) {
8383
GameMode gameMode = GameMode.findById(gameModeId);
8484
if(!Objects.equals(gameMode.game.id, gameId))
8585
throw new BadRequestException("GameMode game does not match path");
8686

87-
gameMode.delete();
87+
gameModeService.delete(gameMode);
8888

8989
return Response.ok(new GameModeDto(gameMode)).build();
9090
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public Response list() {
5656
@POST
5757
@Transactional
5858
@APIResponseSchema(MatchDto.class)
59-
@RolesAllowed("admin")
6059
public Response add(MatchPostDto body) {
6160
matchDtoMapper.setGameModeId(gameModeId);
6261
Match match = matchDtoMapper.toEntity(body);
62+
6363
matchService.save(match);
6464

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

leaderboards/src/main/java/com/lunatech/leaderboard/entity/Game.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Game extends PanacheEntityBase {
2222
@Column(name = "image_url")
2323
public String imageUrl;
2424

25-
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL)
25+
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
2626
public List<GameMode> gameModes = new ArrayList<>();
2727

2828
public static Game findByIdWithGameModes(Long id) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.lunatech.leaderboard.service;
2+
3+
import com.lunatech.leaderboard.entity.Game;
4+
import com.lunatech.leaderboard.entity.GameMode;
5+
import io.quarkus.cache.CacheInvalidate;
6+
import io.quarkus.cache.CacheResult;
7+
8+
import javax.enterprise.context.ApplicationScoped;
9+
import java.util.List;
10+
11+
@ApplicationScoped
12+
public class GameModeService {
13+
14+
@CacheResult(cacheName = "game_mode")
15+
public List<GameMode> findAllByGame(Long gameId) {
16+
Game game = Game.findByIdWithGameModes(gameId);
17+
return game.gameModes;
18+
}
19+
20+
@CacheInvalidate(cacheName = "game_mode")
21+
public GameMode add(GameMode gameMode) {
22+
gameMode.persist();
23+
return gameMode;
24+
}
25+
26+
@CacheInvalidate(cacheName = "games")
27+
public GameMode delete(GameMode gameMode) {
28+
gameMode.delete();
29+
return gameMode;
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lunatech.leaderboard.service;
2+
3+
import com.lunatech.leaderboard.entity.Game;
4+
import io.quarkus.cache.CacheInvalidate;
5+
import io.quarkus.cache.CacheResult;
6+
7+
import javax.enterprise.context.ApplicationScoped;
8+
import java.util.Collection;
9+
10+
@ApplicationScoped
11+
public class GameService {
12+
13+
@CacheResult(cacheName = "games")
14+
public Collection<Game> findAll() {
15+
return Game.<Game>findAll().list();
16+
}
17+
18+
@CacheInvalidate(cacheName = "games")
19+
public Game add(Game game) {
20+
game.persist();
21+
return game;
22+
}
23+
24+
@CacheInvalidate(cacheName = "games")
25+
public Game delete(Long gameId) {
26+
Game game = Game.findById(gameId);
27+
game.delete();
28+
return game;
29+
}
30+
}

0 commit comments

Comments
 (0)