Пример #1
0
        public IActionResult Edit(long id)
        {
            var model = new PostCreationViewModel();
            var post  = _unitOfWork.PostRepository.Get(id);

            if (post != null)
            {
                _mapper.Map(post, model);
                return(View(model));
            }
            return(View("Error"));
        }
Пример #2
0
        public ActionResult Create(PostCreationViewModel creation)
        {
            // TODO: remove this fake data ...
            //string text = "Fake post text, Remember remove when ajax works !";

            var owner = _postsManagementService.UnitOfWork.Users.FindById(creation.OwnerId);
            var post  = new Post(_userProvider.GetCurrentUser(_postsManagementService.UnitOfWork, HttpContext),
                                 owner,
                                 DependencyResolver.Current.GetService <IDateTimeService>().Now(),
                                 creation.Text);

            _postsManagementService.UnitOfWork.Posts.Add(post);
            _postsManagementService.UnitOfWork.Commit();


// ReSharper disable Mvc.InvalidModelType
            return(PartialView("_PostPartial", post));
// ReSharper restore Mvc.InvalidModelType
        }
Пример #3
0
 public IActionResult Edit(PostCreationViewModel model) //Checking is edited post is valid and saving it to database
 {
     if (model != null)
     {
         if (ModelState.IsValid)
         {
             DAL.Models.Post entity = _unitOfWork.PostRepository.Get(model.Id);
             if (entity == null)
             {
                 return(View("Error"));
             }
             _mapper.Map(model, entity);
             _unitOfWork.PostRepository.Update(entity);
             _unitOfWork.Commit();
             return(RedirectToAction("Post", new { id = model.Id })); //Opening edited post
         }
     }
     return(View(model));
 }
Пример #4
0
        public IActionResult Create(PostCreationViewModel model) //Checking is new post is valid and saving it to database
        {
            if (model != null)
            {
                model.AuthorId = _userManager.GetUserId(User); //Setting current user as author

                if (ModelState.IsValid)
                {
                    var entity = new DAL.Models.Post();
                    _mapper.Map(model, entity);
                    entity.Created = DateTime.Now;
                    entity.Image   = model.Image;
                    _unitOfWork.PostRepository.Add(entity);
                    int x = _unitOfWork.Commit();
                    return(RedirectToAction("Index", "Home"));
                }
            }
            var errors = ModelState.Select(x => x.Value.Errors)
                         .Where(y => y.Count > 0)
                         .ToList();

            return(View(model));
        }