Esempio n. 1
0
        public async Task <AvatarBundle> EditBundleAsync(AvatarBundle bundle,
                                                         List <int> itemIds)
        {
            VerifyManagementPermission();

            var currentBundle = await _avatarBundleRepository.GetByIdAsync(bundle.Id, false);

            if (currentBundle.HasBeenAwarded)
            {
                throw new GraException($"This bundle has been awarded to a participant and can no longer be edited. ");
            }

            var items = await _avatarItemRepository.GetByIdsAsync(itemIds);

            if (items.Where(_ => _.Unlockable != currentBundle.CanBeUnlocked).Any())
            {
                throw new GraException($"Not all items are {(bundle.CanBeUnlocked ? "Unlockable" : "Available")}.");
            }

            if (currentBundle.CanBeUnlocked == false &&
                items.GroupBy(_ => _.AvatarLayerId).Where(_ => _.Skip(1).Any()).Any())
            {
                throw new GraException($"Default bundles cannot have multiple items per layer.");
            }

            currentBundle.Name = bundle.Name;
            await _avatarBundleRepository.UpdateSaveAsync(GetClaimId(ClaimType.UserId),
                                                          currentBundle);

            var currentItemIds = currentBundle.AvatarItems.Select(_ => _.Id).ToList();
            var itemsToRemove  = currentItemIds.Except(itemIds).ToList();
            var itemsToAdd     = itemIds.Except(currentItemIds).ToList();

            await _avatarBundleRepository.RemoveItemsAsync(currentBundle.Id, itemsToRemove);

            await _avatarBundleRepository.AddItemsAsync(currentBundle.Id, itemsToAdd);

            return(currentBundle);
        }
Esempio n. 2
0
        private async Task ValidateTriggerAsync(Trigger trigger)
        {
            if (trigger.LimitToSystemId.HasValue)
            {
                if (!(await _systemRepository.ValidateAsync(
                          trigger.LimitToSystemId.Value, trigger.SiteId)))
                {
                    throw new GraException("Invalid System selection.");
                }
                if (trigger.LimitToBranchId.HasValue && !(await _branchRepository.ValidateAsync(
                                                              trigger.LimitToBranchId.Value, trigger.LimitToSystemId.Value)))
                {
                    throw new GraException("Invalid Branch selection.");
                }
            }
            else if (trigger.LimitToBranchId.HasValue &&
                     !await _branchRepository.ValidateBySiteAsync(
                         trigger.LimitToBranchId.Value, trigger.SiteId))
            {
                throw new GraException("Invalid Branch selection.");
            }

            if (trigger.LimitToProgramId.HasValue && !(await _programRepository.ValidateAsync(
                                                           trigger.LimitToProgramId.Value, trigger.SiteId)))
            {
                throw new GraException("Invalid Program selection.");
            }

            if (!string.IsNullOrWhiteSpace(trigger.SecretCode))
            {
                if (await _triggerRepository.CodeExistsAsync(
                        trigger.SiteId, trigger.SecretCode, trigger.Id))
                {
                    throw new GraException("Secret code already in use.");
                }
            }
            else if (trigger.ItemsRequired > 0)
            {
                // To Do
            }
            else if (!(trigger.Points > 0))
            {
                throw new GraException("No criteria selected.");
            }

            if (trigger.AwardAvatarBundleId.HasValue)
            {
                var bundle = await _avatarBundleRepository
                             .GetByIdAsync(trigger.AwardAvatarBundleId.Value, false);

                if (bundle?.CanBeUnlocked != true)
                {
                    throw new GraException("Invalid Avatar Bundle selection.");
                }
            }

            var maxPointLimit = await GetMaximumAllowedPointsAsync(GetCurrentSiteId());

            if (maxPointLimit.HasValue && !HasPermission(Permission.IgnorePointLimits))
            {
                var currentTrigger = await _triggerRepository.GetByIdAsync(trigger.Id);

                if (currentTrigger?.AwardPoints > maxPointLimit)
                {
                    throw new GraException("Permission denied.");
                }
                if (trigger.AwardPoints > maxPointLimit)
                {
                    throw new GraException($"A trigger may award a maximum of {maxPointLimit} points.");
                }
            }
        }
        private async Task ValidateTriggerAsync(Trigger trigger)
        {
            if (trigger.LimitToSystemId.HasValue)
            {
                if (!(await _systemRepository.ValidateAsync(
                          trigger.LimitToSystemId.Value, trigger.SiteId)))
                {
                    throw new GraException("Invalid System selection.");
                }
                if (trigger.LimitToBranchId.HasValue)
                {
                    if (!(await _branchRepository.ValidateAsync(
                              trigger.LimitToBranchId.Value, trigger.LimitToSystemId.Value)))
                    {
                        throw new GraException("Invalid Branch selection.");
                    }
                }
            }
            else if (trigger.LimitToBranchId.HasValue)
            {
                if (!(await _branchRepository.ValidateBySiteAsync(
                          trigger.LimitToBranchId.Value, trigger.SiteId)))
                {
                    throw new GraException("Invalid Branch selection.");
                }
            }

            if (trigger.LimitToProgramId.HasValue)
            {
                if (!(await _programRepository.ValidateAsync(
                          trigger.LimitToProgramId.Value, trigger.SiteId)))
                {
                    throw new GraException("Invalid Program selection.");
                }
            }

            if (!string.IsNullOrWhiteSpace(trigger.SecretCode))
            {
                if (await _triggerRepository.CodeExistsAsync(
                        trigger.SiteId, trigger.SecretCode, trigger.Id))
                {
                    throw new GraException("Secret code already in use.");
                }
            }
            else if (trigger.ItemsRequired > 0)
            {
                // To Do
            }
            else if (!(trigger.Points > 0))
            {
                throw new GraException("No criteria selected.");
            }

            if (trigger.AwardAvatarBundleId.HasValue)
            {
                var bundle = await _avatarBundleRepository
                             .GetByIdAsync(trigger.AwardAvatarBundleId.Value, false);

                if (bundle == null || bundle.CanBeUnlocked == false)
                {
                    throw new GraException("Invalid Avatar Bundle selection.");
                }
            }
        }