Exemple #1
0
            public async Task CheckValidityAsync(IStringLocalizer localizer, MemCheckDbContext dbContext)
            {
                if (DeletionDescription != DeletionDescription.Trim())
                {
                    throw new InvalidOperationException("Invalid name: not trimmed");
                }
                if (DeletionDescription.Length < minDescriptionLength || DeletionDescription.Length > maxDescriptionLength)
                {
                    throw new RequestInputException(localizer["InvalidDeletionDescriptionLength"] + $" {DeletionDescription.Length}" + localizer["MustBeBetween"] + $" {minDescriptionLength} " + localizer["And"] + $" {maxDescriptionLength}");
                }

                if (QueryValidationHelper.IsReservedGuid(User.Id))
                {
                    throw new RequestInputException("InvalidUserId");
                }
                if (QueryValidationHelper.IsReservedGuid(ImageId))
                {
                    throw new RequestInputException("InvalidImageId");
                }

                var images = dbContext.Images.Where(img => img.Id == ImageId);

                if (!await images.AnyAsync())
                {
                    throw new RequestInputException(localizer["UnknownImage"].Value);
                }
                var cardCounts = images.Select(img => img.Cards.Count());
                var cardCount  = await cardCounts.SingleAsync();

                if (cardCount != 0)
                {
                    throw new RequestInputException(localizer["ImageUsedInCardsPart1"] + ' ' + cardCount + ' ' + localizer["ImageUsedInCardsPart2"]);
                }
            }
Exemple #2
0
        public async Task <Result> RunAsync(Guid imageId)
        {
            if (QueryValidationHelper.IsReservedGuid(imageId))
            {
                throw new RequestInputException("Invalid image id");
            }
            var images = dbContext.Images.Where(img => img.Id == imageId);

            return(await ResultFromSearchAsync(images, "?"));
        }
Exemple #3
0
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(UserId))
     {
         throw new RequestInputException("Invalid user id");
     }
     if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId)))
     {
         throw new RequestInputException($"Invalid card id");
     }
 }
Exemple #4
0
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(ImageId))
     {
         throw new RequestInputException($"Invalid image id {ImageId}");
     }
     if (Size < 1 || Size > 3)
     {
         throw new RequestInputException($"Invalid image size {Size}");
     }
 }
 public async Task CheckValidityAsync(MemCheckDbContext dbContext)
 {
     if (QueryValidationHelper.IsReservedGuid(CurrentUserId))
     {
         throw new RequestInputException($"Invalid user id '{CurrentUserId}'");
     }
     if (QueryValidationHelper.IsReservedGuid(CardId))
     {
         throw new RequestInputException($"Invalid card id '{CardId}'");
     }
     await QueryValidationHelper.CheckUserIsAllowedToViewCardAsync(dbContext, CurrentUserId, CardId);
 }
        public async Task <IEnumerable <ResultImageVersion> > RunAsync(Guid imageId)
        {
            if (QueryValidationHelper.IsReservedGuid(imageId))
            {
                throw new RequestInputException("Invalid image id");
            }
            var images = dbContext.Images.Where(img => img.Id == imageId);

            if (!await images.AnyAsync())
            {
                throw new RequestInputException(localizer["UnknownImage"].Value);
            }

            var currentVersion = await images.Include(img => img.PreviousVersion)
                                 .Select(img => new ImageVersionFromDb(
                                             img.Id,
                                             img.PreviousVersion == null ? (Guid?)null : img.PreviousVersion.Id,
                                             img.LastChangeUtcDate,
                                             img.Owner,
                                             img.Name,
                                             img.Description,
                                             img.Source,
                                             img.VersionDescription)
                                         ).SingleAsync();

            var allPreviousVersions = dbContext.ImagePreviousVersions.Where(img => img.Image == imageId)
                                      .Select(img => new ImageVersionFromDb(
                                                  img.Id,
                                                  img.PreviousVersion == null ? (Guid?)null : img.PreviousVersion.Id,
                                                  img.VersionUtcDate,
                                                  img.Owner,
                                                  img.Name,
                                                  img.Description,
                                                  img.Source,
                                                  img.VersionDescription)
                                              );

            var versionDico = allPreviousVersions.ToImmutableDictionary(ver => ver.Id, ver => ver);

            var result           = new List <ResultImageVersion>();
            var iterationVersion = currentVersion.PreviousVersion == null ? null : versionDico[currentVersion.PreviousVersion.Value];

            result.Add(new ResultImageVersion(currentVersion, iterationVersion));

            while (iterationVersion != null)
            {
                var previousVersion = iterationVersion.PreviousVersion == null ? null : versionDico[iterationVersion.PreviousVersion.Value];
                result.Add(new ResultImageVersion(iterationVersion, previousVersion));
                iterationVersion = previousVersion;
            }
            return(result);
        }
Exemple #7
0
            public async Task CheckValidityAsync(MemCheckDbContext dbContext, IStringLocalizer localizer)
            {
                if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId)))
                {
                    throw new RequestInputException($"Invalid card id");
                }

                foreach (var cardId in CardIds)
                {
                    await CheckUsersWithCardInADeckAsync(cardId, dbContext, localizer);
                    await CheckCardVersionsCreatorsAsync(cardId, dbContext, localizer);
                }
            }
        public IEnumerable <ResultModel> Run(Guid userId)
        {
            if (QueryValidationHelper.IsReservedGuid(userId))
            {
                throw new RequestInputException($"Invalid user id {userId}");
            }

            var userDecks = dbContext.Decks.AsNoTracking().Where(deck => deck.Owner.Id == userId).Select(deck => new { deckId = deck.Id, deckDescription = deck.Description }).ToList();

            var result = userDecks.Select(deck =>
                                          new ResultModel(deck.deckId, deck.deckDescription, new GetTagsOfDeck(dbContext).Run(deck.deckId).Select(tag => new ResultTagModel(tag.TagId, tag.TagName)))
                                          );

            return(result.ToList());
        }
Exemple #9
0
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(CurrentUserId))
     {
         throw new RequestInputException($"Invalid user id '{CurrentUserId}'");
     }
     if (QueryValidationHelper.IsReservedGuid(DeckId))
     {
         throw new RequestInputException($"Invalid deck id '{DeckId}'");
     }
     if (QueryValidationHelper.IsReservedGuid(CardId))
     {
         throw new RequestInputException($"Invalid card id '{DeckId}'");
     }
 }
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(DeckId))
     {
         throw new RequestInputException($"Invalid DeckId '{DeckId}'");
     }
     if (TargetHeapId < 0 || TargetHeapId > MoveCardToHeap.MaxTargetHeapId)
     {
         throw new RequestInputException($"Invalid target heap {TargetHeapId}");
     }
     if (CardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId)))
     {
         throw new RequestInputException($"Invalid card id");
     }
 }
Exemple #11
0
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(User.Id))
     {
         throw new RequestInputException("Invalid user id");
     }
     if (QueryValidationHelper.IsReservedGuid(CardId))
     {
         throw new RequestInputException("Invalid card id");
     }
     if (Rating < 1 || Rating > 5)
     {
         throw new RequestInputException($"Invalid rating: {Rating}");
     }
 }
Exemple #12
0
 public async Task CheckValidityAsync(MemCheckDbContext dbContext)
 {
     if (QueryValidationHelper.IsReservedGuid(DeckId))
     {
         throw new RequestInputException($"Invalid DeckId '{DeckId}'");
     }
     if (QueryValidationHelper.IsReservedGuid(CardId))
     {
         throw new RequestInputException($"Invalid DeckId '{DeckId}'");
     }
     if (TargetHeap < minHeapId || TargetHeap > MaxTargetHeapId)
     {
         throw new RequestInputException($"Invalid target heap {TargetHeap}");
     }
     await QueryValidationHelper.CheckUserIsOwnerOfDeckAsync(dbContext, UserId, DeckId);
 }
Exemple #13
0
 public async Task CheckValidityAsync(UserManager <MemCheckUser> userManager)
 {
     if (QueryValidationHelper.IsReservedGuid(CurrentUser.Id))
     {
         throw new RequestInputException($"Invalid user id '{CurrentUser.Id}'");
     }
     if (PageSize < 0 || PageSize > 100)
     {
         throw new RequestInputException($"Invalid page size: {PageSize}");
     }
     if (PageNo < 0)
     {
         throw new RequestInputException($"Invalid page index: {PageNo}");
     }
     if (!await userManager.IsInRoleAsync(CurrentUser, "Admin"))
     {
         throw new SecurityException($"User not admin: {CurrentUser.UserName}");
     }
 }
Exemple #14
0
 public void CheckValidity(MemCheckDbContext dbContext)
 {
     if (QueryValidationHelper.IsReservedGuid(User.Id))
     {
         throw new RequestInputException("Invalid user");
     }
     if (!HeapingAlgorithms.Instance.Ids.Any(algoId => algoId == HeapingAlgorithmId))
     {
         throw new RequestInputException($"Invalid algo id '{HeapingAlgorithmId}'");
     }
     if (Description.Length < minDescriptionLength || Description.Length > maxDescriptionLength)
     {
         throw new InvalidOperationException($"Invalid desciption '{Description}' (length must be between {minDescriptionLength} and {maxDescriptionLength})");
     }
     if (dbContext.Decks.Where(deck => (deck.Owner.Id == User.Id) && EF.Functions.Like(deck.Description, $"{Description}")).Any())
     {
         throw new InvalidOperationException($"A deck with description '{Description}' already exists (this is case insensitive)");
     }
 }
        public async Task <IEnumerable <ResultModel> > RunAsync(Guid userId)
        {
            if (QueryValidationHelper.IsReservedGuid(userId))
            {
                throw new RequestInputException($"Invalid user id {userId}");
            }

            var decks = dbContext.Decks.AsNoTracking().Where(deck => deck.Owner.Id == userId).OrderBy(deck => deck.Description).Select(deck => new { deck.Id, deck.Description }).ToList();

            var result = new List <ResultModel>();

            foreach (var deck in decks)
            {
                var heaps = await dbContext.CardsInDecks.AsNoTracking().Where(cardInDeck => cardInDeck.DeckId == deck.Id).Select(cardInDeck => cardInDeck.CurrentHeap).Distinct().ToListAsync();

                var tags = new GetTagsOfDeck(dbContext).Run(deck.Id);
                result.Add(new ResultModel(deck.Id, deck.Description, heaps, tags.Select(tag => new ResultTagModel(tag.TagId, tag.TagName))));
            }

            return(result);
        }
Exemple #16
0
 public async Task CheckValidityAsync(MemCheckDbContext dbContext)
 {
     if (!CardIds.Any())
     {
         throw new RequestInputException("No card to add label to");
     }
     if (QueryValidationHelper.IsReservedGuid(VersionCreator.Id))
     {
         throw new RequestInputException("Invalid user id");
     }
     if (QueryValidationHelper.IsReservedGuid(TagId))
     {
         throw new RequestInputException("Reserved tag id");
     }
     foreach (var cardId in CardIds)
     {
         await QueryValidationHelper.CheckUserIsAllowedToViewCardAsync(dbContext, VersionCreator.Id, cardId);
     }
     if (!dbContext.Tags.Where(tag => tag.Id == TagId).Any())
     {
         throw new RequestInputException("Invalid tag id");
     }
 }
Exemple #17
0
 public void CheckValidity()
 {
     if (QueryValidationHelper.IsReservedGuid(CurrentUserId))
     {
         throw new RequestInputException($"Invalid user id '{CurrentUserId}'");
     }
     if (QueryValidationHelper.IsReservedGuid(DeckId))
     {
         throw new RequestInputException($"Invalid deck id '{DeckId}'");
     }
     if (ExcludedCardIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId)))
     {
         throw new RequestInputException($"Invalid card id");
     }
     if (ExcludedTagIds.Any(cardId => QueryValidationHelper.IsReservedGuid(cardId)))
     {
         throw new RequestInputException($"Invalid tag id");
     }
     if (CardsToDownload < 1 || CardsToDownload > 100)
     {
         throw new RequestInputException($"Invalid CardsToDownload: {CardsToDownload}");
     }
 }
Exemple #18
0
        public async Task <IEnumerable <ResultCardVersion> > RunAsync(Guid cardId, Guid userId)
        {
            if (QueryValidationHelper.IsReservedGuid(cardId))
            {
                throw new RequestInputException("Invalid card id");
            }
            if (QueryValidationHelper.IsReservedGuid(userId))
            {
                throw new RequestInputException("Invalid user id");
            }
            await QueryValidationHelper.CheckUserIsAllowedToViewCardAsync(dbContext, userId, cardId);

            var cards = dbContext.Cards.Where(card => card.Id == cardId);

            if (!await cards.AnyAsync())
            {
                throw new RequestInputException(localizer["UnknownCard"].Value);
            }

            var currentVersion = await cards.Include(card => card.PreviousVersion)
                                 .Select(card => new CardVersionFromDb(
                                             card.Id,
                                             card.PreviousVersion == null ? (Guid?)null : card.PreviousVersion.Id,
                                             card.VersionUtcDate,
                                             card.VersionCreator,
                                             card.VersionDescription,
                                             card.CardLanguage.Id,
                                             card.FrontSide,
                                             card.BackSide,
                                             card.AdditionalInfo,
                                             card.TagsInCards.Select(tag => tag.TagId),
                                             card.UsersWithView.Select(user => user.UserId),
                                             card.Images.Where(img => img.CardSide == 1).Select(img => img.ImageId),
                                             card.Images.Where(img => img.CardSide == 2).Select(img => img.ImageId),
                                             card.Images.Where(img => img.CardSide == 3).Select(img => img.ImageId)
                                             )
                                         ).SingleAsync();

            var allPreviousVersions = dbContext.CardPreviousVersions.Where(card => card.Card == cardId)
                                      .Select(card => new CardVersionFromDb(
                                                  card.Id,
                                                  card.PreviousVersion == null ? (Guid?)null : card.PreviousVersion.Id,
                                                  card.VersionUtcDate,
                                                  card.VersionCreator,
                                                  card.VersionDescription,
                                                  card.CardLanguage.Id,
                                                  card.FrontSide,
                                                  card.BackSide,
                                                  card.AdditionalInfo,
                                                  card.Tags.Select(tag => tag.TagId),
                                                  card.UsersWithView.Select(u => u.AllowedUserId),
                                                  card.Images.Where(img => img.CardSide == 1).Select(img => img.ImageId),
                                                  card.Images.Where(img => img.CardSide == 2).Select(img => img.ImageId),
                                                  card.Images.Where(img => img.CardSide == 3).Select(img => img.ImageId)
                                                  )
                                              );

            var versionDico = allPreviousVersions.ToImmutableDictionary(ver => ver.Id, ver => ver);

            var result           = new List <ResultCardVersion>();
            var iterationVersion = currentVersion.PreviousVersion == null ? null : versionDico[currentVersion.PreviousVersion.Value];

            result.Add(new ResultCardVersion(currentVersion, iterationVersion));

            while (iterationVersion != null)
            {
                var previousVersion = iterationVersion.PreviousVersion == null ? null : versionDico[iterationVersion.PreviousVersion.Value];
                result.Add(new ResultCardVersion(iterationVersion, previousVersion));
                iterationVersion = previousVersion;
            }
            return(result);
        }