public void Test_Add_ListAll() { List<Orderline> orderlines = new List<Orderline>(); Orderline OL1 = new Orderline() { OrderId = 2, MovieId = 6, Amount = 6, Price = 30 }; Orderline Ol2 = new Orderline() { OrderId = 2, MovieId = 17, Amount = 8, Price = 30 }; orderlines.Add(Ol2); orderlines.Add(OL1); CustomerRepository custrepo = new CustomerRepository(); Customer cust = custrepo.Get(3); Status stat = new Status() { Id = 1, Name = "Shipped" }; Order ord = new Order() { Id = 2, Orderlines = orderlines, Customer = cust, OrderDate = DateTime.Now.Date, Status = stat }; ord.Id = 2; ord.Orderlines = orderlines; ord.Customer = cust; ord.OrderDate = DateTime.Now.Date; ord.Status = stat; OrderRepository ordrepo= new OrderRepository(); int numberOfOrders = ordrepo.GetAll().Count(); Order result1 = ordrepo.Add(ord); Assert.NotNull(result1); Order result2 = ordrepo.Get(result1.Id); Assert.NotNull(result2); Assert.AreEqual(result1.Id, result2.Id); int finalNumberOfOrders = ordrepo.GetAll().Count(); Assert.AreEqual(numberOfOrders + 1, finalNumberOfOrders); }
/// <summary> /// Adds an order to the database and returns it. /// </summary> /// <param name="ord">the order to be added</param> public Order Add(Order ord) { using (var ctx = new MovieShopContext()) { foreach (Orderline item in ord.Orderlines) { bool isDetached = ctx.Entry(item.Movie).State == EntityState.Detached; if (isDetached) ctx.Movies.Attach(item.Movie); ctx.Entry(item.Movie.Genre).State = EntityState.Detached; } GenreRepository genreRep = new GenreRepository(); foreach (var item in genreRep.GetAll()) { ctx.Genres.Attach(item); } foreach (var item in ctx.Status.ToList()) { if (item.Name.Equals("Processing")) ord.Status = item; } Order newOrd = ctx.Orders.Add(ord); ctx.SaveChanges(); return newOrd; } }
public HttpResponseMessage PutOrder(Order order) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } Order newOrder = orderRepository.Update(order); return Request.CreateResponse(HttpStatusCode.OK, newOrder); }
public IHttpActionResult PostOrder(Order order) { if (!ModelState.IsValid) { return BadRequest(ModelState); } orderRepository.Add(order); return CreatedAtRoute("DefaultApi", new { id = order.Id }, order); }
public bool ChangeActive(Order item) { throw new System.NotImplementedException(); }
/// <summary> /// Update one order /// </summary> public Order Update(Order ord) { using (var ctx = new MovieShopContext()) { //gets the item that we want to update var order = ctx.Orders.Include("Orderlines").Include("Status").FirstOrDefault(c => c.Id == ord.Id); //changes the data order.Orderlines = ord.Orderlines; order.OrderDate = ord.OrderDate; //saves the changes. ctx.SaveChanges(); return order; } }