Ejemplo n.º 1
0
        public ActionResult Update(UpdateNewsModel model)
        {
            var currentUser = System.Web.HttpContext.Current.GetOwinContext()
                              .GetUserManager <ApplicationUserManager>()
                              .FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());

            using (var context = ApplicationDbContext.Create())
            {
                var news = context.News.SingleOrDefault(x => x.Id == model.Id);

                if (news == null)
                {
                    throw new ArgumentException($"News Id: {model.Id} does not exist");
                }

                if (currentUser.Id != news.AuthorId && !System.Web.HttpContext.Current.User.IsInRole("Admin"))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.Forbidden));
                }

                news.Title   = model.Title;
                news.Content = model.Content;

                context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 2
0
 public static UpdateNewsRequestModel MapUpdateNewsModel(UpdateNewsModel model)
 {
     return(new UpdateNewsRequestModel
     {
         Id = model.Id,
         Title = model.Title,
         Description = model.Description,
         ImageLink = model.ImageLink,
     });
 }
Ejemplo n.º 3
0
        public async Task <bool> UpdateNewsRequestAsync(UpdateNewsModel model, string endPoint, string token)
        {
            var apiRequestModel = AuthenticationMapper.MapToApiRequestModel(endPoint, token);

            var requestModel = NewsMapper.MapUpdateNewsModel(model);

            var result = await _newsClient.UpdateNewsRequestAsync(apiRequestModel, requestModel);

            if (result == null)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public ActionResult Update(int id)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var news = context.News.SingleOrDefault(x => x.Id == id);

                if (news == null)
                {
                    throw new ArgumentException($"News Id: {id} does not exist");
                }

                var newsModel = new UpdateNewsModel()
                {
                    Id      = news.Id,
                    Title   = news.Title,
                    Content = news.Content
                };

                return(View(newsModel));
            }
        }