Beispiel #1
0
        public async ValueTask <bool> DeleteTagType(long tagTypeId)
        {
            using var context = Context;
            var mangaTagMappingService = new MangaTagMappingService(context);
            var tagService             = new TagService(context);
            var tagTypeService         = new TagTypeService(context);

            try
            {
                context.BeginTran();

                var tags = await tagService.GetListAsync(it => it.TypeId == tagTypeId);

                var keys = tags.Select(it => it.Key).ToList();
                await mangaTagMappingService.DeleteAsync(it => keys.Contains(it.TagId));

                await tagService.DeleteAsync(it => it.TypeId == tagTypeId);

                await tagTypeService.DeleteAsync(it => it.ObjectId == tagTypeId);

                context.CommitTran();
                return(true);
            }
            catch (Exception e)
            {
                context.RollbackTran();
                throw;
            }
        }
Beispiel #2
0
        public async ValueTask <Manga> GetManga(long mangaId, long userId)
        {
            using var context = Context;
            var mangaService           = new MangaService(context);
            var chapterService         = new ChapterService(context);
            var mangaTagMappingService = new MangaTagMappingService(context);
            var favoriteService        = new FavoriteService(context);
            var readProgressService    = new ReadProgressService(context);
            var commentProgressService = new CommentService(context);

            var manga = await mangaService.GetSingleAsync(it => it.ObjectId == mangaId);

            manga.Chapters = (await chapterService
                              .GetListAsync(it => it.MangaId == mangaId))
                             .OrderBy(it => it.Order)
                             .ToList();

            manga.Tags = await mangaTagMappingService.GetTagByMangaId(mangaId);

            if (userId <= 0)
            {
                return(manga);
            }

            manga.IsFavorite = await favoriteService.IsAnyAsync(it => it.EntityId == mangaId && it.UserId == userId);

            manga.ReadProgresses = await readProgressService.GetListAsync(it => it.MangaId == mangaId && it.UserId == userId);

            manga.Comment = await commentProgressService.GetSingleAsync(it => it.EntityId == mangaId && it.UserId == userId);

            return(manga);
        }
Beispiel #3
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 #4
0
        public async ValueTask <Manga> GetManga(long pathId)
        {
            using var context = Context;
            var mangaService           = new MangaService(context);
            var chapterService         = new ChapterService(context);
            var mangaTagMappingService = new MangaTagMappingService(context);
            var manga = await mangaService.GetSingleAsync(it => it.PathId == pathId);

            manga.Chapters = await chapterService.GetListAsync(it => it.MangaId == manga.ObjectId);

            manga.Tags = await mangaTagMappingService.GetTagByMangaId(manga.ObjectId);

            return(manga);
        }
Beispiel #5
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;
            }
        }
Beispiel #6
0
        public async ValueTask <bool> DeleteTag(long tagId)
        {
            using var context = Context;
            var mangaTagMappingService = new MangaTagMappingService(context);
            var tagService             = new TagService(context);

            context.BeginTran();
            try
            {
                context.BeginTran();
                var tag = await tagService.GetSingleAsync(it => it.ObjectId == tagId);

                if (tag.Key == tag.ObjectId)
                {
                    var keyTagList = await tagService.GetListAsync(it => it.Key == tagId && it.ObjectId != tagId);

                    if (keyTagList.Count != 0)
                    {
                        var newKey = keyTagList.First().ObjectId;
                        await tagService.UpdateKey(tagId, newKey);

                        await mangaTagMappingService.Update(tagId, newKey);
                    }
                    else
                    {
                        await mangaTagMappingService.DeleteAsync(it => it.TagId == tagId);
                    }
                }

                await tagService.DeleteAsync(it => it.ObjectId == tagId);

                context.CommitTran();
                FtsIndexService.DeleteTagFtxIndex(tagId);
                return(true);
            }
            catch (Exception e)
            {
                context.RollbackTran();
                throw;
            }
        }
Beispiel #7
0
        public async ValueTask <bool> DeleteManga(long mangaId)
        {
            using var context = Context;
            var mangaService           = new MangaService(context);
            var chapterService         = new ChapterService(context);
            var imageService           = new ImageService(context);
            var mangaTagMappingService = new MangaTagMappingService(context);
            var favoriteService        = new FavoriteService(context);
            var readProgressService    = new ReadProgressService(context);

            context.BeginTran();
            try
            {
                var chapters = await chapterService.GetListAsync(it => it.MangaId == mangaId);

                var chapterIds = chapters.Select(it => it.ObjectId).ToList();
                await mangaService.DeleteAsync(it => it.ObjectId == mangaId);

                await mangaTagMappingService.DeleteAsync(it => it.MangaId == mangaId);

                await chapterService.DeleteAsync(it => it.MangaId == mangaId);

                await imageService.DeleteAsync(it => chapterIds.Contains(it.ChapterId));

                await favoriteService.DeleteAsync(it => it.EntityId == mangaId);

                await readProgressService.DeleteAsync(it => it.MangaId == mangaId);

                context.CommitTran();
                FtsIndexService.DeleteMangaFtxIndex(mangaId);
                return(true);
            }
            catch (Exception e)
            {
                context.RollbackTran();
                throw;
            }
        }
Beispiel #8
0
        public async ValueTask <bool> Update(Tag tag)
        {
            using var context = Context;
            try
            {
                context.BeginTran();
                var tagService             = new TagService(context);
                var toBeMapping            = new HashSet <long>();
                var mangaTagMappingService = new MangaTagMappingService(context);

                var oldTag = await tagService.GetTagWithAliases(it => it.ObjectId == tag.ObjectId);

                // Update metadata
                if (oldTag == null || !await tagService.Update(tag))
                {
                    context.RollbackTran();
                    return(false);
                }

                // Check type of key tag == type of tag
                if (oldTag.Key != tag.Key)
                {
                    var keyTag = await tagService.GetSingleAsync(it => it.ObjectId == tag.Key);

                    if (tag.TypeId != keyTag?.TypeId)
                    {
                        context.RollbackTran();
                        return(false);
                    }

                    toBeMapping.Add(oldTag.Key);
                }

                // Recover all removed aliases
                foreach (var alias in oldTag.Aliases.Where(oldTagAlias => tag.Aliases.All(it => it.ObjectId != oldTagAlias.ObjectId)))
                {
                    var oldTagAlias = await tagService.GetSingleAsync(it => it.ObjectId == alias.ObjectId);

                    oldTagAlias.Key = oldTagAlias.ObjectId;
                    if (!await tagService.Update(oldTagAlias))
                    {
                        context.RollbackTran();
                        return(false);
                    }
                }

                for (int i = 0; i < tag.Aliases.Count; i++)
                {
                    var tagAlias = tag.Aliases[i];
                    var oldAlias = await tagService.GetSingleAsync(it => it.ObjectId == tagAlias.ObjectId);

                    if (oldAlias.TypeId != tag.TypeId)
                    {
                        context.RollbackTran();
                        return(false);
                    }

                    oldAlias.Aliases = await tagService.GetListAsync(it => it.Key == tagAlias.ObjectId && it.Key != it.ObjectId);

                    if (oldAlias.Aliases != null)
                    {
                        tag.Aliases.AddRange(oldAlias.Aliases);
                    }

                    if (oldAlias.Key == tag.Key)
                    {
                        continue;
                    }
                    toBeMapping.Add(oldAlias.Key);
                    oldAlias.Key = tag.Key;

                    if (await tagService.Update(oldAlias))
                    {
                        continue;
                    }
                    context.RollbackTran();
                    return(false);
                }

                foreach (var oldKey in toBeMapping)
                {
                    await mangaTagMappingService.Update(oldKey, tag.Key);
                }

                FtsIndexService.UpdateFtsIndex(tag);
                context.CommitTran();
                return(true);
            }
            catch (Exception e)
            {
                context.RollbackTran();
                throw;
            }
        }