Beispiel #1
0
        public async ValueTask <bool> Update(Manga manga, bool updateChapters)
        {
            using var context = Context;
            var mangaService = new MangaService(context);

            try
            {
                context.BeginTran();
                if ((await mangaService.GetSingleAsync(it => it.ObjectId == manga.ObjectId)).Version >= manga.Version)
                {
                    context.RollbackTran();
                    return(false);
                }
                manga.UpdateTime = DateTime.Now;

                if (updateChapters && manga.Chapters != null && manga.Chapters.Count != 0)
                {
                    var chapterService = new ChapterService(context);
                    var imageService   = new ImageService(context);
                    var result         = await chapterService.Upsert(manga.Chapters);

                    if (result.InsertList.Count != 0)
                    {
                        foreach (var storageableMessage in result.InsertList)
                        {
                            DataFormatter.Format(storageableMessage.Item);
                            await imageService.Upsert(storageableMessage.Item.Images);
                        }
                    }
                }

                if (manga.Tags != null)
                {
                    var mangaTagMappingService = new MangaTagMappingService(context);
                    await mangaTagMappingService.DeleteTagByMangaId(manga.ObjectId);

                    var mangaTagMappings = manga.Tags.Select(tag => new MangaTagMapping()
                    {
                        MangaId = manga.ObjectId,
                        TagId   = tag.Key
                    }).ToList();
                    if (mangaTagMappings.Count > 0)
                    {
                        await mangaTagMappingService.Insert(mangaTagMappings);
                    }
                }

                await mangaService.Upsert(manga);

                FtsIndexService.UpdateFtsIndex(manga);
                context.CommitTran();
                return(true);
            }
            catch (Exception e)
            {
                context.RollbackTran();
                throw;
            }
        }
Beispiel #2
0
        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;
            }
        }