public async Task <IActionResult> Blog(Blog model) { //Check if the model is valid if (ModelState.IsValid) { if (model.Content.Contains("<script>")) { return(CustomBadRequest($"Content : {ErrorMessages.JavaScriptNotAllowed}")); } //Try to get the user id if (Guid.TryParse(HttpContext.User.GetClaimValue(ClaimTypes.NameIdentifier), out Guid id)) { //if the blog is new then set the author id if (model.Id == Guid.Empty) { model.AutherId = id; } model.LastEditUserId = id; } //Try to add of update the blog var blog = await HealthBlogService.AddOrUpdateBlog(model); //Check if the operation was succesfull if (blog != null) { return(Ok(CreateResponseBlogJSON(blog))); } return(InternalServerError()); } return(CustomBadRequest(ModelState.GetValidationErrors())); }
public async Task <IActionResult> DeleteBlog(string id) { //Check if we got an id if (Guid.TryParse(id, out Guid blogId)) { var result = await HealthBlogService.DeleteBlog(blogId); return(result ? Ok() : InternalServerError()); } return(CustomBadRequest()); }
public async Task <IActionResult> GetBlogs(int start, int count, bool loadImages = false) { //Try to get the list of saved blogs in the db var blogs = await HealthBlogService.GetBlogs(start, count, loadImages); //Check if we got any data if (blogs != null) { return(Ok(blogs.Select(item => CreateResponseBlogJSON(item, loadImages)))); } return(InternalServerError()); }
public async Task <IActionResult> GetBlog(string id) { //Try to read the blog id if (Guid.TryParse(id, out Guid blogId)) { //Try to get the blog from teh db var blog = await HealthBlogService.GetBlog(blogId, !HttpContext.User.Identity.IsAuthenticated); if (blog != null) { return(Ok(blog)); } } return(NotFound($"Blog not found with the id of {id}")); }
public async Task <IActionResult> Blog(string id) { //Try to read the blog id if (Guid.TryParse(id, out Guid blogId)) { //Try to get the blog from teh db var blog = await HealthBlogService.GetBlog(blogId, (!User.IsInRole(UserRoles.ADMIN))); if (blog != null) { return(View(new HealthBlogViewModel() { Blog = blog })); } } return(View(StaticViewNames.NOTFOUND)); }