1010import com .faforever .api .error .ApiException ;
1111import com .faforever .api .error .Error ;
1212import com .faforever .api .error .ErrorCode ;
13- import com .faforever .api .error .ProgrammingError ;
14- import com .faforever .api .player .PlayerRepository ;
1513import com .faforever .api .player .PlayerService ;
1614import com .faforever .api .security .JwtService ;
1715import com .fasterxml .jackson .databind .ObjectMapper ;
1816import lombok .RequiredArgsConstructor ;
1917import lombok .SneakyThrows ;
18+ import org .springframework .security .jwt .Jwt ;
2019import org .springframework .security .core .Authentication ;
2120import org .springframework .stereotype .Service ;
21+ import org .springframework .transaction .annotation .Transactional ;
22+ import org .springframework .util .Assert ;
2223
2324import java .time .Instant ;
2425import java .time .temporal .ChronoUnit ;
2526import java .util .Objects ;
2627import java .util .Set ;
28+ import java .util .List ;
2729
2830@ Service
2931@ RequiredArgsConstructor
3032public class ClanService {
3133
3234 private final ClanRepository clanRepository ;
33- private final PlayerRepository playerRepository ;
3435 private final FafApiProperties fafApiProperties ;
3536 private final JwtService jwtService ;
3637 private final ObjectMapper objectMapper ;
3738 private final PlayerService playerService ;
3839 private final ClanMembershipRepository clanMembershipRepository ;
3940
41+ @ Transactional
42+ public void preCreate (Clan clan ) {
43+ Assert .isNull (clan .getId (), "Clan payload with id can not be used for creation." );
44+
45+ Player player = playerService .getCurrentPlayer ();
46+
47+ if (player .getClanMembership () != null ) {
48+ throw ApiException .of (ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN );
49+ }
50+
51+ if (!player .equals (clan .getFounder ())) {
52+ throw ApiException .of (ErrorCode .CLAN_INVALID_FOUNDER );
53+ }
54+
55+ clanRepository .findOneByName (clan .getName ()).ifPresent (c -> {
56+ throw ApiException .of (ErrorCode .CLAN_NAME_EXISTS , clan .getName ());
57+ });
58+
59+ clanRepository .findOneByTag (clan .getTag ()).ifPresent (c -> {
60+ throw ApiException .of (ErrorCode .CLAN_TAG_EXISTS , clan .getTag ());
61+ });
62+
63+ clan .setLeader (player );
64+ clan .setMemberships (List .of (new ClanMembership ()
65+ .setClan (clan )
66+ .setPlayer (player )));
67+ }
68+
4069 @ SneakyThrows
41- Clan create (String name , String tag , String description , Player creator ) {
70+ @ Transactional
71+ @ Deprecated
72+ // use preCreate instead
73+ Clan create (String name , String tag , String description ) {
74+ Player creator = playerService .getCurrentPlayer ();
75+
4276 if (creator .getClanMembership () != null ) {
43- throw new ApiException ( new Error ( ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN ) );
77+ throw ApiException . of ( ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN );
4478 }
4579 if (clanRepository .findOneByName (name ).isPresent ()) {
46- throw new ApiException ( new Error ( ErrorCode .CLAN_NAME_EXISTS , name ) );
80+ throw ApiException . of ( ErrorCode .CLAN_NAME_EXISTS , name );
4781 }
4882 if (clanRepository .findOneByTag (tag ).isPresent ()) {
49- throw new ApiException ( new Error ( ErrorCode .CLAN_TAG_EXISTS , tag ) );
83+ throw ApiException . of ( ErrorCode .CLAN_TAG_EXISTS , tag );
5084 }
5185
5286 Clan clan = new Clan ();
@@ -70,16 +104,18 @@ Clan create(String name, String tag, String description, Player creator) {
70104 }
71105
72106 @ SneakyThrows
73- String generatePlayerInvitationToken (Player requester , int newMemberId , int clanId ) {
107+ @ Transactional
108+ String generatePlayerInvitationToken (int newMemberId , int clanId ) {
109+ Player requester = playerService .getCurrentPlayer ();
110+
74111 Clan clan = clanRepository .findById (clanId )
75112 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
76113
77114 if (!requester .getId ().equals (clan .getLeader ().getId ())) {
78- throw new ApiException ( new Error ( ErrorCode .CLAN_NOT_LEADER , clanId ) );
115+ throw ApiException . of ( ErrorCode .CLAN_NOT_LEADER , clanId );
79116 }
80117
81- Player newMember = playerRepository .findById (newMemberId )
82- .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_GENERATE_LINK_PLAYER_NOT_FOUND , newMemberId )));
118+ Player newMember = playerService .getById (newMemberId );
83119
84120 long expire = Instant .now ()
85121 .plus (fafApiProperties .getClan ().getInviteLinkExpireDurationMinutes (), ChronoUnit .MINUTES )
@@ -92,28 +128,26 @@ String generatePlayerInvitationToken(Player requester, int newMemberId, int clan
92128 }
93129
94130 @ SneakyThrows
95- void acceptPlayerInvitationToken (String stringToken , Authentication authentication ) {
96- String decodedToken = jwtService .decodeAndVerify (stringToken );
97- InvitationResult invitation = objectMapper .readValue (decodedToken , InvitationResult .class );
131+ void acceptPlayerInvitationToken (String stringToken ) {
132+ Jwt token = jwtService .decodeAndVerify (stringToken );
133+ InvitationResult invitation = objectMapper .readValue (token . getClaims () , InvitationResult .class );
98134
99135 if (invitation .isExpired ()) {
100- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE ) );
136+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE );
101137 }
102138
103139 final Integer clanId = invitation .clan ().id ();
104- Player player = playerService .getPlayer ( authentication );
140+ Player player = playerService .getCurrentPlayer ( );
105141 Clan clan = clanRepository .findById (clanId )
106142 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
107143
108- Player newMember = playerRepository .findById (invitation .newMember ().id ())
109- .orElseThrow (() -> new ProgrammingError ("ClanMember does not exist: " + invitation .newMember ().id ()));
110-
144+ Player newMember = playerService .getById (invitation .newMember ().getId ());
111145
112146 if (!Objects .equals (player .getId (), newMember .getId ())) {
113- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER ) );
147+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER );
114148 }
115149 if (newMember .getClan () != null ) {
116- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN ) );
150+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN );
117151 }
118152
119153 ClanMembership membership = new ClanMembership ();
0 commit comments