public async Task <PostResponseViewModel> InsertAsync(PostRequestViewModel postViewModel) { var postRequestModel = _mapper.Map <PostRequestModel>(postViewModel); var applicationId = _identityService.GetApplicationId(); var postInsertDapper = new PostInsertDapper() { Id = postRequestModel.Id, ApplicationId = applicationId, UserId = postRequestModel.User.Id, CreatedDate = DateTime.Now, Title = postRequestModel.Title, Liked = postRequestModel.Liked }; Task.WaitAll(new Task[2] { _userRepository.InsertAsync(new UserInsertDapper() { Id = postRequestModel.User.Id, ApplicationId = applicationId, Name = postRequestModel.User.Name }), _postRepository.InsertAsync(postInsertDapper) }); return(new PostResponseViewModel() { Id = postInsertDapper.Id, CreatedDate = postInsertDapper.CreatedDate, Title = postInsertDapper.Title, Liked = postInsertDapper.Liked }); }
public IActionResult Update(PostRequestViewModel model) { if (ModelState.IsValid) { var mustUpdate = _context.Posts.Find(model.Id); if (mustUpdate != null) { mustUpdate.AuthorId = model.AuthorId; mustUpdate.Content = model.Content; mustUpdate.CreatedAt = DateTime.Now; mustUpdate.Status = model.Status; _context.SaveChanges(); return(RedirectToAction(nameof(Index))); } } return(View(model)); }
public IActionResult Create(PostRequestViewModel model) { if (ModelState.IsValid) { var post = new Post() { AuthorId = model.AuthorId, CreatedAt = DateTime.Now, Status = Status.Enabled, Content = model.Content }; _context.Posts.Add(post); _context.SaveChanges(); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public HttpResponseMessage TenderOrders(PostRequestViewModel request) { DateTime entrytDate; var pendingPost = _cacheManager.Get <List <PostedProduct> >(PostKey); var response = new ApiResultViewModel <string>(); if (!pendingPost.Any()) { response.errorMessage = "Transaction could not be completed!. Please refresh your page and try again."; response.errorStatus = true; return(Request.CreateResponse(response)); } if ( !DateTime.TryParseExact(request.entryDate, "dd/MM/yyyy", DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out entrytDate)) { response.errorMessage = "Transaction date is invalid."; response.errorStatus = true; return(Request.CreateResponse(response)); } AddCurrentTime(ref entrytDate); if (entrytDate > DateTime.Now) { response.errorMessage = "Transaction date cannot be in the future."; response.errorStatus = true; return(Request.CreateResponse(response)); } try { using (var uow = _orderSvc.UnitOfWork) { uow.BeginTransaction(); var currentUserId = User.Identity.GetUserId <int>(); var order = new Order { EntryDate = entrytDate, Remark = request.remarks, OrderUId = Guid.NewGuid(), Total = 0, OrderStatus = (int)OrderStatus.POST, CreatedBy_Id = currentUserId }; if (IposConfig.UseMembership) { order.User_Id = IposMembershipService.GetUserId(User.Identity.Name); } _orderSvc.Add(order); foreach (var pt in pendingPost) { AddMilliSecconds(ref entrytDate); if (pt.Quantity <= 0) { continue; } var product = _productSvc.GetProductById(pt.Id); if (product == null) { continue; } //entrytDate = entrytDate.AddMilliseconds(DateTime.Now.Millisecond); var ordDt = new OrderDetail { EntryDate = entrytDate, OrderDetailUId = Guid.NewGuid(), Remarks = pt.Remarks ?? product.Description, Quantiy = pt.Quantity, Price = (product.Price ?? 0) * pt.Quantity, Order_UId = order.OrderUId, Product_Id = product.ProductId, CostPrice = product.CostPrice ?? (product.Price ?? 0), CreatedBy_Id = currentUserId, Discount = 0 }; order.Total += ordDt.Price; product.Quantity -= pt.Quantity; _orderDetailSvc.Add(ordDt); _productSvc.Update(product); } _orderSvc.Update(order); uow.Commit(); _cacheManager.Remove(PostKey); response.message = "Pending transactions was commited successfully."; } } catch (Exception ex) { _logger.Log(ex); #if DEBUG response.errorMessage = ex.Message; response.errorStatus = true; #else response.errorMessage = "Error occured, please contact admin."; response.errorStatus = true; #endif } return(Request.CreateResponse(response)); }
public async Task <ActionResult <PostResponseViewModel> > PostAsync(PostRequestViewModel postRequestViewModel) { var result = await _likeService.InsertAsync(postRequestViewModel); return(CreatedAtAction(nameof(GetAsync), new { PostId = result.Id, UserId = postRequestViewModel.User.Id }, result)); }