public async Task AddThemeToArticleAsync(ArticleModel article, string theme) { await Initialize(); await ExecuteSafe(async() => { var normalizedTheme = NormalizeThemeName(theme); var themeModel = ThemeManager.TryGetSimilarTheme(normalizedTheme); if (themeModel == null) { themeModel = new ThemeModel() { NormalizedName = normalizedTheme, Name = theme }; //concurrency: soem other thread may have added the same theme var tm = ThemeManager.TryAddTheme(themeModel); if (tm == themeModel) { await _themeGenericRepository.AddAsync(themeModel); } } if (article.Themes.Contains(themeModel)) { return; } article.Themes.Add(themeModel); await _sqliteService.Add(new ThemeArticleRelations() { ArticleId = article.GetId(), ThemeId = themeModel.GetId() }); }); }
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)); } }
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); } }); }
protected async Task AddThemesAsync(ArticleModel model, string[] themes = null) { if (model.GetId() == 0) { return; } if (themes != null) { foreach (var theme in themes.Where(t => t != null)) { await _themeRepository.AddThemeToArticleAsync(model, theme); } } await _themeRepository.AddThemeToArticleAsync(model, model.Feed.Name); }
public async Task LoadArticleThemesAsync(ArticleModel am) { await Initialize(); await ExecuteSafe(async() => { var id = am.GetId(); var cm = await _sqliteService.GetByCondition <ThemeArticleRelations>(d => d.ArticleId == id, null, false, 0, 0); foreach (var entry in cm.ToArray()) { var theme = ThemeManager.GetAllThemes().FirstOrDefault(t => t.GetId() == entry.ThemeId); if (theme != null) { am.Themes.Add(theme); } } }); }
public Task <bool> LoadFullArticleAsync(ArticleModel am) { return(ExecuteSafe(async() => { _imageDownloadService.Download(am); if (am.LoadingState == LoadingState.Loaded) { if (am.Content.Any()) { return true; } var id = am.GetId(); var contents = await _sqliteService.GetByCondition <ContentEntity>(s => s.ParentId == id, s => s.Index, false, 0, 0); for (int index = 0; index < contents.Count; index++) { if (am.Content.Count > index && am.Content[index].GetId() == contents[index].ContentId) { continue; } var contentEntity = contents[index]; switch (contentEntity.ContentType) { case (int)ContentType.Text: { var text = await _textContentGenericRepository.GetByIdAsync(contentEntity.ContentId); text.Content = text.ContentJson != null ? JsonConvert.DeserializeObject <ObservableCollection <ParagraphModel> >( text.ContentJson) : new ObservableCollection <ParagraphModel>(); am.Content.Add(text); break; } case (int)ContentType.Image: { var image = await _imageContentGenericRepository.GetByIdAsync(contentEntity.ContentId); am.Content.Add(image); break; } case (int)ContentType.Gallery: { var amId = am.GetId(); var galleryContents = await _sqliteService.GetByCondition <ContentEntity>(s => s.ParentId == amId, s => s.Index, false, 0, 0); var gallery = await _galleryContentGenericRepository.GetByIdAsync(contentEntity.ContentId); am.Content.Add(gallery); foreach ( var galleryContent in galleryContents.Where(g => g.ContentType == (int)ContentType.Image)) { var image = await _imageContentGenericRepository.GetByIdAsync(galleryContent.ContentId); gallery.Images.Add(image); } break; } } } await _themeRepository.LoadArticleThemesAsync(am); } else if (am.LoadingState == LoadingState.New) { if (await _permissionsService.CanDownloadArticles()) { await ActualizeArticleAsync(am); } } return true; })); }
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); } } }