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 ;
20- import org .springframework .security .core .Authentication ;
2118import org .springframework .security .jwt .Jwt ;
2219import org .springframework .stereotype .Service ;
20+ import org .springframework .transaction .annotation .Transactional ;
21+ import org .springframework .util .Assert ;
2322
2423import java .time .Instant ;
2524import java .time .temporal .ChronoUnit ;
2625import java .util .Collections ;
26+ import java .util .List ;
2727
2828@ Service
2929@ RequiredArgsConstructor
3030public class ClanService {
3131
3232 private final ClanRepository clanRepository ;
33- private final PlayerRepository playerRepository ;
3433 private final FafApiProperties fafApiProperties ;
3534 private final JwtService jwtService ;
3635 private final ObjectMapper objectMapper ;
3736 private final PlayerService playerService ;
3837 private final ClanMembershipRepository clanMembershipRepository ;
3938
39+ @ Transactional
40+ public void preCreate (Clan clan ) {
41+ Assert .isNull (clan .getId (), "Clan payload with id can not be used for creation." );
42+
43+ Player player = playerService .getCurrentPlayer ();
44+
45+ if (player .getClanMembership () != null ) {
46+ throw ApiException .of (ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN );
47+ }
48+
49+ if (!player .equals (clan .getFounder ())) {
50+ throw ApiException .of (ErrorCode .CLAN_INVALID_FOUNDER );
51+ }
52+
53+ clanRepository .findOneByName (clan .getName ()).ifPresent (c -> {
54+ throw ApiException .of (ErrorCode .CLAN_NAME_EXISTS , clan .getName ());
55+ });
56+
57+ clanRepository .findOneByTag (clan .getTag ()).ifPresent (c -> {
58+ throw ApiException .of (ErrorCode .CLAN_TAG_EXISTS , clan .getTag ());
59+ });
60+
61+ clan .setLeader (player );
62+ clan .setMemberships (List .of (new ClanMembership ()
63+ .setClan (clan )
64+ .setPlayer (player )));
65+ }
66+
4067 @ SneakyThrows
41- Clan create (String name , String tag , String description , Player creator ) {
68+ @ Transactional
69+ @ Deprecated
70+ // use preCreate instead
71+ Clan create (String name , String tag , String description ) {
72+ Player creator = playerService .getCurrentPlayer ();
73+
4274 if (creator .getClanMembership () != null ) {
43- throw new ApiException ( new Error ( ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN ) );
75+ throw ApiException . of ( ErrorCode .CLAN_CREATE_FOUNDER_IS_IN_A_CLAN );
4476 }
4577 if (clanRepository .findOneByName (name ).isPresent ()) {
46- throw new ApiException ( new Error ( ErrorCode .CLAN_NAME_EXISTS , name ) );
78+ throw ApiException . of ( ErrorCode .CLAN_NAME_EXISTS , name );
4779 }
4880 if (clanRepository .findOneByTag (tag ).isPresent ()) {
49- throw new ApiException ( new Error ( ErrorCode .CLAN_TAG_EXISTS , tag ) );
81+ throw ApiException . of ( ErrorCode .CLAN_TAG_EXISTS , tag );
5082 }
5183
5284 Clan clan = new Clan ();
@@ -69,16 +101,18 @@ Clan create(String name, String tag, String description, Player creator) {
69101 }
70102
71103 @ SneakyThrows
72- String generatePlayerInvitationToken (Player requester , int newMemberId , int clanId ) {
104+ @ Transactional
105+ String generatePlayerInvitationToken (int newMemberId , int clanId ) {
106+ Player requester = playerService .getCurrentPlayer ();
107+
73108 Clan clan = clanRepository .findById (clanId )
74109 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
75110
76111 if (requester .getId () != clan .getLeader ().getId ()) {
77- throw new ApiException ( new Error ( ErrorCode .CLAN_NOT_LEADER , clanId ) );
112+ throw ApiException . of ( ErrorCode .CLAN_NOT_LEADER , clanId );
78113 }
79114
80- Player newMember = playerRepository .findById (newMemberId )
81- .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_GENERATE_LINK_PLAYER_NOT_FOUND , newMemberId )));
115+ Player newMember = playerService .getById (newMemberId );
82116
83117 long expire = Instant .now ()
84118 .plus (fafApiProperties .getClan ().getInviteLinkExpireDurationMinutes (), ChronoUnit .MINUTES )
@@ -91,28 +125,27 @@ String generatePlayerInvitationToken(Player requester, int newMemberId, int clan
91125 }
92126
93127 @ SneakyThrows
94- void acceptPlayerInvitationToken (String stringToken , Authentication authentication ) {
128+ @ Transactional
129+ void acceptPlayerInvitationToken (String stringToken ) {
95130 Jwt token = jwtService .decodeAndVerify (stringToken );
96131 InvitationResult invitation = objectMapper .readValue (token .getClaims (), InvitationResult .class );
97132
98133 if (invitation .isExpired ()) {
99- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE ) );
134+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE );
100135 }
101136
102137 final Integer clanId = invitation .getClan ().getId ();
103- Player player = playerService .getPlayer ( authentication );
138+ Player player = playerService .getCurrentPlayer ( );
104139 Clan clan = clanRepository .findById (clanId )
105140 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
106141
107- Player newMember = playerRepository .findById (invitation .getNewMember ().getId ())
108- .orElseThrow (() -> new ProgrammingError ("ClanMember does not exist: " + invitation .getNewMember ().getId ()));
109-
142+ Player newMember = playerService .getById (invitation .getNewMember ().getId ());
110143
111144 if (player .getId () != newMember .getId ()) {
112- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER ) );
145+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER );
113146 }
114147 if (newMember .getClan () != null ) {
115- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN ) );
148+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN );
116149 }
117150
118151 ClanMembership membership = new ClanMembership ();
0 commit comments