vwOrder IService1.AddOrder(vwOrder order) // AddOrder { try { using (VinylRecordsShopEntities context = new VinylRecordsShopEntities()) { if (order.OrderID == 0) { // ZA ADD tblAlbum albumToEdit = (from r in context.tblAlbums where r.AlbumID == order.AlbumID select r).First(); //albumToEdit.GenreID = album.GenreID; //albumToEdit.ArtistID = album.ArtistID; //albumToEdit.Title = album.Title; //albumToEdit.Price = album.Price; albumToEdit.Storage -= order.NumberOfPieces; context.Entry(albumToEdit).State = EntityState.Modified; context.SaveChanges(); tblOrder newOrder = new tblOrder(); newOrder.EmployeeID = order.EmployeeID; newOrder.AlbumID = order.AlbumID; newOrder.CustomerID = order.CustomerID; newOrder.OrderDate = order.OrderDate; newOrder.TotalPrice = order.TotalPrice; newOrder.NumberOfPieces = order.NumberOfPieces; context.tblOrders.Add(newOrder); context.SaveChanges(); order.OrderID = newOrder.OrderID; return(order); } else { // ZA EDIT int numberOfPieces = (int)(from x in context.vwOrders where x.OrderID == order.OrderID select x.NumberOfPieces).First(); tblAlbum albumToEdit = (from r in context.tblAlbums where r.AlbumID == order.AlbumID select r).First(); albumToEdit.Storage = (albumToEdit.Storage + numberOfPieces) - order.NumberOfPieces; context.Entry(albumToEdit).State = EntityState.Modified; context.SaveChanges(); tblOrder orderToEdit = (from r in context.tblOrders where r.OrderID == order.OrderID select r).First(); orderToEdit.EmployeeID = order.EmployeeID; orderToEdit.AlbumID = order.AlbumID; orderToEdit.CustomerID = order.CustomerID; orderToEdit.OrderDate = order.OrderDate; orderToEdit.TotalPrice = order.TotalPrice; orderToEdit.NumberOfPieces = order.NumberOfPieces; //orderToEdit.OrderID = order.OrderID; context.Entry(orderToEdit).State = EntityState.Modified; context.SaveChanges(); return(order); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); return(null); } }
void IService1.DeleteOrder(int orderID) // DeleteOrder { try { using (VinylRecordsShopEntities context = new VinylRecordsShopEntities()) { tblOrder orderToDelete = (from r in context.tblOrders where r.OrderID == orderID select r).First(); context.tblOrders.Remove(orderToDelete); context.SaveChanges(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString()); } }