public async Task <ActionResult <PostDto> > AddPost(string userId, [FromForm] PostToAddDto postToAdd) { if (Guid.TryParse(userId, out Guid gUserId)) { try { if (await _userService.CheckIfUserExists(gUserId)) { PostDto addedPost = await _postService.AddPostAsync(gUserId, postToAdd); return(CreatedAtRoute("GetPost", new { userId, postId = addedPost.Id }, addedPost)); } else { return(NotFound($"User: {userId} not found.")); } } catch (Exception ex) { _logger.LogError(ex, "Error occured during adding the user post. User id: {user}", userId); return(StatusCode(StatusCodes.Status500InternalServerError)); } } else { return(BadRequest($"{userId} is not valid guid.")); } }
public async Task <IActionResult> Create(HomeModel model) { if (!ModelState.IsValid) { model.UserProfilePicture = await _userService.GetUserProfilePictureAsync(); model.PostLoadMore = _postService.ShouldPostLoadMore(); return(View(nameof(Index))); } var post = new Post { Content = model.Content, CreatedById = await _userService.GetCurrentUserIdAsync(), CreatedOn = DateTime.Now }; await _postService.AddPostAsync(post); await _unitOfWork.CompleteAsync(); if (model.Files != null) { await _photoService.UploadAsync(model.Files, post.Id); await _unitOfWork.CompleteAsync(); } return(RedirectToAction(nameof(Index), "Home")); }
public async Task <IActionResult> NewPost(CreatePostCommand command) { if (!ModelState.IsValid) { ViewBag.ShowMessage = true; ViewBag.Message = "Something went wrong"; return(View()); } try { CreatePostValidation.CommandValidation(command); var admin = await _adminService.GetAdminAsync(); await _postService.AddPostAsync(command, admin); ViewBag.Added = true; return(View()); } catch (InternalSystemException ex) { ViewBag.ShowMessage = true; ViewBag.Message = ex.Message; return(View()); } catch (Exception) { ViewBag.ShowMessage = true; ViewBag.Message = "Something went wrong!"; return(View()); } }
public async Task SaveBookmark(SaveBookmarkRequest request) { var storageUrl = await _blobProvider.UploadFile(Constants.ContainerName, request.FilePath); if (!string.IsNullOrEmpty(storageUrl)) { var result = await _postService.AddPostAsync(new AddPostRequest() { Url = request.Url, BlobUrl = storageUrl }); if (result.IsSuccess) { await _bookmarkService.AddBookmarkAsync(new AddBookmarkRequest() { UserId = request.UserId, PostId = result.Post.Id }); await _capPublisher.PublishAsync(Constants.EmailQueue, new SendEmailRequest() { PostId = result.Post.Id, UserId = request.UserId }); } } }
public async Task <IActionResult> AddPost(Post newPost) { if (!ModelState.IsValid) { return(View(newPost)); } var currentUser = await _userManager.GetUserAsync(User); if (currentUser == null) { return(Challenge()); } newPost.Id = Guid.NewGuid(); var successful = await _postService.AddPostAsync(newPost, currentUser); if (!successful) { return(BadRequest("Could not add new post.")); } else { var photos = HttpContext.Request.Form.Files; foreach (var Image in photos) { if (Image != null && Image.Length > 0) { var file = Image; int nameFolder = 1; var uploads = Path.Combine(_hostEnvironment.WebRootPath, "uploads\\postImages\\" + nameFolder); int countFiles = Directory.GetFiles(uploads, "*", SearchOption.TopDirectoryOnly).Length; while (countFiles > 254) { uploads = Path.Combine(_hostEnvironment.WebRootPath, "uploads\\postImages\\" + (++nameFolder)); } if (file.Length > 0) { var fileName = newPost.Id + ".jpg"; using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await file.CopyToAsync(fileStream); newPost.PostImage = nameFolder; await _postService.UpdatePost(newPost); } } } } } return(RedirectToAction("Index")); }
public async Task <IActionResult> AddPost(Guid userId, [FromBody] NewPostRequest newpost) { var added = await postService.AddPostAsync(userId, newpost); if (added) { return(Ok()); } return(BadRequest()); }
public async Task <IActionResult> Create(Post post, IFormFile uploadImg, string url) { try { if (uploadImg != null) { var path = await FileUploadHelper.UploadFile(uploadImg); post.ImageUrl = path; if (ModelState.IsValid) { post.Date = DateTime.Now; await postService.AddPostAsync(post); return(RedirectToAction("Index")); } } else { if (ModelState.IsValid) { post.Date = DateTime.Now; var path = await ImageLoader.DownloadFile(url); post.ImageUrl = path; await postService.AddPostAsync(post); return(RedirectToAction("Index")); } } } catch (Exception) { TempData["LinkError"] = "Invalid link"; } return(View()); }
public async Task <ActionResult <GetPostDto> > CreatePost(AddPostDto addPostDto) { GetPostDto getPostDto = null; try { getPostDto = await _postService.AddPostAsync(addPostDto); } catch { return(BadRequest()); } return(CreatedAtRoute(nameof(GetPostById), new { id = getPostDto.Id }, getPostDto)); }
public async Task <IActionResult> CreatePost(CreatePostRequest postRequest) { var post = new PostDb { Description = postRequest.Description, Name = postRequest.Name, Created = DateTime.Now, UserId = HttpContext.GetUserId(), Image = Guid.NewGuid() + Path.GetExtension(postRequest.Image.FileName) }; await _postService.AddPostAsync(post); await _fileService.SaveImageAsync(post.Image, postRequest.Image, "postDb"); var locationUrl = _uriService.GetPostUri(post.Id.ToString()); return(Created(locationUrl, new DataResponse <CreatePostResponse>(_mapper.Map <CreatePostResponse>(post)))); }
public async Task <ActionResult <PostDTO> > AddPost(Post post) { await _service.AddPostAsync(post); return(Created("", post)); }
public async Task <IActionResult> AddPost(CreatePostDto dto) { return(Ok(await _postService.AddPostAsync(dto))); }
public async Task <IActionResult> Post([FromBody] PostDto value) { var result = await _postService.AddPostAsync(value); return(Ok(result)); }