Ejemplo n.º 1
0
        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;
     }));
 }