use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.
the class ClanService method acceptPlayerInvitationToken.
@SneakyThrows
void acceptPlayerInvitationToken(String stringToken, Authentication authentication) {
Jwt token = jwtService.decodeAndVerify(stringToken);
InvitationResult invitation = objectMapper.readValue(token.getClaims(), InvitationResult.class);
if (invitation.isExpired()) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_TOKEN_EXPIRE));
}
final Integer clanId = invitation.getClan().getId();
Player player = playerService.getPlayer(authentication);
Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
Player newMember = playerRepository.findById(invitation.getNewMember().getId()).orElseThrow(() -> new ProgrammingError("ClanMember does not exist: " + invitation.getNewMember().getId()));
if (player.getId() != newMember.getId()) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_WRONG_PLAYER));
}
if (newMember.getClan() != null) {
throw new ApiException(new Error(ErrorCode.CLAN_ACCEPT_PLAYER_IN_A_CLAN));
}
ClanMembership membership = new ClanMembership();
membership.setClan(clan);
membership.setPlayer(newMember);
clanMembershipRepository.save(membership);
}
use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.
the class ClanService method generatePlayerInvitationToken.
@SneakyThrows
String generatePlayerInvitationToken(Player requester, int newMemberId, int clanId) {
Clan clan = clanRepository.findById(clanId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_NOT_EXISTS, clanId)));
if (requester.getId() != clan.getLeader().getId()) {
throw new ApiException(new Error(ErrorCode.CLAN_NOT_LEADER, clanId));
}
Player newMember = playerRepository.findById(newMemberId).orElseThrow(() -> new ApiException(new Error(ErrorCode.CLAN_GENERATE_LINK_PLAYER_NOT_FOUND, newMemberId)));
long expire = Instant.now().plus(fafApiProperties.getClan().getInviteLinkExpireDurationMinutes(), ChronoUnit.MINUTES).toEpochMilli();
InvitationResult result = new InvitationResult(expire, ClanResult.of(clan), PlayerResult.of(newMember));
return jwtService.sign(result);
}
use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.
the class ClansController method createClan.
// This request cannot be handled by JSON API because we must simultaneously create two resources (a,b)
// a: the new clan with the leader membership, b: the leader membership with the new clan
@ApiOperation("Create a clan with correct leader, founder and clan membership")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success with JSON { id: ?, type: 'clan'}"), @ApiResponse(code = 400, message = "Bad Request") })
@RequestMapping(path = "/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasRole('ROLE_USER')")
@Transactional
public Map<String, Serializable> createClan(@RequestParam(value = "name") String name, @RequestParam(value = "tag") String tag, @RequestParam(value = "description", required = false) String description, Authentication authentication) throws IOException {
Player player = playerService.getPlayer(authentication);
Clan clan = clanService.create(name, tag, description, player);
return ImmutableMap.of("id", clan.getId(), "type", "clan");
}
use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.
the class ClanControllerTest method meDataWithClan.
@Test
@WithUserDetails(AUTH_CLAN_MEMBER)
public void meDataWithClan() throws Exception {
Player player = getPlayer();
Clan clan = clanRepository.getOne(1);
mockMvc.perform(get("/clans/me/")).andExpect(status().isOk()).andExpect(jsonPath("$.player.id", is(player.getId()))).andExpect(jsonPath("$.player.login", is(player.getLogin()))).andExpect(jsonPath("$.clan.id", is(clan.getId()))).andExpect(jsonPath("$.clan.tag", is(clan.getTag()))).andExpect(jsonPath("$.clan.name", is(clan.getName())));
}
use of com.faforever.api.data.domain.Clan in project faf-java-api by FAForever.
the class ClanService method create.
@SneakyThrows
Clan create(String name, String tag, String description, Player creator) {
if (!creator.getClanMemberships().isEmpty()) {
throw new ApiException(new Error(ErrorCode.CLAN_CREATE_CREATOR_IS_IN_A_CLAN));
}
if (clanRepository.findOneByName(name).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_NAME_EXISTS, name));
}
if (clanRepository.findOneByTag(tag).isPresent()) {
throw new ApiException(new Error(ErrorCode.CLAN_TAG_EXISTS, name));
}
Clan clan = new Clan();
clan.setName(name);
clan.setTag(tag);
clan.setDescription(description);
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
clanRepository.save(clan);
return clan;
}
Aggregations