예제 #1
0
        public IHttpActionResult CreateOrder(Order order)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (order == null)
            {
                return BadRequest("You must provide an entry of type 'Order', 'NULL' provided.");
            }

            var account = this.Data.Users.GetAll().FirstOrDefault(a => a.Id == order.AccountId);

            if (account == null)
            {
                return BadRequest(string.Format("No account with id {0} found.", order.AccountId));
            }

            var stock = this.Data.Stocks.GetAll().FirstOrDefault(s => s.Id == order.StockId);

            if (stock == null)
            {
                return BadRequest(string.Format("No stock with id {0} found.", order.AccountId));
            }

            order.Account = account;
            order.Stock = stock;

            this.Data.Orders.Add(order);
            this.Data.SaveChanges();

            return Ok(order);
        }
예제 #2
0
        public IHttpActionResult UpdateOrder(int id, Order order)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Order orderToUpdate = this.Data.Orders.Get(id);
            if (orderToUpdate == null)
            {
                return BadRequest(string.Format("Failed to update order. No order with id {0} found.", id));
            }

            // UserID - using Microsoft.AspNet.Identity;
            var userID = User.Identity.GetUserId();

            orderToUpdate.AccountId = order.AccountId;
            orderToUpdate.CreatedOn = order.CreatedOn;
            orderToUpdate.IsExecuted = order.IsExecuted;
            orderToUpdate.Price = order.Price;
            orderToUpdate.Shares = order.Shares;
            orderToUpdate.StockId = order.StockId;

            this.Data.SaveChanges();
            return Ok(order);
        }