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);
            }
        }
Beispiel #2
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);
                }
            }
        }
 public async Task Delete(Customer customer)
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         context.Entry(customer).State = EntityState.Deleted;
         await context.SaveChangesAsync();
     }
 }
Beispiel #4
0
 public void DeleteObject <T>(T obj) where T : class
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         context.Entry <T>(obj).State = EntityState.Deleted;
         context.SaveChanges();
     }
 }
Beispiel #5
0
        private static LocalOptimisticConcurrencyException <T> CreateLocalOptimisticConcurrencyException <T>(CarReservationContext context, T entity)
            where T : class
        {
            var dbEntity = (T)context.Entry(entity)
                           .GetDatabaseValues()
                           .ToObject();

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

                return(customer);
            }
        }
 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);
         }
     }
 }
        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();
            }
        }