Exemple #1
0
        public async Task <OperationResult> Save(List <WidgetModel> widgetList)
        {
            using var tran = await BlogContext.Database.BeginTransactionAsync();

            var entityList = await BlogContext.Widgets.ToListAsync();

            BlogContext.RemoveRange(entityList);
            await BlogContext.SaveChangesAsync();

            entityList = widgetList.Select(t => new Widget
            {
                Type   = t.Type,
                Id     = widgetList.IndexOf(t) + 1,
                Config = JsonConvert.SerializeObject(t.Config)
            }).ToList();

            await BlogContext.AddRangeAsync(entityList);

            await BlogContext.SaveChangesAsync();

            await tran.CommitAsync();

            RemoveCache();

            return(new OperationResult());
        }
Exemple #2
0
        /// <summary>
        /// Add settings
        /// </summary>
        /// <param name="model"></param>
        private async Task <OperationResult> AddSettingsAsync(InstallModel model)
        {
            var settingModel = new SettingModel(new Dictionary <string, string>(), SettingModelLocalizer);

            settingModel.Title        = model.BlogTitle;
            settingModel.Host         = model.BlogHost;
            settingModel.Language     = model.Language;
            settingModel.Registration = false;

            var settingList = settingModel.Settings.Select(t => new Setting
            {
                Key   = t.Key,
                Value = t.Value
            });

            await BlogContext.AddRangeAsync(settingList);

            return(OperationResult.SuccessResult);
        }
Exemple #3
0
        public async Task SavePost(PostDto postDto)
        {
            var userName = _httpContextAccessor.HttpContext.User.Identity.Name;
            var author   = await _blogContext.UserProfiles.FirstOrDefaultAsync(u => u.Email == userName);

            var insertedTags = await _blogContext.Tags.Where(c => postDto.Tags.Contains(c.Name)).ToListAsync();

            var newTags = postDto.Tags.Except(insertedTags.Select(c => c.Name)).Select(x => new Tag {
                Name = x
            });

            if (!postDto.Id.HasValue)
            {
                var newPost = new Post
                {
                    Content          = postDto.Content,
                    Title            = postDto.Title,
                    ShortDescription = postDto.ShortDescription,
                    UrlSlug          = postDto.Title.GenerateSlug(),
                    CategoryId       = Guid.Parse(postDto.CategoryId),
                    CreatedDate      = DateTime.Now,
                    UpdatedDate      = DateTime.Now,
                    IsPublished      = postDto.IsPublished,
                    FileUploadId     = postDto.FileUploadId,
                    AuthorId         = author.Id
                };

                await _blogContext.AddAsync(newPost);

                await _blogContext.AddRangeAsync(newTags);

                await _blogContext.SaveChangesAsync();

                var postTags = new List <PostTag>();
                foreach (var newTag in newTags)
                {
                    var dbNewTag = await _blogContext.Tags.FirstOrDefaultAsync(t => t.Name == newTag.Name);

                    postTags.Add(new PostTag {
                        PostId = newPost.Id, TagId = dbNewTag.Id
                    });
                }

                foreach (var insertedTag in insertedTags)
                {
                    postTags.Add(new PostTag {
                        PostId = newPost.Id, TagId = insertedTag.Id
                    });
                }


                await _blogContext.AddRangeAsync(postTags);
            }
            else
            {
                var post = await _blogContext.Posts.Where(p => p.Id == postDto.Id.Value).FirstOrDefaultAsync();

                post.Content          = postDto.Content;
                post.Title            = postDto.Title;
                post.ShortDescription = postDto.ShortDescription;
                post.UrlSlug          = postDto.Title.GenerateSlug();
                post.UpdatedDate      = DateTime.Now;
                post.IsPublished      = postDto.IsPublished;
                if (postDto.FileUploadId.HasValue)
                {
                    post.FileUploadId = postDto.FileUploadId;
                }

                post.CategoryId = Guid.Parse(postDto.CategoryId);
                post.AuthorId   = author.Id;

                var oldPostTags = await _blogContext.PostTags.Where(p => p.PostId == post.Id).ToListAsync();

                _blogContext.PostTags.RemoveRange(oldPostTags);

                await _blogContext.AddRangeAsync(newTags);

                await _blogContext.SaveChangesAsync();

                var postTags = new List <PostTag>();
                foreach (var newTag in newTags)
                {
                    var dbNewTag = await _blogContext.Tags.FirstOrDefaultAsync(t => t.Name == newTag.Name);

                    postTags.Add(new PostTag {
                        PostId = post.Id, TagId = dbNewTag.Id
                    });
                }

                foreach (var insertedTag in insertedTags)
                {
                    postTags.Add(new PostTag {
                        PostId = post.Id, TagId = insertedTag.Id
                    });
                }

                await _blogContext.AddRangeAsync(postTags);
            }

            await _blogContext.SaveChangesAsync();
        }
        public async Task <int> Add(List <TEntity> listEntity)
        {
            await _mySqlContext.AddRangeAsync(listEntity);

            return(await _mySqlContext.SaveChangesAsync());
        }