예제 #1
0
        public async Task <IActionResult> UpdatePostAsyncActionResult([FromBody] Models.Post post)
        {
            if (!this._blogDbContext.Posts.Any(p => p.Id == post.Id))
            {
                return(this.NotFound());
            }

            post.LastModified = DateTime.Now;

            var postEntity = Mapper.Map <Post>(post);

            this._blogDbContext.Update(postEntity);

            foreach (var category in post.Categories.Where(c => c.Id == Guid.Empty))
            {
                await this._blogDbContext.Categories.AddAsync(Mapper.Map <Category>(category));
            }

            await this._blogDbContext.SaveChangesAsync();

            postEntity = this._blogDbContext.Posts.Include(p => p.PostCategories).FirstOrDefault(p => p.Id == post.Id);

            foreach (var category in post.Categories)
            {
                var categoryEntity = this._blogDbContext.Categories.First(c => c.Name == category.Name);

                if (!postEntity.PostCategories.Any(pc => pc.CategoryId == categoryEntity.Id))
                {
                    postEntity.PostCategories.Add(new PostCategory()
                    {
                        CategoryId = categoryEntity.Id
                    });
                }
            }

            foreach (var postCategory in postEntity.PostCategories.Where(pc => !post.Categories.Any(c => c.Id == pc.CategoryId)).ToCollection())
            {
                postEntity.PostCategories.Remove(postCategory);
            }

            this._blogDbContext.Update(postEntity);

            await this._blogDbContext.SaveChangesAsync();

            return(this.Json(Mapper.Map <Models.Post>(postEntity)));
        }
예제 #2
0
        public async Task <IActionResult> CreatePostAsyncActionResult([FromBody] Models.Post post)
        {
            post.Id      = Guid.NewGuid();
            post.Created = DateTime.Now;

            var postEntity = Mapper.Map <Post>(post);

            var currentUserName = this.HttpContext.User?.Identity.Name;

            var userEntity = this._blogDbContext.Users.FirstOrDefault(u => u.UserName == currentUserName);

            if (userEntity == null)
            {
                var user = await this._applicationUserManager.FindByNameAsync(currentUserName);

                userEntity = Mapper.Map <User>(user);
            }

            postEntity.User = userEntity;

            foreach (var category in post.Categories.Where(c => c.Id == Guid.Empty))
            {
                await this._blogDbContext.Categories.AddAsync(Mapper.Map <Category>(category));
            }

            await this._blogDbContext.SaveChangesAsync();

            postEntity.PostCategories = new Collection <PostCategory>();

            foreach (var category in post.Categories)
            {
                var categoryEntity = this._blogDbContext.Categories.First(c => c.Name == category.Name);

                postEntity.PostCategories.Add(new PostCategory()
                {
                    CategoryId = categoryEntity.Id
                });
            }

            await this._blogDbContext.Posts.AddAsync(postEntity);

            await this._blogDbContext.SaveChangesAsync();

            return(this.Json(Mapper.Map <Models.Post>(postEntity)));
        }