public HttpResponseMessage Update(HttpRequestMessage request, TransactionViewModel transactionVM) { if (!ModelState.IsValid) { return(request.CreateResponse(HttpStatusCode.BadRequest, ModelState)); //return Json("301"); } else { const double dateDelete = 30.5 * 24 * 60 * 60 * 1000; var dbTransaction = _transactionService.GetById(transactionVM.ID); var currentDate = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; if (transactionVM.TransactionDate == null) { transactionVM.TransactionDate = dbTransaction.TransactionDate; } var transactionDate = transactionVM.TransactionDate.Ticks / TimeSpan.TicksPerMillisecond; bool isValid = (currentDate - transactionDate) > dateDelete; if (isValid) { string message = "Chỉ cho phép chỉnh sửa trong thời hạn 30 ngày"; return(request.CreateResponse(HttpStatusCode.BadRequest, message)); } else { ICollection <TransactionDetail> transactionDetails = transactionVM.TransactionDetails; var responseTransactionDetail = Mapper.Map <IEnumerable <TransactionDetail>, IEnumerable <TransactionDetailViewModel> >(transactionDetails); transactionVM.UpdatedBy = User.Identity.Name; Transaction ts = new Transaction(); dbTransaction.UpdateTransaction(transactionVM); _transactionService.Update(dbTransaction); _transactionService.Save(); foreach (var item in responseTransactionDetail) { item.UpdatedDate = DateTime.Now; item.UpdatedBy = User.Identity.Name; var transactionDetail = new TransactionDetail(); transactionDetail.UpdateTransactionDetail(item); _transactionDetailService.Update(transactionDetail); _transactionDetailService.Save(); } //var responseData = Mapper.Map<Transaction, TransactionViewModel>(dbTransaction); //response = request.CreateResponse(HttpStatusCode.OK, responseData); //update total earn money by username //ApplicationUser user = _userService.getByUserName(User.Identity.Name); //var responseData = Mapper.Map<ApplicationUser, ApplicationUserViewModel>(user); //responseData.TotalEarn = _transactionDetailService.GetTotalEarnMoneyByUsername(user.UserName); return(request.CreateResponse(HttpStatusCode.OK, responseTransactionDetail)); // return Json(dbTransaction.ID); } } }
public HttpResponseMessage Create(HttpRequestMessage request, TransactionViewModel transactionVM) { return(CreateHttpResponse(request, () => { if (!ModelState.IsValid) { return request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } else { var transaction = new Transaction(); //var transactionDetails = transactionVM.TransactionDetails; ICollection <TransactionDetail> transactionDetails = transactionVM.TransactionDetails; var responseData = Mapper.Map <IEnumerable <TransactionDetail>, IEnumerable <TransactionDetailViewModel> >(transactionDetails); //transactionVM.TransactionDetails = new List<TransactionDetail>(); if (transactionVM.UserId == null) { transactionVM.UserId = _userService.getByUserName(User.Identity.Name).Id; } transactionVM.CreatedBy = User.Identity.Name; transaction.UpdateTransaction(transactionVM); _transactionService.Add(transaction); _transactionService.Save(); foreach (var item in responseData) { item.TransactionID = transaction.ID; item.CreatedBy = User.Identity.Name; item.CreatedDate = DateTime.Now; var dbTransactionDetail = new TransactionDetail(); dbTransactionDetail.UpdateTransactionDetail(item); _transactionDetailService.Add(dbTransactionDetail); _transactionDetailService.Save(); } //foreach (var item in transactionDetails) //{ // item.TransactionId = transactionVM.ID; // transactionVM.TransactionDetails.Add(item); //} //foreach (var item in responseData) //{ // var dbTransactionDetail = new TransactionDetail(); // dbTransactionDetail.UpdateTransactionDetail(item); // dbTransactionDetail.TransactionId = item.ID; // //_transactionDetailService.Add(dbTransactionDetail); //} return request.CreateErrorResponse(HttpStatusCode.OK, transaction.ID.ToString()); } })); }
public HttpResponseMessage Create(HttpRequestMessage request, TransactionDetailViewModel transactionDetailVM) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (!ModelState.IsValid) { response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } else { var newTransactionDetail = new TransactionDetail(); newTransactionDetail.UpdateTransactionDetail(transactionDetailVM); _transactionDetailService.Add(newTransactionDetail); _transactionDetailService.Save(); var responseData = Mapper.Map <TransactionDetail, TransactionDetailViewModel>(newTransactionDetail); response = request.CreateResponse(HttpStatusCode.Created, responseData); } return response; })); }