Пример #1
0
        // Updates the dictionary
        public void UpdateDictionary()
        {
            TimeAvailableTypes = new Dictionary <TimeSpan, List <BoatType> >();
            if (When.SelectedDate.HasValue)
            {
                if (int.TryParse(Duration.Text, out var durationInt))
                {
                    var duration = TimeSpan.FromMinutes(durationInt);
                    for (var i = TimeSpan.Zero; i < new TimeSpan(0, 23, 59, 0); i += TimeSpan.FromMinutes(15))
                    {
                        if (ReservationConstraints.IsValid(When.SelectedDate.Value + i, duration, _reservationService, _windowManager.UserSession.UserId).IsValid)
                        {
                            var availableTypes =
                                _reservationService.AvailableBoatTypes(When.SelectedDate.Value + i, duration);
                            if (availableTypes.Any())
                            {
                                TimeAvailableTypes[i] = availableTypes;
                            }
                        }
                    }
                }
            }

            UpdateTimeList();
            UpdateBoatTypeList();
        }
Пример #2
0
        /// <inheritdoc />
        public ReservationConstraintsMessage PlaceReservation(int boatType, int memberId, DateTime reservationDate,
                                                              TimeSpan duration)
        {
            var availableBoats = GetAvailableBoats(reservationDate, duration).Where(boat =>
                                                                                    boat.BoatTypeId == boatType && boat.Status != (int)BoatState.InService).ToList();

            // Checks if the reservation doesn't violate any constraints
            var message = ReservationConstraints.IsValid(reservationDate, duration, this, memberId);

            if (!message.IsValid)
            {
                return(message);
            }

            // Check if there is an available boat
            if (availableBoats.Count > 0)
            {
                SailingBoat boatToReserve = null;

                // Take the boat with most reservations
                var max = int.MinValue;
                foreach (var boat in availableBoats)
                {
                    if (boat.SailingReservations.Count >= max)
                    {
                        boatToReserve = boat;
                        max           = boat.SailingReservations.Count;
                    }
                }

                //Create a reservation for this boat
                _context.Reservations.Add(new SailingReservation
                {
                    Date                  = reservationDate,
                    Duration              = duration,
                    ReservedByUserId      = memberId,
                    ReservedSailingBoatId = boatToReserve.Id
                });

                _context.SaveChanges();
                return(new ReservationConstraintsMessage(true, "Reservering is geplaatst"));
            }

            return(new ReservationConstraintsMessage(false, "Geen boten beschikbaar."));
        }