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"]); } }
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, "?")); }
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"); } }
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); }
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 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 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()); }
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"); } }
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}"); } }
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); }
public async Task <IEnumerable <ResultCard> > RunAsync(Request request) { request.CheckValidity(); QueryValidationHelper.CheckUserIsOwnerOfDeck(dbContext, request.CurrentUserId, request.DeckId); var heapingAlgorithm = await GetHeapingAlgorithmAsync(request.DeckId); var userNames = dbContext.Users.AsNoTracking().Select(u => new { u.Id, u.UserName }).ToImmutableDictionary(u => u.Id, u => u.UserName); var imagesDetails = GetAllImagesDetails(); var tagNames = dbContext.Tags.AsNoTracking().Select(t => new { t.Id, t.Name }).ToImmutableDictionary(t => t.Id, t => t.Name); if (request.LearnModeIsUnknown) { return(await GetUnknownCardsAsync(request.CurrentUserId, request.DeckId, request.ExcludedCardIds, request.ExcludedTagIds, heapingAlgorithm, userNames, imagesDetails, tagNames, request.CardsToDownload)); } return(GetCardsToRepeat(request.CurrentUserId, request.DeckId, request.ExcludedCardIds, request.ExcludedTagIds, heapingAlgorithm, userNames, imagesDetails, tagNames, request.CardsToDownload)); }
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}"); } }
public async Task <Result> RunAsync(Request request) { request.CheckValidity(); QueryValidationHelper.CheckUserIsOwnerOfDeck(dbContext, request.CurrentUserId, request.DeckId); var cardsInDeck = dbContext.CardsInDecks.Where(cardInDeck => cardInDeck.CardId == request.CardId && cardInDeck.DeckId == request.DeckId); if (cardsInDeck.Count() != 1) { throw new RequestInputException($"There are {cardsInDeck.Count()} cards with the given id"); } var cardInDeck = cardsInDeck.Include(cardInDeck => cardInDeck.Card).Include(cardInDeck => cardInDeck.Deck).Single(); dbContext.CardsInDecks.Remove(cardInDeck); await dbContext.SaveChangesAsync(); return(new Result(cardInDeck.Card.FrontSide, cardInDeck.Deck.Description)); }
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); }
public async Task RunAsync(Request request) { request.CheckValidity(); QueryValidationHelper.CheckUserIsOwnerOfDeck(dbContext, request.UserId, request.DeckId); var cards = dbContext.CardsInDecks.Where(card => card.DeckId.Equals(request.DeckId) && request.CardIds.Any(cardId => cardId == card.CardId)); await cards.ForEachAsync(card => { if (card.BiggestHeapReached < request.TargetHeapId) { card.BiggestHeapReached = request.TargetHeapId; } }); await cards.ForEachAsync(card => card.CurrentHeap = request.TargetHeapId); if (request.TargetHeapId == 0) { await cards.ForEachAsync(card => card.NbTimesInNotLearnedHeap++); } await dbContext.SaveChangesAsync(); }
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"); } }
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}"); } }
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); }