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 async Task Create() { var processId = Guid.NewGuid().ToString(); var messageBody = new ManagementModel { ProcessId = processId, MessageType = MessageType.BeginProcess.ToString(), ObjectNumber = 1 }; var message = JsonConvert.SerializeObject(messageBody); _sqlite.Add(new ProcessEntity { ProcessId = processId }); await _azure.SendMessageAsync(AppConst.ManagementQueueName, message); }
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 <bool> AddAsync(TBusiness business) { try { var entity = MappingHelper.ConvertToEntity(business, new TEntity()); int id = await _dataService.Add(entity); if (id == 1) { business.SetId(entity.Id); return(true); } } catch (Exception ex) { LogOrThrow(ex); } return(false); }
public async Task SaveAsync(string authToken) { var currentSurveys = GetList().Select(x => x.SurveyId).ToList(); var surveysList = await _rest.GetAsync($"{_server.Get().NfieldServer}/v1/Surveys", authToken); var serverSurveys = JsonConvert.DeserializeObject <List <SurveyDetailsEntity> >(surveysList); var newSurveys = serverSurveys.Select(y => y.SurveyId).ToList(); var nonLocalSurveys = newSurveys.Except(currentSurveys).ToList(); foreach (var nonLocalSurveyId in nonLocalSurveys) { var count = await _surveyCounts.SuccessfulCounts(authToken, nonLocalSurveyId); var survey = serverSurveys.FirstOrDefault(s => s.SurveyId == nonLocalSurveyId); survey.SuccessFulCount = count.ToString(); survey.Icon = survey.SurveyType == SurveyType.OnlineBasic.ToString() ? AppConst.OnlineSurveyIcon : AppConst.MobileSurveyIcon; survey.Image = survey.Image ?? AppConst.UnSelectFavourite; _sqlite.Add(survey); } }
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 ManagerPage() { InitializeComponent(); switchOnListen = true; ToolbarItems.Add(new ToolbarItem("Delete", "", () => { _sqlite.DeleteAll(); })); ToolbarItems.Add(new ToolbarItem("StopListen", "", () => { Task.Run(async() => await SendMessage("managerId", "stop")); switchOnListen = false; })); Device.StartTimer(TimeSpan.FromSeconds(5), () => { Task.Run(async() => await SendMessage("managerId", "start")); return(switchOnListen); }); Device.StartTimer(TimeSpan.FromSeconds(5), () => { return(true); }); _sqlite = DependencyService .Get <ISqliteService <InterviewerEntity> >(); var client = new ClientHandler("Manager", "SendMessageInterviewer"); Task.Run(async() => await client.StartAsync()); var interviewer = new Label(); var watchGrid = new Grid(); client.Message += (sender, e) => { if (e.Id != "managerId") { var interviewerEntity = new InterviewerEntity { InterviewerId = e.Id }; var interviewerExist = _sqlite.Get().Any(x => x.InterviewerId == e.Id); var initialInterviewerCount = _sqlite.Get().Count(); if (!interviewerExist) { _sqlite.Add(interviewerEntity); } var interviewerAddedCount = _sqlite.Get().Count(); Device.BeginInvokeOnMainThread(() => { var stack = new StackLayout(); int numberOfRows = watchGrid.RowDefinitions.Count; var column = 0; var row = 0; if (numberOfRows > 0) { var numberOfChildren = watchGrid.Children.Count; if (interviewerAddedCount % 2 != 0 && initialInterviewerCount != interviewerAddedCount) { watchGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(600) }); } for (int childIndex = 0; childIndex < numberOfChildren; ++childIndex) { var view = watchGrid.Children[childIndex]; var element = ((StackLayout)view).Children.ElementAt(0); row = Grid.GetRow(watchGrid.Children[childIndex]); column = Grid.GetColumn(watchGrid.Children[childIndex]); if (interviewerAddedCount % 2 != 0 && initialInterviewerCount != interviewerAddedCount) { row++; } if (((Label)element).Text == e.Id) { watchGrid.Children.RemoveAt(childIndex); stack = new ContentViewBuilder().Build(e.Message, e.Id); watchGrid.Children.Add(stack, column, row); break; } if (((Label)element).Text != e.Id && row == Grid.GetRow(watchGrid.Children[childIndex]) && initialInterviewerCount != interviewerAddedCount) { stack = new ContentViewBuilder().Build(e.Message, e.Id); watchGrid.Children.Add(stack, column + 1, row); break; } if (((Label)element).Text != e.Id && row != Grid.GetRow(watchGrid.Children[childIndex]) && initialInterviewerCount != interviewerAddedCount) { stack = new ContentViewBuilder().Build(e.Message, e.Id); watchGrid.Children.Add(stack, column, row); break; } } } else { watchGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(300) }); watchGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(300) }); watchGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(600) }); stack = new ContentViewBuilder().Build(e.Message, e.Id); watchGrid.Children.Add(stack, 0, 0); } Content = watchGrid; }); } }; }
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); } }
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)); }
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); } } }
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)); }
public void Set(string serverSelected) { var server = new ServerEntity(); switch (serverSelected) { case AppConst.America: server.NfieldServer = AppConst.UrlAmerica; server.ServerName = AppConst.America; break; case AppConst.Asia: server.NfieldServer = AppConst.UrlAsia; server.ServerName = AppConst.Asia; break; case AppConst.Blue: server.NfieldServer = AppConst.UrlBlue; server.ServerName = AppConst.Blue; break; case AppConst.China: server.NfieldServer = AppConst.UrlChina; server.ServerName = AppConst.China; break; case AppConst.Europe: server.NfieldServer = AppConst.UrlEurope; server.ServerName = AppConst.Europe; break; case AppConst.Orange: server.NfieldServer = AppConst.UrlOrange; server.ServerName = AppConst.Orange; break; case AppConst.Purple: server.NfieldServer = AppConst.UrlPurple; server.ServerName = AppConst.Purple; break; case AppConst.Red: server.NfieldServer = AppConst.UrlRed; server.ServerName = AppConst.Red; break; case AppConst.White: server.NfieldServer = AppConst.UrlWhite; server.ServerName = AppConst.White; break; case AppConst.Yellow: server.NfieldServer = AppConst.UrlYellow; server.ServerName = AppConst.Yellow; break; case AppConst.Rc: server.NfieldServer = AppConst.UrlRc; server.ServerName = AppConst.Rc; break; default: break; } var exists = Get(); if (exists == null) { _sqliteService.Add(server); } else { _sqliteService.Update(server); } }