private Task CreateNewPost(string message, string link) { DisableGui(); return(Task.Run(async() => { CreatePostResponse createPostReponse = await Connector.CreatePost(message, link); Dispatcher.Invoke(() => { if (createPostReponse.Error == null) { NewPost = createPostReponse.ToPost(); MessageBox.Show( "Publicación creada.", "Éxito", MessageBoxButton.OK, MessageBoxImage.Information ); Close(); } else { MessageBox.Show( $"Se produjo un error al publicar:\n{createPostReponse.Error}", "Error", MessageBoxButton.OK, MessageBoxImage.Error ); } EnableGui(); }); })); }
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 <IActionResult> CreatePost([FromHeader(Name = "X-Account-Id")] Guid creatorId, [FromForm] IFormFile imageFile) { var form = await Request.ReadFormAsync(); CreatePostResponse response = await mediator.Send(new CreatePostRequest(creatorId, form.Files[0])); return(CreatedAtAction(nameof(GetPostById), new { id = response.Id }, response)); }
public async Task <IActionResult> Create(CreatePostCommand command, CancellationToken cancellationToken) { if (!ModelState.IsValid) { return(View(command)); } command.User = UserContext; CreatePostResponse response = await Mediator.Send(command, cancellationToken); return(RedirectToAction("View", new { blogId = response.BlogId, postId = response.PostId })); }
public async Task <IActionResult> Create([FromBody] CreatePostRequest post) { var newPost = new Post(post.Name); var createdPost = await _postServices.CreatePostAsync(newPost); var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var locationUri = baseUrl + "/" + ApiRoutes.Posts.Get.Replace("{postId}", createdPost.Id.ToString()); var postResponse = new CreatePostResponse() { Id = createdPost.Id, Name = post.Name }; return(Created(locationUri, postResponse)); }
public async Task <IActionResult> CreatePost([FromBody] CreatePostRequest request) { var user = this.GetUser(); var post = new Post { Text = request.Text }; await _postService.CreatePostAsync(post, await user); var response = new CreatePostResponse { Code = "success", Message = "create.post.success", Data = post }; return(Ok(response)); }
public async Task <CreatePostResponse> CreatePost(CreatePostRequest req) { var userProfileOwner = await _dbContext.UserProfiles.Include(x => x.User).Where(x => x.User.Id == req.UserId).FirstOrDefaultAsync(); var response = new CreatePostResponse(); if (userProfileOwner != null) { var imagePaths = await _imageService.UploadImages(Enums.ImageTypeEnum.POST_PICTURE, req.PostImages); var mappedPaths = _mapper.Map <ICollection <UserImagePostModel> >(imagePaths); var postModel = new UserPostModel { PostDescription = req.PostDescription, CreationDate = DateTime.UtcNow, Latitude = req.Latitude, Longitude = req.Longitude, Owner = userProfileOwner, Photos = mappedPaths }; response = _mapper.Map <CreatePostResponse>(postModel); await _dbContext.UserPosts.AddAsync(postModel); await _dbContext.SaveChangesAsync(); } return(response); }
public async Task <CreatePostResponse> CreatePostAsync(CreatePostRequest request, string userId) { var entity = _postMapper.ToEntity(request.Post); entity.UserId = userId; var result = await _postRepository.CreatePostAsync(entity); if (!result) { return new CreatePostResponse { StatusCode = (int)HttpStatusCode.Unauthorized } } ; var response = new CreatePostResponse { StatusCode = (int)HttpStatusCode.Created }; return(response); }
public async Task <IActionResult> create([FromBody] CreatePostRequest post) { Post realPost = new Post() { Name = post.Name }; var result = await _postService.CreateAsync(realPost); if (!result) { return(BadRequest()); } CreatePostResponse postResponse = new CreatePostResponse() { Id = realPost.Id, Name = realPost.Name }; var baseUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}"; var location = baseUrl + "/" + ApiRouts.Post.Get.Replace("{postId}", realPost.Id); return(Created(location, postResponse)); }
public CreatePostResponse Handle(CreatePost command) { var response = new CreatePostResponse(); try { var user = _membershipService.GetUserById(command.CreatedByUserId); if (user == null) { response.Error = "Invalid user."; return(response); } if (string.IsNullOrEmpty(command.SubName)) { response.Error = "The sub name is required."; return(response); } var sub = _subService.GetSubByName(command.SubName); if (sub == null) { response.Error = "That sub doesn't exist."; return(response); } // todo: make sure that the user can post into the sub // is admin? is user banned? does user look like spam? // todo: make sure the post type is allowed if (string.IsNullOrEmpty(command.Title)) { response.Error = "The title is required."; return(response); } // remove extrenous white space, and trim whitespace from the edges command.Title = Regex.Replace(command.Title, @"\s+", " ").Trim(); if (command.Title.Length > 100) { response.Error = "The title is too long."; return(response); } if (command.PostType == PostType.Link) { if (string.IsNullOrEmpty(command.Url)) { response.Error = "You must provide a url."; return(response); } // todo: improve url validation command.Url = command.Url.ToLower(); if (!command.Url.Contains("://")) { command.Url = "http://" + command.Url; } Uri uri; try { uri = new Uri(command.Url); } catch (Exception ex) { response.Error = "The url appears to be invalid."; return(response); } switch (uri.Scheme) { case "http": case "https:": break; default: response.Error = "The scheme is invalid for the url."; return(response); } // todo: make sure the domain isn't banned // todo: make sure the url wasn't already submitted } else if (command.PostType == PostType.Text) { // todo: only validate this length is user is not an admin if (!string.IsNullOrEmpty(command.Content) && command.Content.Length > 40000) { response.Error = "The post content is too long (maximum 40000 characters)."; return(response); } } else { throw new Exception("unknown post type " + command.PostType); } var post = new Post(); post.Id = GuidUtil.NewSequentialId(); post.DateCreated = Common.CurrentTime(); post.LastEditDate = null; post.SubName = sub.Name; post.UserName = user.UserName; post.UserIp = command.IpAddress; post.PostType = command.PostType; post.Title = command.Title; if (post.PostType == PostType.Link) { post.Url = command.Url; } else { post.Content = command.Content; } post.SendReplies = command.NotifyReplies; post.Slug = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); while (_postService.GetPostBySlug(post.Slug) != null) { post.Slug = Path.GetFileNameWithoutExtension(Path.GetRandomFileName()); } //_postService.InsertPost(post); response.Title = command.Title; response.Slug = post.Slug; } catch (Exception ex) { // todo: log response.Error = ex.Message; } return(response); }
public IHttpActionResult CreateOrder(OfferViewModel offerViewModel) { try { string username = Utilities.GetUserNameFromRequest(Request); var user = userService.GetUsers().Where(u => u.UserName == username).FirstOrDefault(); Post post = postService.GetPost(offerViewModel.PostId); Offer offer = offerService.GetOffer(offerViewModel.OfferId); if (post == null || offer == null) { return(BadRequest("Not found data")); } CreatePostResponse response = new CreatePostResponse(); if (offerViewModel.Deposit > 0) { if (offerViewModel.Deposit > user.Current_Money) { response.Status_code = "E01"; } else { transactionService.CreateTransaction(new Transaction() { DateCreated = DateTime.Now, Description = "Nạp tiền vào đơn hàng " + post.ProductName, IsSuccess = true, PostId = post.Id, Type = Transaction.TypeTrans.PostIn, IsDelete = false, Value = offerViewModel.Deposit, UserId = user.Id, }); user.Current_Money -= offerViewModel.Deposit; post.Deposit += offerViewModel.Deposit; postService.UpdatePost(post); userService.UpdateUser(user); transactionService.SaveTransaction(); } } //update create Order order = new Order() { IsActive = true, IsDelete = false, BuyerId = user.Id, TravellerId = offerViewModel.TravelerId, Total = post.Price * post.Quantity + offer.ShippingFee, ShippingFee = offer.ShippingFee, DateCreated = DateTime.UtcNow.AddHours(7), DeliveryDate = DateTime.UtcNow.AddHours(7), DateUpdated = DateTime.UtcNow.AddHours(7), Status = (int)OrderType.Transit, OfferId = offer.Id, }; orderService.CreateOrder(order); orderService.SaveOrder(); //update post post.Type = Post.PostType.Transit; // post.Deposit = post.Deposit == null ? offerViewModel.Deposit : (offerViewModel.Deposit + post.Deposit); postService.UpdatePost(post); postService.SavePost(); var traveler = userService.GetUsers().FirstOrDefault(t => t.Id.Equals(offerViewModel.TravelerId)).SignalConnect; if (traveler != null) { notificationService.CreateNotification(new Notification { DateCreated = DateTime.UtcNow.AddHours(7), IsRead = false, Message = "Đề nghị cho đơn hàng " + post.ProductName + " đã được đồng ý", Title = "Chuyến đi " + order.TripId, UserId = offerViewModel.TravelerId }); notificationService.SaveNotification(); HubUtilities.GetHub().Clients.Client(traveler).acceptOffer("Để nghị đã được đồng ý ", offerViewModel.OfferId); } //delete post on redis Utilities.DeleteonRedis(post, post.Id.ToString()); response.Current_Money = user.Current_Money; return(Ok(response)); } catch { return(BadRequest("Error")); } }