Exemplo n.º 1
0
        public virtual int SaveChanges(List <Booking> bookings, bool dontCheckClash)
        {
            int objectsWritten = 0;

            if (!dontCheckClash)
            {
                AvailabilityService aC = new AvailabilityService(_bookingRepository, _roomRepository, _locationRepository);
                List <Booking>      clashedBookings;

                using (var scope = TransactionUtils.CreateTransactionScope())
                {
                    foreach (var b in bookings)
                    {
                        bool invalid = aC.DoesMeetingClash(b, out clashedBookings);
                        //Checks if the booking that clashed is the current booking being saved, this allows us to edit bookings.
                        if (clashedBookings.Count == 1)
                        {
                            if (invalid && b.ID != 0)
                            {
                                invalid = !(b.ID == clashedBookings[0].ID);
                            }
                        }

                        if (invalid)
                        {
                            throw new BookingConflictException("Simultaneous booking conflict, please try again.");
                        }
                    }

                    //We dont mess around with the recurrence ID's after they are first created. So if the first booking in the batch has an ID, it exists in the DB. So dont go on further.
                    if (bookings.First().ID == 0)
                    {
                        int?recurrenceId = bookings.Select(x => x.RecurrenceId).FirstOrDefault();
                        //Check if the recurrence id is infact the next id in the DB (could potentially not be if a recurrence was made during execution of this request)
                        int nextRecurrenceId = _bookingRepository.GetNextRecurrenceId();
                        if (recurrenceId != null && recurrenceId != nextRecurrenceId)
                        {
                            //if the bookings recurrenceid is not the next, then set it.
                            bookings.ForEach(x => x.RecurrenceId = nextRecurrenceId);
                        }
                    }
                    objectsWritten = base.SaveChanges();
                    scope.Complete();
                }
            }
            else
            {
                objectsWritten = base.SaveChanges();
            }
            return(objectsWritten);
        }
Exemplo n.º 2
0
        public virtual int SaveChanges(Booking booking, bool dontCheckClash)
        {
            List <Booking> clashedBookings;
            int            objectsWritten = 0;

            if (!dontCheckClash)
            {
                using (var scope = TransactionUtils.CreateTransactionScope())
                {
                    AvailabilityService aC = new AvailabilityService(_bookingRepository, _roomRepository, _locationRepository);

                    bool invalid = aC.DoesMeetingClash(booking, out clashedBookings);
                    //Checks if the booking that clashed is the current booking being saved, this allows us to edit bookings.
                    if (clashedBookings.Count == 1)
                    {
                        if (invalid && booking.ID != 0)
                        {
                            invalid = !(booking.ID == clashedBookings[0].ID);
                        }
                    }

                    if (invalid)
                    {
                        throw new BookingConflictException("Simultaneous booking conflict, please try again.");
                    }

                    objectsWritten = base.SaveChanges();
                    scope.Complete();
                }
            }
            else
            {
                objectsWritten = base.SaveChanges();
            }
            return(objectsWritten);
        }