Ejemplo n.º 1
0
        private async Task LoadArticlesIntoFeed(FeedModel feed, int max = -1)
        {
            if (max == -1)
            {
                max = 100;
            }
            for (int i = 0; i < max; i++)
            {
                if (feed.AllArticles.Count <= i)
                {
                    var stringGuid = feed.Guid.ToString();
                    var realmax    = max == 0 ? 0 : max - i;
                    var relations  = await _sqliteService.GetByCondition <FeedArticleRelationEntity>(s => s.FeedGuid == stringGuid, s => s.Index, false, realmax, i);

                    foreach (var feedArticleRelationEntity in relations)
                    {
                        var article = await LoadHelper.LoadForFeed(feedArticleRelationEntity.ArticleId, feed, _sqliteService, _imageDownloadService);

                        feed.AllArticles.Add(article);
                    }
                }

                _imageDownloadService.Download(feed);

                //no more entries
                if (feed.AllArticles.Count <= i)
                {
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        public async Task AddRelatedThemesArticlesAsync(ArticleModel article)
        {
            await Initialize();

            await ExecuteSafe(async() =>
            {
                var aId       = article.GetId();
                var relations = await _sqliteService.GetByCondition <ThemeArticleRelations>(a => a.ArticleId == aId, null, false, 0, 0);
                var ids       = relations.Select(q => q.ThemeId);
                foreach (var source in ThemeManager.GetAllThemes().Where(d => ids.Any(id => id == d.GetId())))
                {
                    article.Themes.Add(source);
                }
            });
        }
Ejemplo n.º 3
0
        public async Task <List <TBusiness> > GetByConditionAsyc(Expression <Func <TEntity, bool> > func, Expression <Func <TEntity, object> > orderByProperty = null, bool descending = false, int limit = 0, int skip = 0)
        {
            try
            {
                var entityList = await _dataService.GetByCondition(func, orderByProperty, descending, limit, skip);

                if (entityList.Any())
                {
                    return((from entity in entityList let business = new TBusiness() select MappingHelper.ConvertToModel(entity, business)).ToList());
                }
            }
            catch (Exception ex)
            {
                LogOrThrow(ex);
            }
            return(new List <TBusiness>());
        }
Ejemplo n.º 4
0
        public static async Task<ArticleModel> LoadForFeed(int id, FeedModel feed, ISqliteService sqliteService, IImageDownloadService imageDownloadService)
        {
            var arRepo = new GenericRepository<ArticleModel, ArticleEntity>(sqliteService);
            var imgRepo = new GenericRepository<ImageContentModel, ImageContentEntity>(sqliteService);

            var art = await arRepo.GetByIdAsync(id);
            var contents = await sqliteService.GetByCondition<ContentEntity>(s => s.ParentId == id && s.ContentType == (int)ContentType.LeadImage, s => s.Index, false, 1, 0);
            if (contents?.FirstOrDefault() != null)
            {
                var image = await imgRepo.GetByIdAsync(contents.FirstOrDefault().ContentId);
                art.LeadImage = image;

                if (art.LeadImage?.LoadingState < LoadingState.Loaded)
                    imageDownloadService.Download(art);
            }
            art.Feed = feed;
            return art;
        }
Ejemplo n.º 5
0
        public static async Task SaveArticleLeadImage(ArticleModel model, ISqliteService service, bool skipCleaning = false)
        {
            var imageContentGenericRepository  = new GenericRepository <ImageContentModel, ImageContentEntity>(service);
            List <ContentEntity> oldLeadImages = null;
            var articleId = model.GetId();

            if (!skipCleaning)
            {
                oldLeadImages = (await service.GetByCondition <ContentEntity>(e => e.ParentId == articleId && e.ContentType == (int)ContentType.LeadImage, null, false, 0, 0)).ToList();
            }

            if (model.LeadImage != null)
            {
                if (model.LeadImage.GetId() != 0)
                {
                    var leadImageId  = model.LeadImage.GetId();
                    var oldLeadImage = oldLeadImages?.FirstOrDefault(o => o.ContentId == leadImageId);
                    if (oldLeadImage != null)
                    {
                        oldLeadImages?.Remove(oldLeadImage);
                    }

                    await imageContentGenericRepository.SaveAsyc(model.LeadImage);
                }
                else
                {
                    await imageContentGenericRepository.SaveAsyc(model.LeadImage);

                    var entity = new ContentEntity
                    {
                        ContentId   = model.LeadImage.GetId(),
                        ParentId    = model.GetId(),
                        Index       = 0,
                        ContentType = (int)ContentType.LeadImage
                    };
                    await service.Add(entity);
                }
            }

            if (!skipCleaning)
            {
                await service.DeleteAllById <ContentEntity>(oldLeadImages.Select(d => d.Id));
            }
        }
Ejemplo n.º 6
0
        public static async Task <ArticleModel> LoadForFeed(int id, FeedModel feed, ISqliteService sqliteService, IImageDownloadService imageDownloadService)
        {
            var arRepo  = new GenericRepository <ArticleModel, ArticleEntity>(sqliteService);
            var imgRepo = new GenericRepository <ImageContentModel, ImageContentEntity>(sqliteService);

            var art = await arRepo.GetByIdAsync(id);

            var contents = await sqliteService.GetByCondition <ContentEntity>(s => s.ParentId == id && s.ContentType == (int)ContentType.LeadImage, s => s.Index, false, 1, 0);

            if (contents?.FirstOrDefault() != null)
            {
                var image = await imgRepo.GetByIdAsync(contents.FirstOrDefault().ContentId);

                art.LeadImage = image;

                if (art.LeadImage?.LoadingState < LoadingState.Loaded)
                {
                    imageDownloadService.Download(art);
                }
            }
            art.Feed = feed;
            return(art);
        }
Ejemplo n.º 7
0
        public static async Task SaveArticleLeadImage(ArticleModel model, ISqliteService service, bool skipCleaning = false)
        {
            var imageContentGenericRepository = new GenericRepository<ImageContentModel, ImageContentEntity>(service);
            List<ContentEntity> oldLeadImages = null;
            var articleId = model.GetId();
            if (!skipCleaning)
                oldLeadImages = (await service.GetByCondition<ContentEntity>(e => e.ParentId == articleId && e.ContentType == (int)ContentType.LeadImage, null, false, 0, 0)).ToList();

            if (model.LeadImage != null)
            {
                if (model.LeadImage.GetId() != 0)
                {
                    var leadImageId = model.LeadImage.GetId();
                    var oldLeadImage = oldLeadImages?.FirstOrDefault(o => o.ContentId == leadImageId);
                    if (oldLeadImage != null)
                        oldLeadImages?.Remove(oldLeadImage);

                    await imageContentGenericRepository.SaveAsyc(model.LeadImage);
                }
                else
                {
                    await imageContentGenericRepository.SaveAsyc(model.LeadImage);

                    var entity = new ContentEntity
                    {
                        ContentId = model.LeadImage.GetId(),
                        ParentId = model.GetId(),
                        Index = 0,
                        ContentType = (int)ContentType.LeadImage
                    };
                    await service.Add(entity);
                }
            }

            if (!skipCleaning)
                await service.DeleteAllById<ContentEntity>(oldLeadImages.Select(d => d.Id));
        }
Ejemplo n.º 8
0
        public static async Task SaveArticleContent(ArticleModel model, ISqliteService service, bool skipCleaning = false)
        {
            var imageContentGenericRepository = new GenericRepository<ImageContentModel, ImageContentEntity>(service);
            var textContentGenericRepository = new GenericRepository<TextContentModel, TextContentEntity>(service);
            var galleryContentGenericRepository = new GenericRepository<GalleryContentModel, GalleryContentEntity>(service);

            var supportedContents = new[] { (int)ContentType.Text, (int)ContentType.Gallery, (int)ContentType.Image };
            List<ContentEntity> oldModels = null;
            if (!skipCleaning)
            {
                var id = model.GetId();
                oldModels = (await service.GetByCondition<ContentEntity>(e => e.ParentId == id, null, false, 0, 0)).ToList();
                oldModels = oldModels.Where(e => supportedContents.Any(s => s == e.ContentType)).ToList();
            }
            for (int i = 0; i < model.Content.Count; i++)
            {
                var baseContentModel = model.Content[i];

                ContentEntity entity = null;
                if (!skipCleaning)
                {
                    entity = oldModels.FirstOrDefault(m => m.ContentId == baseContentModel.GetId());
                    oldModels.Remove(entity);
                }

                if (entity == null)
                    entity = new ContentEntity();

                if (baseContentModel is TextContentModel)
                {
                    var text = (TextContentModel)baseContentModel;
                    text.ContentJson = JsonConvert.SerializeObject(text.Content);
                    await textContentGenericRepository.SaveAsyc(text);
                    entity.ContentType = (int)ContentType.Text;
                }
                else if (baseContentModel is ImageContentModel)
                {
                    var image = (ImageContentModel)baseContentModel;
                    if (image.Text != null)
                    {
                        await textContentGenericRepository.SaveAsyc(image.Text);
                        image.TextContentId = image.Text.GetId();
                    }
                    await imageContentGenericRepository.SaveAsyc(image);
                    entity.ContentType = (int)ContentType.Image;
                }
                else if (baseContentModel is GalleryContentModel)
                {
                    var gallery = (GalleryContentModel)baseContentModel;
                    if (gallery.Text != null)
                    {
                        await textContentGenericRepository.SaveAsyc(gallery.Text);
                        gallery.TextContentId = gallery.Text.GetId();
                    }
                    await galleryContentGenericRepository.SaveAsyc(gallery);
                    for (int index = 0; index < gallery.Images.Count; index++)
                    {
                        gallery.Images[index].GalleryId = gallery.GetId();
                        gallery.Images[index].GalleryIndex = index;
                        if (gallery.Images[index].Text != null)
                        {
                            await textContentGenericRepository.SaveAsyc(gallery.Images[index].Text);
                            gallery.Images[index].TextContentId = gallery.Images[index].Text.GetId();
                        }
                        await imageContentGenericRepository.SaveAsyc(gallery.Images[index]);
                    }
                    entity.ContentType = (int)ContentType.Gallery;
                }
                else
                {
                    continue;
                }
                entity.ContentId = baseContentModel.GetId();
                entity.ParentId = model.GetId();
                entity.Index = i;
                if (entity.Id == 0)
                    await service.Add(entity);
                else
                    await service.Update(entity);
            }

            if (!skipCleaning && oldModels != null)
                foreach (var contentEntity in oldModels)
                {
                    await service.DeleteById<ContentEntity>(contentEntity.Id);
                }
        }
Ejemplo n.º 9
0
        public static async Task SaveFeed(FeedModel model, List<ArticleModel> newArticles, ISqliteService service, IImageDownloadService imageDownloadService)
        {
            var stringGuid = model.Guid.ToString();
            var feedEntries = await service.GetByCondition<FeedArticleRelationEntity>(d => d.FeedGuid == stringGuid, null, false, 0, 0);
            var oldArticles = new List<ArticleModel>(model.AllArticles);


            for (int index = 0; index < newArticles.Count; index++)
            {
                var articleModel = newArticles[index];
                articleModel.Feed = model;

                var oldOne = oldArticles.FirstOrDefault(s => s.PublicUri == articleModel.PublicUri);
                if (oldOne == null)
                {
                    var oldFromDatabase = feedEntries.FirstOrDefault(s => articleModel.PublicUri == s.Url);
                    if (oldFromDatabase != null)
                    {
                        var article = await LoadHelper.LoadForFeed(oldFromDatabase.ArticleId, model, service, imageDownloadService);
                        feedEntries.Remove(oldFromDatabase);
                        oldFromDatabase.Index = index;
                        await service.Update(oldFromDatabase);

                        if (model.AllArticles.Count > index)
                            model.AllArticles[index] = article;
                        else
                            model.AllArticles.Add(article);
                    }
                    else
                    {
                        await SaveArticle(articleModel, service);
                        await SaveArticleLeadImage(articleModel, service, true);
                        await SaveArticleContent(articleModel, service, true);

                        var fe = new FeedArticleRelationEntity()
                        {
                            ArticleId = articleModel.GetId(),
                            Url = articleModel.PublicUri,
                            FeedGuid = model.Guid.ToString(),
                            Index = index
                        };
                        await service.Add(fe);

                        if (model.AllArticles.Count > index)
                            model.AllArticles[index] = articleModel;
                        else
                            model.AllArticles.Add(articleModel);
                    }
                }
                else
                {
                    if (model.AllArticles.Count > index)
                        model.AllArticles[index] = oldOne;
                    else
                        model.AllArticles.Add(oldOne);

                    var oldFromDatabase = feedEntries.FirstOrDefault(s => articleModel.PublicUri == s.Url);
                    if (oldFromDatabase != null)
                    {
                        oldFromDatabase.Index = index;
                        await service.Update(oldFromDatabase);
                    }
                    else
                    {
                        var fe = new FeedArticleRelationEntity()
                        {
                            ArticleId = oldOne.GetId(),
                            Url = oldOne.PublicUri,
                            FeedGuid = model.Guid.ToString(),
                            Index = index
                        };
                        await service.Add(fe);
                    }
                }
            }

            await service.DeleteAllById<FeedArticleRelationEntity>(feedEntries.Select(s => s.Id));
        }
Ejemplo n.º 10
0
        public static async Task SaveArticleContent(ArticleModel model, ISqliteService service, bool skipCleaning = false)
        {
            var imageContentGenericRepository   = new GenericRepository <ImageContentModel, ImageContentEntity>(service);
            var textContentGenericRepository    = new GenericRepository <TextContentModel, TextContentEntity>(service);
            var galleryContentGenericRepository = new GenericRepository <GalleryContentModel, GalleryContentEntity>(service);

            var supportedContents          = new[] { (int)ContentType.Text, (int)ContentType.Gallery, (int)ContentType.Image };
            List <ContentEntity> oldModels = null;

            if (!skipCleaning)
            {
                var id = model.GetId();
                oldModels = (await service.GetByCondition <ContentEntity>(e => e.ParentId == id, null, false, 0, 0)).ToList();
                oldModels = oldModels.Where(e => supportedContents.Any(s => s == e.ContentType)).ToList();
            }
            for (int i = 0; i < model.Content.Count; i++)
            {
                var baseContentModel = model.Content[i];

                ContentEntity entity = null;
                if (!skipCleaning)
                {
                    entity = oldModels.FirstOrDefault(m => m.ContentId == baseContentModel.GetId());
                    oldModels.Remove(entity);
                }

                if (entity == null)
                {
                    entity = new ContentEntity();
                }

                if (baseContentModel is TextContentModel)
                {
                    var text = (TextContentModel)baseContentModel;
                    text.ContentJson = JsonConvert.SerializeObject(text.Content);
                    await textContentGenericRepository.SaveAsyc(text);

                    entity.ContentType = (int)ContentType.Text;
                }
                else if (baseContentModel is ImageContentModel)
                {
                    var image = (ImageContentModel)baseContentModel;
                    if (image.Text != null)
                    {
                        await textContentGenericRepository.SaveAsyc(image.Text);

                        image.TextContentId = image.Text.GetId();
                    }
                    await imageContentGenericRepository.SaveAsyc(image);

                    entity.ContentType = (int)ContentType.Image;
                }
                else if (baseContentModel is GalleryContentModel)
                {
                    var gallery = (GalleryContentModel)baseContentModel;
                    if (gallery.Text != null)
                    {
                        await textContentGenericRepository.SaveAsyc(gallery.Text);

                        gallery.TextContentId = gallery.Text.GetId();
                    }
                    await galleryContentGenericRepository.SaveAsyc(gallery);

                    for (int index = 0; index < gallery.Images.Count; index++)
                    {
                        gallery.Images[index].GalleryId    = gallery.GetId();
                        gallery.Images[index].GalleryIndex = index;
                        if (gallery.Images[index].Text != null)
                        {
                            await textContentGenericRepository.SaveAsyc(gallery.Images[index].Text);

                            gallery.Images[index].TextContentId = gallery.Images[index].Text.GetId();
                        }
                        await imageContentGenericRepository.SaveAsyc(gallery.Images[index]);
                    }
                    entity.ContentType = (int)ContentType.Gallery;
                }
                else
                {
                    continue;
                }
                entity.ContentId = baseContentModel.GetId();
                entity.ParentId  = model.GetId();
                entity.Index     = i;
                if (entity.Id == 0)
                {
                    await service.Add(entity);
                }
                else
                {
                    await service.Update(entity);
                }
            }

            if (!skipCleaning && oldModels != null)
            {
                foreach (var contentEntity in oldModels)
                {
                    await service.DeleteById <ContentEntity>(contentEntity.Id);
                }
            }
        }
Ejemplo n.º 11
0
        public static async Task SaveFeed(FeedModel model, List <ArticleModel> newArticles, ISqliteService service, IImageDownloadService imageDownloadService)
        {
            var stringGuid  = model.Guid.ToString();
            var feedEntries = await service.GetByCondition <FeedArticleRelationEntity>(d => d.FeedGuid == stringGuid, null, false, 0, 0);

            var oldArticles = new List <ArticleModel>(model.AllArticles);


            for (int index = 0; index < newArticles.Count; index++)
            {
                var articleModel = newArticles[index];
                articleModel.Feed = model;

                var oldOne = oldArticles.FirstOrDefault(s => s.PublicUri == articleModel.PublicUri);
                if (oldOne == null)
                {
                    var oldFromDatabase = feedEntries.FirstOrDefault(s => articleModel.PublicUri == s.Url);
                    if (oldFromDatabase != null)
                    {
                        var article = await LoadHelper.LoadForFeed(oldFromDatabase.ArticleId, model, service, imageDownloadService);

                        feedEntries.Remove(oldFromDatabase);
                        oldFromDatabase.Index = index;
                        await service.Update(oldFromDatabase);

                        if (model.AllArticles.Count > index)
                        {
                            model.AllArticles[index] = article;
                        }
                        else
                        {
                            model.AllArticles.Add(article);
                        }
                    }
                    else
                    {
                        await SaveArticle(articleModel, service);
                        await SaveArticleLeadImage(articleModel, service, true);
                        await SaveArticleContent(articleModel, service, true);

                        var fe = new FeedArticleRelationEntity()
                        {
                            ArticleId = articleModel.GetId(),
                            Url       = articleModel.PublicUri,
                            FeedGuid  = model.Guid.ToString(),
                            Index     = index
                        };
                        await service.Add(fe);

                        if (model.AllArticles.Count > index)
                        {
                            model.AllArticles[index] = articleModel;
                        }
                        else
                        {
                            model.AllArticles.Add(articleModel);
                        }
                    }
                }
                else
                {
                    if (model.AllArticles.Count > index)
                    {
                        model.AllArticles[index] = oldOne;
                    }
                    else
                    {
                        model.AllArticles.Add(oldOne);
                    }

                    var oldFromDatabase = feedEntries.FirstOrDefault(s => articleModel.PublicUri == s.Url);
                    if (oldFromDatabase != null)
                    {
                        oldFromDatabase.Index = index;
                        await service.Update(oldFromDatabase);
                    }
                    else
                    {
                        var fe = new FeedArticleRelationEntity()
                        {
                            ArticleId = oldOne.GetId(),
                            Url       = oldOne.PublicUri,
                            FeedGuid  = model.Guid.ToString(),
                            Index     = index
                        };
                        await service.Add(fe);
                    }
                }
            }

            await service.DeleteAllById <FeedArticleRelationEntity>(feedEntries.Select(s => s.Id));
        }