예제 #1
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));
 }
예제 #2
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));
        }