예제 #1
0
        public async Task <IHttpActionResult> PostOrder(OrderViewModel order)
        {
            Order dbOrder = new Order();

            dbOrder.Id         = Guid.NewGuid();
            dbOrder.CustomerId = order.OrderInfo.CustomerId;
            Decimal num1 = new Decimal();

            foreach (EBillAppTest.Models.Item obj in order.Items)
            {
                EBillAppTest.Models.Item item = obj;
                db.OrderItems.Add(new OrderItem()
                {
                    Id       = Guid.NewGuid(),
                    OrderId  = dbOrder.Id,
                    ItemId   = item.Id,
                    Quantity = new int?(item.Quantity)
                });
                var itemType = db.ItemTypes.SingleOrDefault(it => it.ItemId == item.Id && it.TypeId == (int)item.Type);
                if (itemType != null)
                {
                    num1 += itemType.Amount * (Decimal)item.Quantity;
                }
            }
            dbOrder.Amount      = num1;
            dbOrder.OrderNumber = "OD" + string.Format("{0:d9}", (object)(DateTime.Now.Ticks / 10L % 1000000000L));
            db.Orders.Add(dbOrder);
            try
            {
                int num2 = await db.SaveChangesAsync();
            }
            catch (DbUpdateException ex)
            {
                if (OrderExists(dbOrder.Id))
                {
                    return(Conflict());
                }
                throw;
            }
            var    customer = db.Customers.SingleOrDefault(cust => cust.Id == dbOrder.CustomerId);
            string email    = customer.Email;
            string from     = "*****@*****.**";
            await EmailHelper.SendEmailAsync(EmailContentHelper.GenerateContent(TableBuilder.BuildTable(order), dbOrder.OrderNumber, dbOrder.Amount, customer.FirstName + " " + customer.LastName), from, email, dbOrder.OrderNumber);

            return(Ok(new
            {
                status = "success"
            }));
        }