public async Task <IActionResult> CreatePost([Bind("Title,Description,Content,Id,AuthorId,CategoryId,CategoryName")] PostDto post, IFormFile uploaded_image) { if (ModelState.IsValid) { // Save image to server string imageName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(uploaded_image.FileName); var path = Path.Combine( Directory.GetCurrentDirectory(), PathConstant.PostThumbnailPath, imageName); if (uploaded_image.Length > 0) { using (var stream = new FileStream(path, FileMode.Create)) { await uploaded_image.CopyToAsync(stream); } } post.Slug = RegexHelper.CreateSlug(post.Title); post.ImagePath = PathConstant.UploadedImagePath + imageName; await postService.AddAsync(post); return(RedirectToAction("PostManage", "Manage")); } return(View("Post/Create", post)); }
public async Task <IActionResult> Create( [FromBody] CreatePostRequest createPostRequest) { if (createPostRequest == null || createPostRequest.Name == null) { return(BadRequest()); } var post = new Post { Name = createPostRequest.Name }; var added = await _postService.AddAsync(post); if (!added) { return(NoContent()); } var uriPath = ApiRoutes.Posts.Get.Replace("{postId}", post.Id.ToString()); var locationUri = HttpHelper.GetFullUriPath(HttpContext.Request, uriPath); var response = new CreatePostResponse { CreatedPostId = post.Id, CustomMessage = "yay!" }; return(Created(locationUri, response)); }
public async Task <ActionResult> AddAsync([FromForm] PostDto postDto) { try { int loginuserId = Convert.ToInt32(User.FindFirst(ClaimTypes.NameIdentifier)?.Value); postDto.UserId = loginuserId; postDto.PostDate = DateTime.Now; var newPost = await _postService.AddAsync(_mapper.Map <Post>(postDto)); if (postDto.image == null) { return(Created(string.Empty, _mapper.Map <PostDto>(newPost))); } else { var resimler = Path.Combine(_environment.WebRootPath, "img"); var imageName = $"{newPost.Id}.jpg"; if (postDto.image.Length > 0) { using (var fileStream = new FileStream(Path.Combine(resimler, imageName), FileMode.Create)) { await postDto.image.CopyToAsync(fileStream); } } newPost.PostImage = imageName; _postService.Update(newPost); return(Created(string.Empty, _mapper.Map <PostDto>(newPost))); } } catch { return(BadRequest()); } }
public async Task <IActionResult> Add(PostAddViewModel postAddViewModel) { if (ModelState.IsValid) { var postAddDto = Mapper.Map <PostAddDto>(postAddViewModel); var imageResult = await ImageHelper.Upload(postAddViewModel.Title, postAddViewModel.ThumbnailFile, PictureType.Post); postAddDto.Thumbnail = imageResult.Data.FullName; var result = await _postService.AddAsync(postAddDto, LoggedInUser.UserName, LoggedInUser.Id); if (result.ResultStatus == ResultStatus.Success) { _toastNotification.AddSuccessToastMessage(result.Message, new ToastrOptions { Title = "Başarılı İşlem!" }); return(RedirectToAction("Index", "Post")); } else { ModelState.AddModelError("", result.Message); } } var categories = await _categoryService.GetAllByNoneDeletedAndActiveAsync(); postAddViewModel.Categories = categories.Data.Categories; return(View(postAddViewModel)); }
public void AddAsync_TitleIsNull_ThrowsNotFoundException() { // Arrange var post = new BlogPost { Id = 1, Description = "This is a test blog" }; _postUnitOfWorkMock.Setup(x => x.PostRepository) .Returns(_postRepositoryMock.Object); // Act Should.Throw <NotFoundException>(() => _postService.AddAsync(post)); // Assert _postRepositoryMock.VerifyAll(); }
public async Task <IActionResult> New(PostAddDto postAddDto) { if (!ModelState.IsValid) { return(View(postAddDto)); } await _postService.AddAsync(postAddDto); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Post([FromBody] PostRequest post) { var pt = _mapper.Map <PostRequest, Post>(post); pt.UserId = HttpContext.GetUserId(); var result = await _postService.AddAsync(pt); if (!result.Success) { return(BadRequest(new ErrorViewModel())); } var ptDTO = _mapper.Map <Post, PostResponse>(result.Resource); return(Ok(new Response <PostResponse>(ptDTO))); }
public async Task <IActionResult> AddAsync([FromBody] CreatePost newPost) { try { var postId = await _postService.AddAsync(User.GetUserId(), newPost.Title, newPost.Content); return(Created($"posts/{postId}", null)); } catch (Exception e) { _logger.LogError($"Returning exception: {e.Message}"); return(Json(new ExceptionDto { Error = e.Message })); } }
public async Task <IActionResult> CreatePost(PostDto postDto, bool isDraft = false) { var isExist = (await _postService.GetOneAsync(i => i.Title == postDto.title)) != null; if (isExist) { return(Json(new ReturnDto { State = "fail", Message = "存在相同标题的文章,请更改标题后重试" })); } var tags = postDto.tags.Split(',', ',').Where(i => i != "").ToList(); await _postService.AddAsync(postDto.title, tags, postDto.context, isDraft); return(Json(new ReturnDto { Message = "ok" })); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } try { await _postService.AddAsync(Post, user); return(RedirectToPage("/Blogs/Index", new { Id = Post.BlogId })); } catch (ValidationException vex) { SetModelError(vex); return(Page()); } }
public async Task <IActionResult> AddPost(CreatePostDto createPostDto) { try { var user = await _userManager.GetUserAsync(User); createPostDto.Author = user; var post = _mapper.Map <Post>(createPostDto); var result = await _postService.AddAsync(post); await _postService.SaveAllAsync(); return(Ok(new { id = result.Entity.Id, userName = result.Entity.Author.UserName, title = result.Entity.Title, content = result.Entity.Content, createdAt = result.Entity.CreatedAt })); } catch (Exception e) { return(BadRequest(e)); } }
public async Task HandlerAsync(CreatePost command) { await _postService.AddAsync(command.Content, command.UserId); }
public async Task <IActionResult> Create(Post post) { var result = await postService.AddAsync(post); return(CreatedAtAction(nameof(Get), new { id = result.PostId }, result)); }