public async Task <CommandResult <SetBlogPostFeaturesCommand> > ExecuteAsync(SetBlogPostFeaturesCommand command)
        {
            try
            {
                var bpfts = await _context.BlogPostFeature.Where(bpf => bpf.BlogPostId == command.BlogPostId).ToListAsync();

                _context.BlogPostFeature.RemoveRange(bpfts);

                await _context.SaveChangesAsync();

                foreach (long fid in command.FeatureIds)
                {
                    BlogPostFeature bpf = new BlogPostFeature
                    {
                        BlogPostId = command.BlogPostId,
                        FeatureId  = fid
                    };

                    _context.BlogPostFeature.Add(bpf);
                }

                await _context.SaveChangesAsync();

                return(new CommandResult <SetBlogPostFeaturesCommand>(command, true));
            }
            catch (Exception ex)
            {
                return(new CommandResult <SetBlogPostFeaturesCommand>(command, false, ex));
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> EditPost(EditBlogPostViewModel model)
        {
            //var post = await _context.BlogPosts.FindAsync(model.Id);

            // fix these queries, the query for author can select from the author table without the join

            var post    = _context.BlogPosts.Where(bp => bp.Id == model.Id).Include(bp => bp.Author).FirstOrDefault();
            var curUser = await _userManager.GetUserAsync(User);

            var author = await(from u in _userManager.Users
                               where u.Id == curUser.Id
                               join a in _context.Authors on u.Id equals a.ApplicationUserId
                               select a).FirstOrDefaultAsync();

            // An admin is taking ownership
            if (post.Author == null)
            {
                post.Author = author;
            }

            post.Title       = model.Title;
            post.Description = model.Description;
            post.Content     = model.Content;
            post.ModifiedAt  = DateTime.Now;
            post.PublishOn   = model.PublishOn;
            post.Public      = model.Public;

            var bpfts = from bpf in _context.BlogPostFeature
                        where bpf.BlogPostId == post.Id
                        select bpf;

            _context.BlogPostFeature.RemoveRange(bpfts);

            await _context.SaveChangesAsync();

            if (model.FeaturesList != null)
            {
                foreach (var feature in model.FeaturesList)
                {
                    if (feature.IsSelected)
                    {
                        //var f = await _context.Features.FindAsync(feature.FeatureId);
                        BlogPostFeature bpf = new BlogPostFeature()
                        {
                            //BlogPost = post,
                            BlogPostId = post.Id,
                            //Feature = f,
                            FeatureId = feature.FeatureId
                        };
                        _context.BlogPostFeature.Add(bpf);
                    }
                }
            }
            else
            {
                model.FeaturesList = new List <FeaturesCheckBox>();
            }
            var bpcts = from bpc in _context.BlogPostCategory
                        where bpc.BlogPostId == post.Id
                        select bpc;

            _context.BlogPostCategory.RemoveRange(bpcts);

            await _context.SaveChangesAsync();

            if (model.CategoriesList != null)
            {
                foreach (var category in model.CategoriesList)
                {
                    if (category.IsSelected)
                    {
                        //var cat = await _context.Categories.FindAsync(category.CategoryId);

                        BlogPostCategory bpc = new BlogPostCategory()
                        {
                            //BlogPost = post,
                            BlogPostId = post.Id,
                            //Category = cat,
                            CategoryId = category.CategoryId
                        };
                        _context.BlogPostCategory.Add(bpc);
                    }
                }
            }
            else
            {
                model.CategoriesList = new List <CategoriesCheckBox>();
            }

            await _context.SaveChangesAsync();

            //return RedirectToAction("ManagePosts");
            //return Redirect(Request.Headers["Referer"]);
            ViewData["SavedMessage"] = "Post saved.";

            return(View(model));
        }