Example #1
0
        public async Task<IActionResult> Add(PostViewModel model)
        {
            if (ModelState.IsValid)
            {
                model.Slug = model.Slug.ToSlug(); // Remove any special chars etc.
                var newModel = Mapper.Map(model, new Post(), m => new { m.Title, m.Slug, m.ShortDescription, m.Content, m.PostedOn, m.PublishStatus, m.Visibility, m.AuthorId });

                newModel.CreatedOn = DateTime.Now;
                newModel.CreatedBy = AppSession.AppUserId;
                newModel.CommentAllowed = model.NewCommentAllowed;
                
                Db.Posts.Add(newModel);
                await Db.SaveChangesAsync();

                foreach (var pc in model.PostCategories) newModel.Categories.Add(new PostCategory() { PostId = newModel.Id, CategoryId = pc});
                model.PostTags = await AddNewTagsAsync(model.PostTags);
                foreach (var pt in model.PostTags) newModel.Tags.Add(new PostTag() { PostId = newModel.Id, TagId = int.Parse(pt) });
                await Db.SaveChangesAsync();
                
                return RedirectToAction("Index");
            }
            //TempData.Danger("Post was not saved! Please check errors.");

            ViewBag.Title = "Add";
            await SetViewItemsAsync();
            return View(model);
        }
Example #2
0
 public async Task<IActionResult> Add()
 {
     var model = new PostViewModel();
     ViewBag.Title = "Add";
     model.PostedOn = DateTime.Now;
     model.NewCommentAllowed = true;
     await SetViewItemsAsync();
     return View(model);
 }
Example #3
0
        public async Task<IActionResult> Edit(PostViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Can we just update DB without fetching first, need ideas, also concurrency check needs to be done
                var dbModel = await Db.Posts.Include(d => d.Categories).Include(d => d.Tags).FirstAsync(c => c.Id == model.Id);
                model.Slug = model.Slug.ToSlug(); // Remove any special chars etc.
                Mapper.Map(model, dbModel, m => new { m.Title, m.Slug, m.ShortDescription, m.Content, m.PostedOn, m.PublishStatus, m.Visibility, m.AuthorId });
                
                dbModel.ModifiedOn = DateTime.Now;
                dbModel.ModifiedBy = AppSession.AppUserId;
                dbModel.CommentAllowed = model.NewCommentAllowed;

                foreach (var c in dbModel.Categories.Where(c => !model.PostCategories.Any(t => t == c.CategoryId)).ToList())
                    Db.PostCategories.Remove(c);
                foreach (var c in model.PostCategories.Except(dbModel.Categories.Select(s => s.CategoryId).DefaultIfEmpty()))
                    dbModel.Categories.Add(new PostCategory() { PostId = dbModel.Id, CategoryId = c});

                model.PostTags = await AddNewTagsAsync(model.PostTags);
                foreach (var t in dbModel.Tags.Where(c => !model.PostTags.Any(t => t == c.TagId.ToString())).ToList())
                    Db.PostTags.Remove(t);
                foreach (var t in model.PostTags.Except(dbModel.Tags.Select(s => s.TagId.ToString()).DefaultIfEmpty()))
                    dbModel.Tags.Add(new PostTag() { PostId = dbModel.Id, TagId = int.Parse(t) });

                // Need to fix RowStamp check
                //Db.Entry(dbModel).OriginalValues["RowStamp"] = model.RowStamp;
                Db.Update(dbModel);
                await Db.SaveChangesAsync();

                //TempData.Success("Post was successfully saved!");
                return RedirectToAction("Index");
            }
            //TempData.Danger("Post was not saved! Please check errors.");

            ViewBag.Title = "Edit";
            await SetViewItemsAsync();
            return View("Add", model);
        }