public async Task TestInitialize() { DbContextOptions <FlyingDutchmanAirlinesContext> dbContextOptions = new DbContextOptionsBuilder <FlyingDutchmanAirlinesContext>().UseInMemoryDatabase("FlyingDutchman") .Options; _context = new FlyingDutchmanAirlinesContext_Stub(dbContextOptions); Flight flight = new Flight { FlightNumber = 1, Origin = 1, Destination = 2 }; Flight flight2 = new Flight { FlightNumber = 10, Origin = 3, Destination = 4 }; _context.Flight.Add(flight); _context.Flight.Add(flight2); await _context.SaveChangesAsync(); _repository = new FlightRepository(_context); Assert.IsNotNull(_repository); }
public virtual async Task CreateBooking(int customerID, int flightNumber) { if (!customerID.IsPositiveInteger() || !flightNumber.IsPositiveInteger()) { Console.WriteLine($"Argument Exception in CreateBooking! CustomerID = { customerID}, flightNumber = { flightNumber}"); throw new ArgumentException("invalid arguments provided"); } Booking newBooking = new Booking { CustomerId = customerID, FlightNumber = flightNumber }; try { _context.Booking.Add(newBooking); await _context.SaveChangesAsync(); } catch (Exception exception) { Console.WriteLine($"Exception during database query: {exception.Message}"); throw new CouldNotAddBookingToDatabaseException(); } }
public virtual async Task CreateBooking(int customerID, int flightNumber) { // Validate parameters: customerID and flightNumber should never be negative. if (!customerID.IsPositive() || !flightNumber.IsPositive()) { Console.WriteLine($"Argument Exception in CreateBooking! customerID = {customerID}, flightNumber = {flightNumber}."); throw new ArgumentException("Invalid arguments provided."); } // Create new Booking instance using an object initializer Booking newBooking = new Booking { CustomerId = customerID, FlightNumber = flightNumber }; try { _context.Bookings.Add(newBooking); await _context.SaveChangesAsync(); } // General catch for exception catch (Exception exception) { Console.WriteLine($"Exception during database query: {exception.Message}"); throw new CouldNotAddBookingToDatabaseException(); } }
public async Task TestInitialize() { DbContextOptions <FlyingDutchmanAirlinesContext> dbContextOptions = new DbContextOptionsBuilder <FlyingDutchmanAirlinesContext>().UseInMemoryDatabase("FlyingDutchman") .Options; _context = new FlyingDutchmanAirlinesContext_Stub(dbContextOptions); SortedList <string, Airport> airports = new SortedList <string, Airport> { { "GOH", new Airport { AirportId = 0, City = "Nuuk", Iata = "GOH" } }, { "PHX", new Airport { AirportId = 1, City = "Phoenix", Iata = "PHX" } }, { "DDH", new Airport { AirportId = 2, City = "Bennington", Iata = "DDH" } }, { "RDU", new Airport { AirportId = 3, City = "Raleigh-Durham", Iata = "RDU" } } }; _context.Airport.AddRange(airports.Values); await _context.SaveChangesAsync(); _repository = new AirportRepository(_context); Assert.IsNotNull(_repository); }
public async Task TestInitialize() { DbContextOptions <FlyingDutchmanAirlinesContext> dbContextOptions = new DbContextOptionsBuilder <FlyingDutchmanAirlinesContext>().UseInMemoryDatabase("FlyingDutchman").Options; _context = new FlyingDutchmanAirlinesContext(dbContextOptions); Customer testCustomer = new Customer("Linus Torvalds"); _context.Customers.Add(testCustomer); await _context.SaveChangesAsync(); _repository = new CustomerRepository(_context); Assert.IsNotNull(_repository); }
public async Task <bool> CreateCustomer(string name) { if (IsInvalidCustomerName(name)) { return(false); } Customer newCustomer = new Customer(name); using (_context) { _context.Customer.Add(newCustomer); await _context.SaveChangesAsync(); } return(true); }
public async Task <bool> CreateCustomer(string name) { if (string.IsNullOrEmpty(name) || IsInvalidCustomerName(name)) { return(false); } try { Customer customer = new Customer(name); using (_context) { _context.Customers.Add(customer); await _context.SaveChangesAsync(); } } catch { return(false); } return(true); }