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 .Set ;
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 ();
@@ -70,16 +102,18 @@ Clan create(String name, String tag, String description, Player creator) {
70102 }
71103
72104 @ SneakyThrows
73- String generatePlayerInvitationToken (Player requester , int newMemberId , int clanId ) {
105+ @ Transactional
106+ String generatePlayerInvitationToken (int newMemberId , int clanId ) {
107+ Player requester = playerService .getCurrentPlayer ();
108+
74109 Clan clan = clanRepository .findById (clanId )
75110 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
76111
77112 if (requester .getId () != clan .getLeader ().getId ()) {
78- throw new ApiException ( new Error ( ErrorCode .CLAN_NOT_LEADER , clanId ) );
113+ throw ApiException . of ( ErrorCode .CLAN_NOT_LEADER , clanId );
79114 }
80115
81- Player newMember = playerRepository .findById (newMemberId )
82- .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_GENERATE_LINK_PLAYER_NOT_FOUND , newMemberId )));
116+ Player newMember = playerService .getById (newMemberId );
83117
84118 long expire = Instant .now ()
85119 .plus (fafApiProperties .getClan ().getInviteLinkExpireDurationMinutes (), ChronoUnit .MINUTES )
@@ -92,28 +126,27 @@ String generatePlayerInvitationToken(Player requester, int newMemberId, int clan
92126 }
93127
94128 @ SneakyThrows
95- void acceptPlayerInvitationToken (String stringToken , Authentication authentication ) {
129+ @ Transactional
130+ void acceptPlayerInvitationToken (String stringToken ) {
96131 Jwt token = jwtService .decodeAndVerify (stringToken );
97132 InvitationResult invitation = objectMapper .readValue (token .getClaims (), InvitationResult .class );
98133
99134 if (invitation .isExpired ()) {
100- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE ) );
135+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_TOKEN_EXPIRE );
101136 }
102137
103138 final Integer clanId = invitation .getClan ().getId ();
104- Player player = playerService .getPlayer ( authentication );
139+ Player player = playerService .getCurrentPlayer ( );
105140 Clan clan = clanRepository .findById (clanId )
106141 .orElseThrow (() -> new ApiException (new Error (ErrorCode .CLAN_NOT_EXISTS , clanId )));
107142
108- Player newMember = playerRepository .findById (invitation .getNewMember ().getId ())
109- .orElseThrow (() -> new ProgrammingError ("ClanMember does not exist: " + invitation .getNewMember ().getId ()));
110-
143+ Player newMember = playerService .getById (invitation .getNewMember ().getId ());
111144
112145 if (player .getId () != newMember .getId ()) {
113- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER ) );
146+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_WRONG_PLAYER );
114147 }
115148 if (newMember .getClan () != null ) {
116- throw new ApiException ( new Error ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN ) );
149+ throw ApiException . of ( ErrorCode .CLAN_ACCEPT_PLAYER_IN_A_CLAN );
117150 }
118151
119152 ClanMembership membership = new ClanMembership ();
0 commit comments