public async Task <IActionResult> CreateDiplomacy([FromBody] ClanDiplomacyCreateView clanDiplomacyCreateView) { if (HttpContext.TryGetCurrentSession(out AccountSessionView accountSessionView)) { return(Ok(await ClanService.CreateDiplomacy(accountSessionView.AccountID, clanDiplomacyCreateView))); } return(Ok(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED))); }
public static async Task <ValidatedView> CreateDiplomacy(int accountId, ClanDiplomacyCreateView clanDiplomacyCreateView) { if (!clanDiplomacyCreateView.IsValid(out string message)) { return(ValidatedView.Invalid(message)); } try { ClanMemberModel clanMemberModel = await Model <ClanMemberModel> .AsQueryable().FirstOrDefault(x => x.AccountID == accountId); if (clanMemberModel != null) { ClanModel clanModel = await Model <ClanModel> .AsQueryable() .FirstOrDefault(x => x.ID == clanMemberModel.ClanID); if (clanModel == null) { return(ValidatedView.Invalid(ErrorCode.CLAN_NOT_FOUND)); } else { if (clanMemberModel.Role < ClanRole.DIPLOMAT) { return(ValidatedView.Invalid(ErrorCode.CLAN_MANAGE_DIPLOMACIES_INSUFFICIENT_RIGHTS)); } if (await Model <ClanRelationModel> .AsQueryable() .Any(x => (x.InitiatorID == clanModel.ID || x.InitiatorID == clanDiplomacyCreateView.TargetID) && (x.TargetID == clanModel.ID || x.TargetID == clanDiplomacyCreateView.TargetID))) { return(ValidatedView.Invalid(ErrorCode.CLAN_RELATION_ALREADY_EXISTS)); } await Model <ClanRelationPendingModel> .AsCollection() .DeleteManyAsync(x => (x.InitiatorID == clanModel.ID || x.InitiatorID == clanDiplomacyCreateView.TargetID) && (x.TargetID == clanModel.ID || x.TargetID == clanDiplomacyCreateView.TargetID)); if (clanDiplomacyCreateView.Type == ClanRelationType.WAR) // sofortige Wirkung { ClanRelationModel clanRelationModel = Mapper <ClanDiplomacyCreateView> .Map <ClanRelationModel>(clanDiplomacyCreateView); clanRelationModel.InitiatorID = clanModel.ID; await Model <ClanRelationModel> .AsCollection() .InsertOneAsync(clanRelationModel); } else { ClanRelationPendingModel clanRelationPendingModel = Mapper <ClanDiplomacyCreateView> .Map <ClanRelationPendingModel>(clanDiplomacyCreateView); clanRelationPendingModel.InitiatorID = clanModel.ID; await Model <ClanRelationPendingModel> .AsCollection() .InsertOneAsync(clanRelationPendingModel); } return(ValidatedView.Valid()); } } else { return(ValidatedView.Invalid(ErrorCode.CLAN_NOT_MEMBER)); } } catch (Exception e) { GameContext.Logger.LogError(e); } return(ValidatedView.Invalid(ErrorCode.OPERATION_FAILED)); }