<commit_before>﻿namespace Finance.Service.Controllers
{
    using System.Web.Http;
    using Microsoft.AspNet.Identity;

    using Finance.Data;
    using Finance.Models;

    public class OrderController : ApiController
    {
        private IFinanceData data;

        public OrderController()
            : this(new FinanceData())
        {
        }

        public OrderController(IFinanceData data)
        {
            this.data = data;
        }

        [HttpGet]
        public IHttpActionResult AllOrders()
        {
            var allOrders = this.data.Orders.GetAll();
            return Ok(allOrders);
        }

        [HttpGet]
        public IHttpActionResult ById(int id)
        {
            var orderToShow = this.data.Orders.Get(id);
            return Ok(orderToShow);
        }


        [HttpPost]
        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.");
            }

            // TODO: Fix it!
            this.data.Orders.Add(order); // throwing an exception {"The property 'StockId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type."}
            this.data.SaveChanges();

            return Ok(order);
        }

        [HttpPut]
        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);
        }

        [HttpDelete]
        public IHttpActionResult DeleteOrder(int id)
        {
            var orderToDelete = this.data.Orders.Get(id);

            if (orderToDelete == null)
            {
                return BadRequest(string.Format("No order with id {0} found to be deleted", id));
            }

            this.data.Orders.Delete(orderToDelete);
            this.data.SaveChanges();

            return Ok();
        }
    }
}
<commit_msg>Edited Creation of Orders in the Orders controller<commit_after>﻿namespace Finance.Service.Controllers
{
    using System.Web.Http;
    using System.Linq;
    using Microsoft.AspNet.Identity;

    using Finance.Data;
    using Finance.Models;

    public class OrderController : ApiController
    {
        private IFinanceData data;

        public OrderController()
            : this(new FinanceData())
        {
        }

        public OrderController(IFinanceData data)
        {
            this.data = data;
        }

        [HttpGet]
        public IHttpActionResult AllOrders()
        {
            var allOrders = this.data.Orders.GetAll();
            return Ok(allOrders);
        }

        [HttpGet]
        public IHttpActionResult ById(int id)
        {
            var orderToShow = this.data.Orders.Get(id);
            return Ok(orderToShow);
        }


        [HttpPost]
        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.Accounts.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);
        }

        [HttpPut]
        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);
        }

        [HttpDelete]
        public IHttpActionResult DeleteOrder(int id)
        {
            var orderToDelete = this.data.Orders.Get(id);

            if (orderToDelete == null)
            {
                return BadRequest(string.Format("No order with id {0} found to be deleted", id));
            }

            this.data.Orders.Delete(orderToDelete);
            this.data.SaveChanges();

            return Ok();
        }
    }
}

{'fraction_non_alphanumeric': 0.09125944196084476, 'fraction_numerical': 0.0009249267766301834, 'mean_word_length': 1.5264797507788161, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319024', 'n_tokens_mistral': 1723, 'n_tokens_neox': 1640, 'n_words': 391}