public ValidationResult PlanTrip(Trip trip, Order secondaryOrder) { if (trip == null || secondaryOrder == null) { return(ValidationResult.Error("Existující jízda a/nebo zakázka nebyla vybrána.")); } if (trip.SecondaryOrder != null) { return(ValidationResult.Error("Vybraná jízda již má naplánovanou objednávku pro zpáteční cestu.")); } using (var context = new ReturnFreightContext()) { context.Orders.Attach(secondaryOrder); context.Trips.Attach(trip); // Is trip with this order already in DB? var validationResult = ValidateOrderNotPlannedYet(secondaryOrder, context); if (!validationResult.IsValid) { return(validationResult); } trip.SecondaryOrder = secondaryOrder; return(TrySaveAndNotify(context, trip)); } }
private static void InitializeOrders(ReturnFreightContext context) { context.Orders.Add(new Order { Customer = _customer, DeliveryDeadline = DateTime.Now.AddDays(30), PickupDate = DateTime.Now.AddDays(7), From = "Pardubice", To = "Brno" }); context.Orders.Add(new Order { Customer = _customer, DeliveryDeadline = DateTime.Now.AddDays(60), PickupDate = DateTime.Now.AddDays(35), From = "Brno", To = "Pardubice" }); context.Orders.Add(new Order { Customer = _customer, DeliveryDeadline = DateTime.Now.AddDays(60), PickupDate = DateTime.Now.AddDays(32), From = "Hradec Králové", To = "Pardubice" }); }
private void Window_Initialized(object sender, EventArgs e) { using (var context = new ReturnFreightContext()) { context.Users.Load(); context.Orders.Load(); dgOrders.ItemsSource = context.Orders.Local; } }
public static void DeleteTestInovice(Invoice invoice) { using (var context = new ReturnFreightContext()) { context.Invoices.Attach(invoice); context.Invoices.Remove(invoice); context.TrySaveChanges(); } }
private static ValidationResult ValidateOrderNotPlannedYet(Order order, ReturnFreightContext context) { var count = context.Trips.Count(trip => trip.PrimaryOrder.Id == order.Id || trip.SecondaryOrder.Id == order.Id); return(count == 0 ? ValidationResult.Ok() : ValidationResult.Error("Pro zadanou objednávku je již jízda naplánovaná.")); }
private Trip GetTripByOrder(Order order) { using (var context = new ReturnFreightContext()) { return (context.Trips.SingleOrDefault( trip => trip.PrimaryOrder.Id == order.Id || trip.SecondaryOrder.Id == order.Id)); } }
private static void InitializeUsers(ReturnFreightContext context) { using (SHA512 sha = new SHA512Managed()) { _customer = CreateUser("customer", sha, "pass", UserRole.Customer); context.Users.Add(_customer); context.Users.Add(CreateUser("accountant", sha, "pass", UserRole.Accountant)); context.Users.Add(CreateUser("planner", sha, "pass", UserRole.Planner)); } }
private ValidationResult TrySaveAndNotify(ReturnFreightContext context, Trip newTrip) { var savedChanges = context.TrySaveChanges(); if (savedChanges.IsValid) { NotifyObservers(newTrip); } return(savedChanges); }
public static void DeleteTestOrder(Order o) { if (o != null) { using (var context = new ReturnFreightContext()) { context.Orders.Attach(o); context.Orders.Remove(o); context.TrySaveChanges(); } } }
public void RegisterOrderNotPersistedUser() { var order = new Order(new User("newuser", "password", UserRole.Planner), DateTime.Now.AddDays(2), "a", DateTime.Now.AddDays(1), "b"); _orderController.RegisterOrder(order); using (var context = new ReturnFreightContext()) { var count = context.Users.Count(user => user.UserName == "newuser" && user.UserRole == UserRole.Planner); Assert.AreEqual(0, count); } }
public static Order CreateTestOrder(DateTime deadline, DateTime pickupDate, string from = "from", string to = "to") { var order = new Order(Customer, deadline, from, pickupDate, to); using (var context = new ReturnFreightContext()) { context.Users.Attach(order.Customer); context.Orders.Add(order); context.TrySaveChanges(); } return(order); }
public ValidationResult RegisterInvoice(Invoice invoice) { var validationResult = Validate(invoice); if (!validationResult.IsValid) { return(validationResult); } using (var context = new ReturnFreightContext()) { context.Orders.Attach(invoice.Order); context.Invoices.Add(invoice); return(context.TrySaveChanges()); } }
public ValidationResult RegisterOrder(Order order) { var validationResult = Validate(order); if (!validationResult.IsValid) { return(validationResult); } using (var context = new ReturnFreightContext()) { context.Users.Attach(order.Customer); context.Orders.Add(order); return(context.TrySaveChanges()); } }
public void DeleteTestOrder() { //remove all created trips using (var context = new ReturnFreightContext()) { var createdTrips = context.Trips.Where( t => t.PrimaryOrder.Id == _primaryOrder.Id || t.PrimaryOrder.Id == _secondaryOrder.Id || t.SecondaryOrder.Id == _primaryOrder.Id || t.SecondaryOrder.Id == _secondaryOrder.Id).ToList(); context.Trips.RemoveRange(createdTrips); context.TrySaveChanges(); } TestUtils.DeleteTestOrder(_primaryOrder); TestUtils.DeleteTestOrder(_secondaryOrder); }
private void LoadData() { using (var context = new ReturnFreightContext()) { context.Users.Load(); context.Orders.Load(); context.Trips.Load(); var orders = context.Orders.SqlQuery( "select o.* from Orders o where o.Id not in (select ISNULL(PrimaryOrder_Id, 0) from Trips union all select ISNULL(SecondaryOrder_Id, 0) from Trips)") .ToList(); dgOrders.ItemsSource = orders; comboTrips.ItemsSource = context.Trips.Where(trip => trip.SecondaryOrder == null).ToList(); dgTrips.ItemsSource = context.Trips.Local; } }
private static void Main(string[] args) { Console.WriteLine("Loading initial data..."); Database.SetInitializer(new DropCreateDatabaseAlways <ReturnFreightContext>()); using (var context = new ReturnFreightContext()) { Console.WriteLine("Initializing users..."); InitializeUsers(context); Console.WriteLine("Done initializing users."); Console.WriteLine("Initializing dummy orders..."); InitializeOrders(context); Console.WriteLine("Done initializing dummy orders."); Console.WriteLine("Saving to DB..."); context.SaveChanges(); } Console.WriteLine("Done loading initial data."); }
public void Login(string username, string password) { using (var context = new ReturnFreightContext()) { using (SHA512 sha = new SHA512Managed()) { var encoding = Encoding.UTF8; var hash = encoding.GetString(sha.ComputeHash(encoding.GetBytes(password))); var foundUser = context.Users.FirstOrDefault(user => user.UserName == username && user.Password == hash); if (foundUser != null) { LoggedUser = foundUser; } } } }
public ValidationResult PlanTrip(Order primaryOrder) { if (primaryOrder == null) { return(ValidationResult.Error("Zakázka nebyla vybrána.")); } using (var context = new ReturnFreightContext()) { context.Orders.Attach(primaryOrder); // Is trip with this order already in DB? var validationResult = ValidateOrderNotPlannedYet(primaryOrder, context); if (!validationResult.IsValid) { return(validationResult); } // Add a new trip var newTrip = new Trip(primaryOrder); context.Trips.Add(newTrip); return(TrySaveAndNotify(context, newTrip)); } }