public ActionResult Create(PostDto dto) { if (!ModelState.IsValid) { ViewBag.Categories = _getCategories.Execute(new GeneralSearchQuery()); ViewBag.Users = _getUsers.Execute(new GeneralSearchQuery()); ViewBag.Tags = _getTags.Execute(new GeneralSearchQuery()); return(View(dto)); } try { // TODO: Add insert logic here _addPost.Execute(dto); return(RedirectToAction(nameof(Index))); } catch (EntityAlreadyExistsException) { TempData["error"] = "Post with the same title already exists"; return(View()); } catch (NotFoundException) { TempData["error"] = "The title has to be added"; return(View()); } catch { TempData["error"] = "Something went wrong. Please try again."; return(View()); } }
public IActionResult Post([FromBody] AddPostDto dto) { try { _addPostsCommand.Execute(dto); return(StatusCode(201, "Successfully created.")); } catch { return(StatusCode(422, "An error has occurred.")); } }
public IActionResult Post([FromForm] PostDto dto) { try { _addPost.Execute(dto); return(StatusCode(204)); } catch (EntityAlreadyExistsException) { return(StatusCode(422)); } catch (Exception e) { return(StatusCode(500, e)); } }
public ActionResult Post([FromForm] PostImageDto dto) { var ext = Path.GetExtension(dto.Image.FileName); //.jpg etc. if (!FileUpload.AllowedExtensions.Contains(ext)) { return(UnprocessableEntity("Image extension is not allowed.")); } try { var newFileName = Guid.NewGuid().ToString() + "_" + dto.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); dto.Image.CopyTo(new FileStream(filePath, FileMode.Create)); var post = new PostDto { Id = dto.Id, Title = dto.Title, Content = dto.Content, ImagePath = newFileName, CategoryId = dto.CategoryId, UserId = dto.UserId }; try { _addCommand.Execute(post); return(StatusCode(201)); } catch (EntityAlreadyExistsException e) { return(Conflict(e.Message)); } catch (Exception) { return(StatusCode(500, "Server error has occurred.")); } } catch (Exception) { return(StatusCode(500, "Server error has occurred.")); } }
public ActionResult Create([FromForm] PostImageDto dto) { if (!ModelState.IsValid) { return(View()); } var extension = Path.GetExtension(dto.Image.FileName); if (!FileUpload.AllowedExtensions.Contains(extension)) { return(UnprocessableEntity("You must upload image.")); } try { var newFileName = Guid.NewGuid().ToString() + "_" + dto.Image.FileName; var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", newFileName); dto.Image.CopyTo(new FileStream(filePath, FileMode.Create)); var post = new PostDto { Id = dto.Id, Title = dto.Title, Content = dto.Content, ImagePath = newFileName, CategoryId = dto.CategoryId, UserId = dto.UserId }; _addCommand.Execute(post); TempData["success"] = "Post successfully added."; return(RedirectToAction(nameof(Index))); } catch (Exception) { TempData["error"] = "An error has occured."; } return(View()); }
public ActionResult Create(PostDTO collection) { if (!ModelState.IsValid) { return(View(collection)); } try { _addPost.Execute(collection); return(RedirectToAction(nameof(Index))); } catch (EntityExistException) { TempData["error"] = "Post with same name already exist!"; } catch (Exception) { TempData["error"] = "An error has occured!"; } return(View()); }
public ActionResult <IEnumerable <PostDTO> > Post([FromBody] PostDTO dto) { try { _addCommand.Execute(dto); return(Created("/api/posts/" + dto.Id, new PostDTO { Id = dto.Id, Title = dto.Title, Description = dto.Description })); _emailSender.Subject = "Success!"; _emailSender.Body = "Post was successfully created !"; _emailSender.ToEmail = "*****@*****.**"; _emailSender.Send(); } catch { return(StatusCode(500, "An error has occured !!")); } }