public async ValueTask <Tag> Insert(Tag tag) { if (!DataFormatter.Format(tag)) { return(null); } using var context = Context; try { context.BeginTran(); var tagService = new TagService(context); var result = await tagService.Insert(tag); if (!result) { return(null); } FtsIndexService.CreateFtsIndex(new List <Tag>() { tag }); context.CommitTran(); return(tag); } catch (Exception e) { context.RollbackTran(); throw; } }
public async ValueTask <bool> Insert(Manga manga) { if (!DataFormatter.Format(manga)) { return(false); } using var context = Context; try { context.BeginTran(); var mangaService = new MangaService(context); await mangaService.Upsert(manga); var chapterService = new ChapterService(context); await chapterService.Upsert(manga.Chapters); var imageService = new ImageService(context); var images = manga.Chapters.SelectMany(it => it.Images).ToList(); images.Add(manga.Cover); await imageService.Upsert(images); if (manga.Tags != null && manga.Tags.Count > 0) { var mangaTagMappingService = new MangaTagMappingService(context); var mappings = manga.Tags.Select(it => new MangaTagMapping() { MangaId = manga.ObjectId, TagId = it.Key }).ToList(); await mangaTagMappingService.Insert(mappings); } FtsIndexService.CreateFtsIndex(new List <Manga>() { manga }); context.CommitTran(); return(true); } catch (Exception e) { context.RollbackTran(); throw; } }
public async ValueTask <List <Tag> > Insert(List <Tag> tags) { if (tags == null || tags.Count <= 0) { return(null); } foreach (var tag in tags) { DataFormatter.Format(tag); } var context = Context; var tagTypeService = new TagTypeService(context); var tagService = new TagService(context); try { context.BeginTran(); await tagTypeService.Insert(tags.Select(it => it.Type).Distinct().ToList()); var tagType = (await tagTypeService .GetTagTypes(tags.Select(it => it.Type.Name).Distinct().ToList())) .ToDictionary(it => it.Name, it => it); foreach (var tag in tags) { tag.Type = tagType[tag.Type.Name]; tag.TypeId = tag.Type.ObjectId; } var result = await tagService.Insert(tags); FtsIndexService.CreateFtsIndex(result.InsertList.Select(it => it.Item).ToList()); var realTags = await tagService.GetTags(tags.Select(it => new Tuple <long, string>(it.TypeId, it.Name)).ToList()); context.CommitTran(); return(realTags); } catch (Exception e) { context.RollbackTran(); throw; } }