Skip to content

Commit 3636ba7

Browse files
committed
Changes user creation flow, they are now added when first inserted in a match
1 parent 6221d1a commit 3636ba7

File tree

10 files changed

+45
-55
lines changed

10 files changed

+45
-55
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.lunatech.leaderboard.client.graphql.lunagraph;
22

3-
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphEmployeeType;
4-
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphPeopleModel;
3+
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphPersonModel;
54
import org.eclipse.microprofile.graphql.Name;
65
import org.eclipse.microprofile.graphql.Query;
76

87
public interface LunagraphApi {
98

10-
@Query("people")
11-
LunagraphPeopleModel people(@Name("employeeType") LunagraphEmployeeType employeeType);
9+
@Query("person")
10+
LunagraphPersonModel person(@Name("emailAddress") String email);
1211
}

leaderboards/src/main/java/com/lunatech/leaderboard/client/graphql/lunagraph/model/LunagraphPeopleModel.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.lunatech.leaderboard.client.graphql.lunagraph.model;
2+
3+
4+
public record LunagraphPersonModel(String fullName, String emailAddress) {}
5+

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.lunatech.leaderboard.entity.Match;
66
import com.lunatech.leaderboard.entity.User;
77
import com.lunatech.leaderboard.service.MatchService;
8+
import com.lunatech.leaderboard.service.UserService;
89
import io.quarkus.security.Authenticated;
910
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
1011
import org.eclipse.microprofile.openapi.annotations.media.Content;
@@ -31,6 +32,9 @@ public class MatchController {
3132
@Inject
3233
MatchService matchService;
3334

35+
@Inject
36+
UserService userService;
37+
3438
@PathParam("gameId")
3539
private Long gameId;
3640

@@ -52,7 +56,7 @@ public Response list() {
5256
@Transactional
5357
@APIResponseSchema(MatchDto.class)
5458
public Response add(MatchPostDto body) {
55-
Match match = body.toEntity();
59+
Match match = body.toEntity(userService);
5660
matchService.save(match);
5761

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
import com.lunatech.leaderboard.entity.GameMode;
55
import com.lunatech.leaderboard.entity.Match;
66
import com.lunatech.leaderboard.entity.MatchUser;
7+
import com.lunatech.leaderboard.service.UserService;
78

89
import java.util.List;
910

1011
public record MatchPostDto(Match.Outcome outcome, Long gameModeId, List<MatchUserPostDto> teamA, List<MatchUserPostDto> teamB) {
1112

12-
public Match toEntity() {
13+
public Match toEntity(UserService userService) {
1314
GameMode gameMode = new GameMode();
1415
gameMode.id = gameModeId;
1516

1617
Match match = new Match();
1718
match.gameMode = gameMode;
1819
match.outcome = outcome;
19-
match.teamA = teamA.stream().map(dto -> dto.toEntity(match, MatchUser.Team.TEAM_A)).toList();
20-
match.teamB = teamB.stream().map(dto -> dto.toEntity(match, MatchUser.Team.TEAM_B)).toList();
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();
2122
return match;
2223
}
2324
}

leaderboards/src/main/java/com/lunatech/leaderboard/dto/matchuser/MatchUserPostDto.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
import com.lunatech.leaderboard.entity.Match;
44
import com.lunatech.leaderboard.entity.MatchUser;
55
import com.lunatech.leaderboard.entity.User;
6+
import com.lunatech.leaderboard.service.UserService;
67

7-
public record MatchUserPostDto(Long userId) {
8+
public record MatchUserPostDto(String userEmail) {
89

9-
public MatchUser toEntity(Match match, MatchUser.Team team) {
10+
public MatchUser toEntity(Match match, MatchUser.Team team, UserService userService) {
1011
MatchUser matchUser = new MatchUser();
1112
matchUser.match = match;
1213
matchUser.team = team;
1314
matchUser.outcomeConfirmed = false;
14-
matchUser.user = User.findById(userId);
15+
16+
matchUser.user = User.findByEmail(userEmail)
17+
.orElseGet(() -> userService.add(userEmail));
1518
return matchUser;
1619
}
1720
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
44

55
import javax.persistence.*;
6+
import java.util.Optional;
67

78
@Entity
89
@Table(name = "app_user")
@@ -22,4 +23,8 @@ public class User extends PanacheEntityBase {
2223

2324
@Column(name = "profilepicurl")
2425
public String profilePicUrl;
26+
27+
public static Optional<User> findByEmail(String email) {
28+
return find("email", email).firstResultOptional();
29+
}
2530
}

leaderboards/src/main/java/com/lunatech/leaderboard/scheduled/FetchUsersScheduler.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,35 @@
11
package com.lunatech.leaderboard.service;
22

33
import com.lunatech.leaderboard.client.graphql.lunagraph.LunagraphApi;
4-
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphEmployeeType;
5-
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphPeopleModel;
4+
import com.lunatech.leaderboard.client.graphql.lunagraph.model.LunagraphPersonModel;
65
import com.lunatech.leaderboard.entity.User;
7-
import io.quarkus.runtime.StartupEvent;
8-
import lombok.extern.java.Log;
6+
import io.smallrye.graphql.client.InvalidResponseException;
97

108
import javax.enterprise.context.ApplicationScoped;
11-
import javax.enterprise.event.Observes;
129
import javax.inject.Inject;
13-
import java.util.Set;
14-
import java.util.stream.Collectors;
10+
import javax.ws.rs.BadRequestException;
11+
import javax.ws.rs.InternalServerErrorException;
1512

1613
@ApplicationScoped
17-
@Log
1814
public class UserService {
1915

2016
@Inject
2117
LunagraphApi lunagraphApi;
2218

23-
public void startUp(@Observes StartupEvent event) {
24-
importAnniaUsers();
25-
}
26-
27-
public void importAnniaUsers() {
19+
public User add(String email) {
2820
try {
29-
Set<String> usersEmails = User.<User>streamAll().map(user -> user.email).collect(Collectors.toSet());
30-
31-
LunagraphPeopleModel peopleModel = lunagraphApi.people(LunagraphEmployeeType.INTERNAL);
32-
peopleModel.people().stream()
33-
.filter(person -> !usersEmails.contains(person.emailAddress()))
34-
.forEach(person -> add(person.emailAddress(), person.fullName()));
35-
} catch (Exception e) {
36-
log.severe(e.getMessage());
21+
LunagraphPersonModel person = lunagraphApi.person(email);
22+
if(person == null)
23+
throw new BadRequestException("No user found for email " + email);
24+
25+
User user = new User();
26+
user.email = email;
27+
user.displayName = person.fullName();
28+
user.persist();
29+
30+
return user;
31+
} catch (InvalidResponseException e) {
32+
throw new InternalServerErrorException("Something happened while trying to communicate with Lunagraph " + e.getMessage());
3733
}
3834
}
39-
40-
public User add(String emailAddress, String fullName) {
41-
User user = new User();
42-
user.email = emailAddress;
43-
user.displayName = fullName;
44-
user.persist();
45-
return user;
46-
}
4735
}

leaderboards/src/main/resources/application-dev.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ quarkus:
1515

1616
oidc-client:
1717
auth-server-url: http://localhost:8081/realms/Quarkus
18-
client-id: leaderboards-backend
1918
credentials:
2019
secret: secret
2120

0 commit comments

Comments
 (0)