Skip to content

Commit

Permalink
Refactor unit tests and original clan create
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Aug 19, 2020
1 parent 1f7e66c commit 2879712
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 271 deletions.
28 changes: 4 additions & 24 deletions src/main/java/com/faforever/api/clan/ClanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;

@Service
Expand Down Expand Up @@ -67,36 +66,17 @@ public void preCreate(Clan clan) {
@SneakyThrows
@Transactional
@Deprecated
// use preCreate instead
// use POST via Elide instead
Clan create(String name, String tag, String description) {
Player creator = playerService.getCurrentPlayer();

if (creator.getClanMembership() != null) {
throw ApiException.of(ErrorCode.CLAN_CREATE_FOUNDER_IS_IN_A_CLAN);
}
if (clanRepository.findOneByName(name).isPresent()) {
throw ApiException.of(ErrorCode.CLAN_NAME_EXISTS, name);
}
if (clanRepository.findOneByTag(tag).isPresent()) {
throw ApiException.of(ErrorCode.CLAN_TAG_EXISTS, tag);
}

Clan clan = new Clan();
clan.setName(name);
clan.setTag(tag);
clan.setDescription(description);
clan.setRequiresInvitation(true);
clan.setFounder(playerService.getCurrentPlayer());
clan.setLeader(playerService.getCurrentPlayer());

clan.setFounder(creator);
clan.setLeader(creator);

ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(creator);

clan.setMemberships(Collections.singletonList(membership));

// clan membership is saved over cascading, otherwise validation will fail
// validation is done at preCreate() called by ClanListener
clanRepository.save(clan);
return clan;
}
Expand Down
Loading

0 comments on commit 2879712

Please sign in to comment.