public async ValueTask Create(MangasContext context) { var type = await context.Types .FirstAsync(t => t.Id == Type.Id); var tagsIds = Tags.Select(t => t.Id).ToList(); var tags = await context.Tags .Where(tag => tagsIds.Contains(tag.Id)) .ToListAsync(); var translatorsIds = Translators.Select(t => t.Id).ToList(); var translators = await context.Translators .Where(translator => translatorsIds.Contains(translator.Id)) .ToListAsync(); var authorsIds = Authors.Select(a => a.Id).ToList(); var authors = await context.Authors .Where(author => authorsIds.Contains(author.Id)) .ToListAsync(); var manga = new Manga { Title = Title, Description = Description, Image = Image, Type = type }; await context.MangaTags .AddRangeAsync(tags.Select(tag => new MangaTag(tag, manga))); await context.MangaTranslators .AddRangeAsync(translators.Select(translator => new MangaTranslator(translator, manga))); await context.MangaAuthors .AddRangeAsync(authors.Select(author => new MangaAuthor(author, manga))); await context.Mangas.AddAsync(manga); }
public async ValueTask Update(MangasContext context, int mangaId) { var manga = await context.Mangas .Include(m => m.Tags) .Include(m => m.Translators) .Include(m => m.Authors) .FirstAsync(m => m.Id == mangaId); if (Title != null) { manga.Title = Title; } if (Description != null) { manga.Description = Description; } if (Type != null) { var type = await context.Types.FirstAsync(t => t.Id == Type.Id); manga.Type = type; } if (Tags != null) { var modelTagIds = Tags.Select(t => t.Id).ToList(); var mangaTagIds = manga.Tags.Select(t => t.Tag.Id).ToList(); var mangaTagsToRemove = mangaTagIds.Except(modelTagIds) .Select(tagId => manga.Tags.First(t => t.Tag.Id == tagId)) .ToList(); var mangaTagsToAdd = modelTagIds.Except(mangaTagIds) .Select(tagId => context.Tags.First(t => t.Id == tagId)) .Select(tag => new MangaTag(tag, manga)) .ToList(); context.MangaTags.RemoveRange(mangaTagsToRemove); await context.MangaTags.AddRangeAsync(mangaTagsToAdd); } if (Translators != null) { var modelTranslatorIds = Translators.Select(t => t.Id).ToList(); var mangaTranslatorIds = manga.Translators.Select(t => t.Translator.Id).ToList(); var mangaTranslatorsToRemove = mangaTranslatorIds.Except(modelTranslatorIds) .Select(translatorId => manga.Translators.First(t => t.Translator.Id == translatorId)) .ToList(); var mangaTranslatorsToAdd = modelTranslatorIds.Except(mangaTranslatorIds) .Select(translatorId => context.Translators.First(t => t.Id == translatorId)) .Select(translator => new MangaTranslator(translator, manga)) .ToList(); context.MangaTranslators.RemoveRange(mangaTranslatorsToRemove); await context.MangaTranslators.AddRangeAsync(mangaTranslatorsToAdd); } if (Authors != null) { var modelAuthorIds = Authors.Select(t => t.Id).ToList(); var mangaAuthorIds = manga.Authors.Select(t => t.Author.Id).ToList(); var mangaAuthorsToRemove = mangaAuthorIds.Except(modelAuthorIds) .Select(authorId => manga.Authors.First(t => t.Author.Id == authorId)) .ToList(); var mangaAuthorsToAdd = modelAuthorIds.Except(mangaAuthorIds) .Select(authorId => context.Authors.First(t => t.Id == authorId)) .Select(author => new MangaAuthor(author, manga)) .ToList(); context.MangaAuthors.RemoveRange(mangaAuthorsToRemove); await context.MangaAuthors.AddRangeAsync(mangaAuthorsToAdd); } }
public JsExpression TryTranslateCall(HalfTranslatedExpression context, HalfTranslatedExpression[] arguments, MethodInfo method) => Translators.Select(t => t.TryTranslateCall(context, arguments, method)).FirstOrDefault(d => d != null);