public async Task <bool> DeleteAsync(int id)
 {
     try
     {
         return(await _dataService.DeleteById <TEntity>(id));
     }
     catch (Exception ex)
     {
         LogOrThrow(ex);
     }
     return(false);
 }
Example #2
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);
                }
        }
Example #3
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);
                }
            }
        }