private async Task UpdateTagsAsync(Request request, Card card) { var tagsBeforeUpdate = dbContext.TagsInCards.Where(tag => tag.CardId == request.CardId); dbContext.TagsInCards.RemoveRange(tagsBeforeUpdate); var tagsInCards = new List <TagInCard>(); foreach (var tagToAdd in request.Tags) { var tagFromDb = dbContext.Tags.Where(t => t.Id == tagToAdd).Single(); var tagInCard = new TagInCard() { TagId = tagFromDb.Id, Tag = tagFromDb, CardId = card.Id }; await dbContext.TagsInCards.AddAsync(tagInCard); tagsInCards.Add(tagInCard); } card.TagsInCards = tagsInCards; }
protected override async Task <ResultWithMetrologyProperties <Result> > DoRunAsync(Request request) { var language = DbContext.CardLanguages.Where(language => language.Id == request.LanguageId).Single(); var versionCreator = DbContext.Users.Where(user => user.Id == request.VersionCreatorId).Single(); var card = new Card() { FrontSide = request.FrontSide, BackSide = request.BackSide, AdditionalInfo = request.AdditionalInfo, CardLanguage = language, VersionCreator = versionCreator, InitialCreationUtcDate = DateTime.Now.ToUniversalTime(), VersionUtcDate = DateTime.Now.ToUniversalTime(), VersionDescription = request.VersionDescription }; DbContext.Cards.Add(card); var usersWithView = new List <UserWithViewOnCard>(); foreach (var userFromRequestId in request.UsersWithVisibility) { var userFromDb = DbContext.Users.Where(u => u.Id == userFromRequestId).Single(); var userWithView = new UserWithViewOnCard() { UserId = userFromDb.Id, User = userFromDb, CardId = card.Id, Card = card }; DbContext.UsersWithViewOnCards.Add(userWithView); usersWithView.Add(userWithView); } card.UsersWithView = usersWithView; var tagsInCards = new List <TagInCard>(); foreach (var tagToAdd in request.Tags) { var tagFromDb = DbContext.Tags.Where(t => t.Id == tagToAdd).Single(); var tagInCard = new TagInCard() { TagId = tagFromDb.Id, Tag = tagFromDb, CardId = card.Id }; DbContext.TagsInCards.Add(tagInCard); tagsInCards.Add(tagInCard); } card.TagsInCards = tagsInCards; var cardImageList = new List <ImageInCard>(); foreach (var image in request.FrontSideImageList) { AddImage(card.Id, image, 1, cardImageList); } foreach (var image in request.BackSideImageList) { AddImage(card.Id, image, 2, cardImageList); } foreach (var image in request.AdditionalInfoImageList) { AddImage(card.Id, image, 3, cardImageList); } card.Images = cardImageList; if (versionCreator.SubscribeToCardOnEdit) { AddCardSubscriptions.CreateSubscription(DbContext, versionCreator.Id, card.Id, card.VersionUtcDate, CardNotificationSubscription.CardNotificationRegistrationMethod_VersionCreation); } await DbContext.SaveChangesAsync(); return(new ResultWithMetrologyProperties <Result>(new Result(card.Id), ("CardId", card.Id.ToString()), ("Public", (!request.UsersWithVisibility.Any()).ToString()))); }
public async Task <Guid> RunAsync(Request request, IStringLocalizer localizer) { CardInputValidator.Run(request, localizer); var language = dbContext.CardLanguages.Where(language => language.Id == request.LanguageId).Single(); var card = new Card() { FrontSide = request.FrontSide, BackSide = request.BackSide, AdditionalInfo = request.AdditionalInfo, CardLanguage = language, VersionCreator = request.VersionCreator, InitialCreationUtcDate = DateTime.Now.ToUniversalTime(), VersionUtcDate = DateTime.Now.ToUniversalTime(), VersionDescription = request.VersionDescription }; dbContext.Cards.Add(card); var usersWithView = new List <UserWithViewOnCard>(); foreach (var userFromRequestId in request.UsersWithVisibility) { var userFromDb = dbContext.Users.Where(u => u.Id == userFromRequestId).Single(); var userWithView = new UserWithViewOnCard() { UserId = userFromDb.Id, User = userFromDb, CardId = card.Id, Card = card }; dbContext.UsersWithViewOnCards.Add(userWithView); usersWithView.Add(userWithView); } card.UsersWithView = usersWithView; var tagsInCards = new List <TagInCard>(); foreach (var tagToAdd in request.Tags) { var tagFromDb = dbContext.Tags.Where(t => t.Id == tagToAdd).Single(); var tagInCard = new TagInCard() { TagId = tagFromDb.Id, Tag = tagFromDb, CardId = card.Id }; dbContext.TagsInCards.Add(tagInCard); tagsInCards.Add(tagInCard); } card.TagsInCards = tagsInCards; var cardImageList = new List <ImageInCard>(); foreach (var image in request.FrontSideImageList) { AddImage(card.Id, image, 1, cardImageList); } foreach (var image in request.BackSideImageList) { AddImage(card.Id, image, 2, cardImageList); } foreach (var image in request.AdditionalInfoImageList) { AddImage(card.Id, image, 3, cardImageList); } card.Images = cardImageList; await dbContext.SaveChangesAsync(); return(card.Id); }
public static async Task <Card> CreateAsync(DbContextOptions <MemCheckDbContext> testDB, Guid versionCreatorId, DateTime?versionDate = null, IEnumerable <Guid>?userWithViewIds = null, Guid?language = null, IEnumerable <Guid>?tagIds = null, string?frontSide = null, string?backSide = null, string?additionalInfo = null, IEnumerable <Guid>?frontSideImages = null, IEnumerable <Guid>?additionalSideImages = null, string?versionDescription = null) { //userWithViewIds null means public card using var dbContext = new MemCheckDbContext(testDB); var creator = await dbContext.Users.Where(u => u.Id == versionCreatorId).SingleAsync(); var result = new Card { VersionCreator = creator, FrontSide = frontSide ?? RandomHelper.String(), BackSide = backSide ?? RandomHelper.String(), AdditionalInfo = additionalInfo ?? RandomHelper.String(), VersionDescription = versionDescription ?? RandomHelper.String(), VersionType = CardVersionType.Creation }; if (language == null) { language = await CardLanguagHelper.CreateAsync(testDB); } result.CardLanguage = await dbContext.CardLanguages.SingleAsync(l => l.Id == language); if (versionDate != null) { result.InitialCreationUtcDate = versionDate.Value; result.VersionUtcDate = versionDate.Value; } dbContext.Cards.Add(result); var usersWithView = new List <UserWithViewOnCard>(); if (userWithViewIds != null && userWithViewIds.Any()) { Assert.IsTrue(userWithViewIds.Any(id => id == versionCreatorId), "Version creator must be allowed to view"); foreach (var userWithViewId in userWithViewIds) { var userWithView = new UserWithViewOnCard { CardId = result.Id, UserId = userWithViewId }; dbContext.UsersWithViewOnCards.Add(userWithView); usersWithView.Add(userWithView); } } result.UsersWithView = usersWithView; var tags = new List <TagInCard>(); if (tagIds != null) { foreach (var tagId in tagIds) { var tagInCard = new TagInCard { CardId = result.Id, TagId = tagId }; dbContext.TagsInCards.Add(tagInCard); tags.Add(tagInCard); } } result.TagsInCards = tags; var images = new List <ImageInCard>(); if (frontSideImages != null) { foreach (var frontSideImage in frontSideImages) { var img = new ImageInCard() { ImageId = frontSideImage, CardId = result.Id, CardSide = ImageInCard.FrontSide }; dbContext.ImagesInCards.Add(img); images.Add(img); } } if (additionalSideImages != null) { foreach (var additionalSideImage in additionalSideImages) { var img = new ImageInCard() { ImageId = additionalSideImage, CardId = result.Id, CardSide = ImageInCard.AdditionalInfo }; dbContext.ImagesInCards.Add(img); images.Add(img); } } result.Images = images; await dbContext.SaveChangesAsync(); return(result); }