Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 3
0
 public async Task Delete(Customer customer)
 {
     using (CarReservationContext context = new CarReservationContext())
     {
         context.Entry(customer).State = EntityState.Deleted;
         await context.SaveChangesAsync();
     }
 }
Ejemplo n.º 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();
     }
 }
Ejemplo n.º 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));
        }
Ejemplo n.º 6
0
        public async Task <Customer> Insert(Customer customer)
        {
            using (CarReservationContext context = new CarReservationContext())
            {
                context.Entry(customer).State = EntityState.Added;
                await context.SaveChangesAsync();

                return(customer);
            }
        }
Ejemplo n.º 7
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);
         }
     }
 }
Ejemplo n.º 8
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();
            }
        }