public async Task <BookingResponse> GetBookingsDay(BookingsDayRequest model)
        {
            // Get activity and check if it exists
            var activity = await _bookingDbContext.Activities.Include(a => a.Bookings)
                           .Where(a => a.Id == model.ActivityId).FirstAsync();

            if (activity == null)
            {
                return(new BookingResponse(400, "Activity does not exist"));
            }

            var today = DateTime.Today;

            // Check if date is in the past
            if (model.Date < today)
            {
                return(new BookingResponse(400, "Date must be in the future"));
            }

            var year  = model.Date.Year;
            var month = model.Date.Month;
            var day   = model.Date.Day;

            var date            = new DateTime(year, month, day);
            var centralEuropean = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
            var now             = TimeZoneInfo.ConvertTime(DateTime.Now, centralEuropean);
            var from            = now > date ? now : date;

            var to = GetNextDay(from);

            // Get bookings in given day
            var bookings = activity.Bookings.Where(booking => booking.Start >= from && booking.Start < to);

            var times = new List <int>();

            now = DateRoundUp(now);
            for (var i = activity.Open; i < activity.Close; i++)
            {
                if (now <= date.AddHours(i))
                {
                    times.Add(i);
                }
            }

            var openHours = activity.Close - activity.Open;

            // Go through bookings and set times as booked
            foreach (var booking in bookings)
            {
                var current = booking.Start;
                while (current < booking.End)
                {
                    Console.WriteLine(current);
                    times.Remove(current.Hour);
                    current = current.AddHours(1);
                }
            }

            return(new BookingResponse(times));
        }
Exemple #2
0
        public async Task <IActionResult> GetBookingsDay([FromQuery] BookingsDayRequest model)
        {
            var response = await _bookingService.GetBookingsDay(model);

            return(StatusCode(response.StatusCode, response));
        }