public async Task CheckValidityAsync(IStringLocalizer localizer, MemCheckDbContext dbContext) { CardInputValidator.Run(this, localizer); var cards = dbContext.Cards .Include(card => card.CardLanguage) .Include(card => card.Images) .Include(card => card.TagsInCards) .ThenInclude(tag => tag.Tag) .Include(card => card.UsersWithView) .AsSingleQuery() .Where(card => card.Id == CardId); if (!await cards.AnyAsync()) { throw new RequestInputException("Unknown card id"); } var card = cards.Single(); var dataBeforeUpdate = new { CardLanguageId = card.CardLanguage.Id, card.FrontSide, card.BackSide, card.AdditionalInfo, TagIds = card.TagsInCards.Select(tag => tag.TagId), UserWithVisibilityIds = card.UsersWithView.Select(u => u.UserId), card.Images }; if ((dataBeforeUpdate.CardLanguageId == LanguageId) && (dataBeforeUpdate.FrontSide == FrontSide) && (dataBeforeUpdate.BackSide == BackSide) && (dataBeforeUpdate.AdditionalInfo == AdditionalInfo) && Enumerable.SequenceEqual(dataBeforeUpdate.TagIds.OrderBy(tagId => tagId), Tags.OrderBy(tagId => tagId)) && Enumerable.SequenceEqual(dataBeforeUpdate.UserWithVisibilityIds.OrderBy(userId => userId), UsersWithVisibility.OrderBy(userId => userId)) && SameImageLists(dataBeforeUpdate.Images)) { throw new RequestInputException(localizer["CanNotUpdateMetadataBecauseSameAsOriginal"].Value); } //to do: reducing UsersWithView must check that this will not break anybody's deck, nor prevent an author of a version of this card from viewing it }
private void CheckAtLeastOneFieldDifferent(Card card, ILocalized localizer) { var dataBeforeUpdate = new { CardLanguageId = card.CardLanguage.Id, card.FrontSide, card.BackSide, card.AdditionalInfo, TagIds = card.TagsInCards.Select(tag => tag.TagId), UserWithVisibilityIds = card.UsersWithView.Select(u => u.UserId), card.Images }; if ((dataBeforeUpdate.CardLanguageId == LanguageId) && (dataBeforeUpdate.FrontSide == FrontSide) && (dataBeforeUpdate.BackSide == BackSide) && (dataBeforeUpdate.AdditionalInfo == AdditionalInfo) && Enumerable.SequenceEqual(dataBeforeUpdate.TagIds.OrderBy(tagId => tagId), Tags.OrderBy(tagId => tagId)) && Enumerable.SequenceEqual(dataBeforeUpdate.UserWithVisibilityIds.OrderBy(userId => userId), UsersWithVisibility.OrderBy(userId => userId)) && SameImageLists(dataBeforeUpdate.Images)) { throw new RequestInputException(localizer.Get("CanNotUpdateBecauseNoDifference")); } }