public ActionResult Destroy(int micropostId) { MicropostRepository.Delete(micropostId); MicropostRepository.Save(); TempData["success"] = "Micropost deleted"; if (Request.UrlReferrer != null) { return(Redirect(Request.UrlReferrer.ToString())); } else { return(RedirectToRoute("Default")); } }
protected override bool AuthorizeCore(HttpContextBase httpContext) { var authorized = base.AuthorizeCore(httpContext); if (!authorized) { return(false); } var micropostId = Convert.ToInt32(httpContext.Request.Params.Get("micropostId")); var MicropostRepository = new MicropostRepository(httpContext.GetOwinContext().Get <ApplicationDbContext>()); var postUserId = MicropostRepository.FindById(micropostId).User.Id; var userId = httpContext.User.Identity.GetUserId <int>(); return(postUserId == userId); }
public async Task <ActionResult> Create(NewMicropost newMP) { var currentUser = await UserManager.FindByIdAsync(User.Identity.GetUserId <int>()); if (ModelState.IsValid) { _newMPId = MicropostRepository.Create(newMP.TweetContent, currentUser); IFileHandler handler = Backload.FileHandler.Create(); handler.Events.StoreFileRequestStarted += Events_StoreFileRequestStarted; handler.Init(HttpContext.Request); var result = await handler.Execute(); if (handler.FileStatus.Files[0].Success) { var request = HttpContext.Request; var absoluteRoot = request.Url.AbsoluteUri.Replace(request.Url.AbsolutePath, String.Empty); var relativeURL = handler.FileStatus.Files[0].FileUrl.Replace(absoluteRoot, ""); MicropostRepository.AttachPicture(_newMPId, relativeURL); } else { TempData["error"] = handler.FileStatus.Files[0].ErrorMessage; ViewBag.FeedItems = Enumerable.Empty <Micropost>().ToPagedList(1, 25);; ViewBag.MicropostCount = currentUser.Microposts.Count; return(View("../StaticPages/Home", newMP)); } MicropostRepository.Save(); TempData.Add("success", "Micropost created."); return(RedirectToRoute("Default")); } else { ViewBag.FeedItems = Enumerable.Empty <Micropost>().ToPagedList(1, 25);; ViewBag.MicropostCount = currentUser.Microposts.Count; return(View("../StaticPages/Home", newMP)); } }