Example #1
0
        public ICommandResult Execute(CreateOrUpdateBillCommand command)
        {
            var bill = new BillModel
            {
                Id               = command.Id,
                BillNumber       = command.BillNumber,
                Amount           = command.Amount,
                BillDate         = command.BillDate,
                Category         = command.Category,
                CreatedBy        = command.CreatedBy,
                CreatedOn        = command.CreatedOn,
                Description      = command.Description,
                DueDate          = command.DueDate,
                IsRecurring      = command.IsRecurring,
                Items            = command.Items,
                LastUpdatedBy    = command.LastUpdatedBy,
                LastUpdatedOn    = command.LastUpdatedOn,
                RecurringSetting = command.RecurringSetting,
                Status           = command.Status,
                Vendor           = command.Vendor
            };

            var entity = ModelFactory.Create(bill);

            if (string.IsNullOrEmpty(command.Id))
            {
                if (entity.BillItems != null)
                {
                    foreach (var item in entity.BillItems)
                    {
                        _billItemRepository.Add(item);
                    }
                }

                _billRepository.Add(entity);
                _unitOfWork.Commit();
            }
            else
            {
                if (entity.BillItems != null)
                {
                    foreach (var item in entity.BillItems)
                    {
                        _billItemRepository.Update(item);
                    }
                }

                _billRepository.Update(entity);
                _unitOfWork.Commit();
            }

            return(new CommandResult(true));
        }
Example #2
0
        /// <summary>
        /// 新增一笔Bill信息
        /// </summary>
        /// <param name="bill"></param>
        /// <returns></returns>
        public IHttpActionResult Post([FromBody] BillModel bill)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (bill == null)
            {
                return(InternalServerError(new ArgumentNullException("Bills 信息为空")));
            }
            var _currentUser = User.Identity.GetUserId();
            var dateNow      = DateTime.UtcNow;

            bill.Id            = Guid.NewGuid().ToString();
            bill.CreatedBy     = User.Identity.GetUserId();
            bill.CreatedOn     = dateNow;
            bill.LastUpdatedOn = dateNow;
            bill.LastUpdatedBy = User.Identity.GetUserId();

            var entity = ModelFactory.Create(bill);

            // remove the navigation object, to stop EF save referenced object. http://msdn.microsoft.com/en-us/magazine/dn166926.aspx
            entity.Category         = null;
            entity.Vendor           = null;
            entity.RecurringSetting = null;

            if (entity.BillItems != null)
            {
                foreach (var item in entity.BillItems)
                {
                    item.BillId = entity.Id;
                    item.Bill   = null;
                    item.Id     = Guid.NewGuid().ToString();
                    _billItemRepo.Add(item);
                }
            }
            _billRepo.Add(entity);
            try
            {
                _unitOfWork.Commit();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                return(InternalServerError(dbEx));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
            return(Created("/bill/" + bill.Id, bill));
        }