예제 #1
0
 public List <Reservation> GetAllReservation()
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         return(context.Reservations.Include(r => r.Customer).Include(r => r.Car).ToList());
     }
 }
예제 #2
0
 public T GetElement <T>(Func <T, bool> predicateFunc) where T : class
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         return(context.Set <T>().SingleOrDefault(predicateFunc));
     }
 }
예제 #3
0
        public async Task <Reservation> Insert(Reservation reservation)
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                if (!DateRangeCheck(reservation))
                {
                    throw new InvalidDateRangeException(
                              $"Reservation < 24h -> {(reservation.From - reservation.To).TotalHours}h");
                }

                if (!await AvailabilityCheck(reservation))
                {
                    throw new CarUnavailableException($"car: {reservation.CarId} unavailable");
                }

                context.Entry(reservation).State = EntityState.Added;
                await context.SaveChangesAsync();

                reservation.Car = await context.Cars.FindAsync(reservation.CarId);

                await context.Entry(reservation).Reference(r => r.Customer).LoadAsync();

                return(reservation);
            }
        }
예제 #4
0
 public Reservation GetReservationById(int id)
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         return(context.Reservations.Include(r => r.Customer).Include(r => r.Car).SingleOrDefault(r => r.ReservationNo == id));
     }
 }
예제 #5
0
 public List <T> GetAll <T>() where T : class
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         return(context.Set <T>().ToList());
     }
 }
예제 #6
0
        public T SaveObject <T>(T obj, int id, bool isNew) where T : class
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                context.Database.Log = Console.Write;
                try
                {
                    context.Entry <T>(obj).State = isNew ? EntityState.Added : EntityState.Modified;
                    context.SaveChanges();
                    if (!isNew && typeof(T).Name == "Car")
                    {
                        context.Database.ExecuteSqlCommand("UPDATE cars SET CarClass = {0} WHERE Id = {1}",
                                                           obj.GetType().Name == "StandardCar" ? 2: (obj.GetType().Name == "MidRangeCar"? 1 : 0), id);
                        context.SaveChanges();
                    }

                    return(obj);
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    ex.Entries.SingleOrDefault().Reload();
                    throw CreateLocalOptimisticConcurrencyException(context, obj);
                }
            }
        }
예제 #7
0
 public async Task Delete(Customer customer)
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         context.Entry(customer).State = EntityState.Deleted;
         await context.SaveChangesAsync();
     }
 }
예제 #8
0
 public void DeleteObject <T>(T obj) where T : class
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         context.Entry <T>(obj).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
예제 #9
0
        protected static OptimisticConcurrencyException <T> CreateOptimisticConcurrencyException <T>(
            CarReservationContext context, T entity)
            where T : class
        {
            T dbEntity = (T)context.Entry(entity)
                         .GetDatabaseValues()
                         .ToObject();

            return(new OptimisticConcurrencyException <T>($"Update {typeof(T).Name}: Concurrency-Fehler", dbEntity));
        }
예제 #10
0
        public async Task <Customer> Insert(Customer customer)
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                context.Entry(customer).State = EntityState.Added;
                await context.SaveChangesAsync();

                return(customer);
            }
        }
예제 #11
0
 public async Task <Customer> Get(int primaryKey)
 {
     using CarReservationContext context = new CarReservationContext();
     try
     {
         return(await context.Customers.Include(c => c.Reservations).SingleAsync(c => c.Id == primaryKey));
     }
     catch (Exception ex)
     {
         throw new KeyNotFoundException(ex.Message, ex);
     }
 }
예제 #12
0
 public async Task <Reservation> Get(int primaryKey)
 {
     try
     {
         using CarReservationContext context = new CarReservationContext();
         return(await context.Reservations.Include(r => r.Car).Include(r => r.Customer)
                .SingleAsync(c => c.ReservationNr == primaryKey));
     }
     catch (Exception ex)
     {
         throw new KeyNotFoundException(ex.Message, ex);
     }
 }
예제 #13
0
 public async Task Update(Customer customer)
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         try
         {
             context.Entry(customer).State = EntityState.Modified;
             await context.SaveChangesAsync();
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
             throw CreateOptimisticConcurrencyException(context, customer);
         }
     }
 }
예제 #14
0
        public async Task Update(Reservation reservation)
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                if (!DateRangeCheck(reservation))
                {
                    throw new InvalidDateRangeException(
                              $"Reservation < 24h -> {(reservation.From - reservation.To).TotalHours}h");
                }

                if (!await AvailabilityCheck(reservation))
                {
                    throw new CarUnavailableException($"car: {reservation.CarId} unavailable");
                }

                context.Entry(reservation).State = EntityState.Modified;
                await context.SaveChangesAsync();
            }
        }
예제 #15
0
        /// <summary>
        /// This method initializes the test environment including the database.
        /// </summary>
        private void InitializeTestEnvironment()
        {
            using CarReservationContext context = new CarReservationContext();
            TestDataHelper testDataHelper;

            if (context.Database.IsInMemory())
            {
                testDataHelper = new TestDataHelperInMemory(context);
            }
            else if (context.Database.IsSqlServer())
            {
                testDataHelper = new TestDataHelperSqlServer(context);
            }
            else
            {
                throw new NotImplementedException();
            }

            testDataHelper.InitializeTestData();
        }
예제 #16
0
 public async Task <List <Customer> > GetAll()
 {
     using CarReservationContext context = new CarReservationContext();
     return(await context.Customers.Include(c => c.Reservations).ToListAsync());
 }
예제 #17
0
 public TestDataHelperInMemory(CarReservationContext context)
     : base(context)
 {
 }
예제 #18
0
 protected TestDataHelper(CarReservationContext context)
 {
     Context = context;
 }
예제 #19
0
        public static void InitializeTestData()
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                var luxuryCarTableName   = context.GetTableName <LuxuryCar>();
                var midRangeCarTableName = context.GetTableName <MidRangeCar>();
                var standardCarTableName = context.GetTableName <StandardCar>();
                var carTableName         = context.GetTableName <Car>();
                var customerTableName    = context.GetTableName <Customer>();
                var reservationTableName = context.GetTableName <Reservation>();

                try
                {
                    context.Database.Log = Console.Write;
                    // Delete all records from tables
                    //      > Cleanup for specific subtypes necessary when not using table per hierarchy (TPH)
                    //        since entities will be stored in different tables.
                    if (luxuryCarTableName != carTableName)
                    {
                        context.DeleteAllRecords(luxuryCarTableName);
                    }
                    if (midRangeCarTableName != carTableName)
                    {
                        context.DeleteAllRecords(midRangeCarTableName);
                    }
                    if (standardCarTableName != carTableName)
                    {
                        context.DeleteAllRecords(standardCarTableName);
                    }
                    context.DeleteAllRecords(reservationTableName);
                    context.DeleteAllRecords(carTableName);
                    context.DeleteAllRecords(customerTableName);

                    // Reset the identity seed (Id's will start again from 1)
                    context.ResetEntitySeed(luxuryCarTableName);
                    context.ResetEntitySeed(midRangeCarTableName);
                    context.ResetEntitySeed(standardCarTableName);
                    context.ResetEntitySeed(carTableName);
                    context.ResetEntitySeed(customerTableName);
                    context.ResetEntitySeed(reservationTableName);

                    // Temporarily allow insertion of identity columns (Id)
                    context.SetAutoIncrementOnTable(luxuryCarTableName, true);
                    context.SetAutoIncrementOnTable(midRangeCarTableName, true);
                    context.SetAutoIncrementOnTable(standardCarTableName, true);
                    context.SetAutoIncrementOnTable(carTableName, true);
                    context.SetAutoIncrementOnTable(customerTableName, true);
                    context.SetAutoIncrementOnTable(reservationTableName, true);

                    // Insert test data
                    context.Cars.AddRange(Cars);
                    context.Customers.AddRange(Customers);
                    context.Reservations.AddRange(Reservations);
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Error while re-initializing database entries.", ex);
                }
                finally
                {
                    // Disable insertion of identity columns (Id)
                    context.SetAutoIncrementOnTable(luxuryCarTableName, false);
                    context.SetAutoIncrementOnTable(midRangeCarTableName, false);
                    context.SetAutoIncrementOnTable(standardCarTableName, false);
                    context.SetAutoIncrementOnTable(carTableName, false);
                    context.SetAutoIncrementOnTable(customerTableName, false);
                    context.SetAutoIncrementOnTable(reservationTableName, false);
                }
            }
        }
예제 #20
0
 public async Task <List <Reservation> > GetAll()
 {
     using CarReservationContext context = new CarReservationContext();
     return(await context.Reservations.Include(r => r.Car).Include(r => r.Customer).ToListAsync());
 }
 public TestDataHelperSqlServer(CarReservationContext context)
     : base(context)
 {
 }