public IActionResult AddPost() { try { ViewBag.History = "/Home"; var claims = HttpContext.User.Claims; var authorName = claims.Where(c => c.Type == ClaimTypes.Name) .Select(c => c.Value) .SingleOrDefault(); var authorID = -1; Int32.TryParse(claims.Where(c => c.Type == "AuthorID") .Select(c => c.Value) .SingleOrDefault(), out authorID); var author = new Author(authorName, authorID); var post = new Post("", author, ""); var pmBuilder = new PostModelBuilder(post); return(View("AddPost", pmBuilder.build())); } catch (Exception e) { var errorMessage = new ErrorPageModel("Eror adding post.", "We couldn't add the post."); ViewBag.History = "/Home"; Console.WriteLine(e.ToString()); return(RedirectToAction("Index", "NullPost", errorMessage)); } }
public async Task <IActionResult> ViewSinglePost(String postid) { var postResult = _postDataAccess.GetPostById(Guid.Parse(postid)); if (postResult == null) { ViewBag.History = "/Home"; var errorMessage = new ErrorPageModel("Post Does Not Exist", "This post does not exist."); return(ShowError(errorMessage)); } ViewBag.History = Request.Headers["Referer"].ToString(); var pmBuilder = new PostModelBuilder(postResult); var claims = HttpContext.User.Claims; var userAuthorId = -1; Int32.TryParse(claims.Where(c => c.Type == "AuthorID") .Select(c => c.Value) .SingleOrDefault(), out userAuthorId); var postAuthorId = postResult.Author.ID; var hasEditPowers = (await _authorization.AuthorizeAsync(User, "BlogEditor")).Succeeded; if (userAuthorId == postAuthorId || hasEditPowers) { return(View("ViewSinglePost", pmBuilder.build())); } return(View("ViewOnlySinglePost", pmBuilder.build())); }
public IActionResult AddPostResult(PostModel post) { try { var claims = HttpContext.User.Claims; post.AuthorName = claims.Where(c => c.Type == ClaimTypes.Name) .Select(c => c.Value) .SingleOrDefault(); var authorID = -1; Int32.TryParse(claims.Where(c => c.Type == "AuthorID") .Select(c => c.Value) .SingleOrDefault(), out authorID); post.AuthorID = authorID; var postBuilder = new PostBuilder(post); var postToAdd = postBuilder.build(); var postResult = _postDataAccess.AddPost(postToAdd); ViewBag.History = "/Home"; var pmBuilder = new PostModelBuilder(postResult); return(View("ViewSinglePost", pmBuilder.build())); } catch (ArgumentException e) { var errorMessage = new ErrorPageModel("Cannot add post.", "The post had empty properties."); Console.WriteLine(e.ToString()); return(ShowError(errorMessage)); } }
public async Task <IActionResult> EditPostResult(PostModel post) { var userCanEdit = await HasEditPowers(); if (IsUserPostAuthor(post.AuthorID) || userCanEdit) { try { ViewBag.History = "/Home/ViewAll"; var postBuilder = new PostBuilder(post); var postToAdd = postBuilder.build(); var postResult = _postDataAccess.EditPost(postToAdd); var pmBuilder = new PostModelBuilder(postResult); return(View("ViewSinglePost", pmBuilder.build())); } catch (ArgumentException e) { ErrorPageModel errorMessage = new ErrorPageModel("Invalid Post.", "The post contained invalid input."); Console.WriteLine(e.ToString()); return(ShowError(errorMessage)); } } else { var errorMessage = new ErrorPageModel("Permission Denied", "You do not have permission to edit this post."); return(ShowError(errorMessage)); } }
public IActionResult ViewByAuthor(int authorID) { ViewBag.History = "/Home/Authors"; var listOfPostsByAuthor = _postDataAccess.GetListOfPostsByAuthorID(authorID); var listOfPostModels = new List <PostModel>(); foreach (Post p in listOfPostsByAuthor) { var pmBuilder = new PostModelBuilder(p); listOfPostModels.Add(pmBuilder.build()); } return(View("ViewAll", listOfPostModels)); }
public IActionResult ViewAll() { ViewBag.History = "/Home"; List <PostModel> postResult = _postDataAccess.GetAllPosts().ConvertAll <PostModel>((p) => { var pmBuilder = new PostModelBuilder(p); return(pmBuilder.build()); }); if (postResult == null) { var errorMessage = new ErrorPageModel("No Posts", "There are no posts."); return(ShowError(errorMessage)); } return(View(postResult)); }
public IActionResult EditPost(String postid) { var postResult = _postDataAccess.GetPostById(Guid.Parse(postid)); if (postResult == null) { var errorMessage = new ErrorPageModel("Invalid Post.", "We couldn't find the post. :("); ViewBag.History = "/Home/ViewAll"; return(ShowError(errorMessage)); } ViewBag.History = "/Home/ViewSinglePost?postid=" + postid; var postModelBuilder = new PostModelBuilder(postResult); var postToEdit = postModelBuilder.build(); return(View("EditPost", postToEdit)); }
public IActionResult SearchResult(SearchCriteria searchCriteria) { ViewBag.History = "/Home/"; if (String.IsNullOrEmpty(searchCriteria.SearchString)) { return(RedirectToAction("ViewAll")); } List <PostModel> results = _postDataAccess.SearchBy((post) => { return(post.Title.IndexOf(searchCriteria.SearchString, StringComparison.OrdinalIgnoreCase) != -1 || post.Author.Name.IndexOf(searchCriteria.SearchString, StringComparison.OrdinalIgnoreCase) != -1 || post.Body.IndexOf(searchCriteria.SearchString, StringComparison.OrdinalIgnoreCase) != -1); } ).ConvertAll <PostModel>((p) => { var pmBuilder = new PostModelBuilder(p); return(pmBuilder.build()); }); return(View("ViewAll", results)); }