public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "appointments/{handle}")] HttpRequest req,
            string handle,
            ILogger log)
        {
            DateTime     startDate, endDate;
            StringValues startBuffer, endBuffer;

            log.LogInformation("Appointments(): Received request");

            if (req.Query.TryGetValue("startdate", out startBuffer) && req.Query.TryGetValue("enddate", out endBuffer))
            {
                if (!DateTime.TryParse(startBuffer, out startDate) || !DateTime.TryParse(endBuffer, out endDate))
                {
                    log.LogError("Appointments(): Badly formed dates in query string");
                    return(new BadRequestResult());
                }
            }
            else
            {
                // Dates were not provided, use default of next 60 days
                startDate = DateTime.Today;
                endDate   = DateTime.Today.AddDays(60);
            }

            // Protect from too large of a range
            if ((endDate - startDate).TotalDays > 100)
            {
                endDate = startDate.AddDays(100);
            }

            var result = await AppointmentData.GetAppointmentsAsync(handle, AppointmentData.AppointmentStatus.Requested, startDate, endDate);

            return(new OkObjectResult(result));
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "monthlyavailability/{repo}/{handle}")] HttpRequest req,
            string repo,
            string handle,
            ILogger log)
        {
            DateTime     startDate, endDate;
            StringValues startBuffer, endBuffer;

            log.LogInformation("MonthlyAvailability(): Received request");

            if (req.Query.TryGetValue("startdate", out startBuffer) && req.Query.TryGetValue("enddate", out endBuffer))
            {
                if (!DateTime.TryParse(startBuffer, out startDate) || !DateTime.TryParse(endBuffer, out endDate))
                {
                    log.LogError("MonthlyAvailability(): Badly formed dates in query string");
                    return(new BadRequestResult());
                }
            }
            else
            {
                // Dates were not provided, use default of next 45 days
                startDate = DateTime.Today;
                endDate   = DateTime.Today.AddDays(45);
            }

            // Protect from too large of a range
            if ((endDate - startDate).TotalDays > 100)
            {
                endDate = startDate.AddDays(100);
            }

            var result = await AppointmentData.GetAppointmentsAsync(repo, handle, startDate, endDate);

            var expert = await ExpertData.GetExpertAsync(handle);

            var availabilityByDay = new List <AvailabilityEntity>((int)(endDate - startDate).TotalDays);

            // Compute number of 30 minute time slots for this expert
            var totalSlotsForExpert = (expert.EndTime.TimeOfDay - expert.StartTime.TimeOfDay).TotalHours / 30;
            var dayLoop             = startDate;

            while (dayLoop <= endDate)
            {
                bool hasAvailability = false;

                // Check if day is a "working" day
                if (expert.ExcludeWeekends == false ||
                    (dayLoop.DayOfWeek != DayOfWeek.Saturday && dayLoop.DayOfWeek != DayOfWeek.Sunday))
                {
                    // Look how many time slots were filled and compare against total slots
                    var numAppointments = result.Count(x => x.DateTime.Date == dayLoop.Date);

                    hasAvailability = numAppointments < totalSlotsForExpert;
                }

                availabilityByDay.Add(new AvailabilityEntity
                {
                    StartDate = dayLoop,
                    EndDate   = dayLoop,
                    Available = hasAvailability,
                });

                dayLoop = dayLoop.AddDays(1);
            }

            log.LogInformation("here");

            return(new OkObjectResult(availabilityByDay));
        }
Пример #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "dailyavailability/{repo}/{handle}")] HttpRequest req,
            string repo,
            string handle,
            ILogger log)
        {
            DateTime     startDate, endDate;
            StringValues startBuffer, endBuffer;

            log.LogInformation("DailyAvailability(): Received request");

            if (req.Query.TryGetValue("startdate", out startBuffer) && req.Query.TryGetValue("enddate", out endBuffer))
            {
                if (!DateTime.TryParse(startBuffer, out startDate) || !DateTime.TryParse(endBuffer, out endDate))
                {
                    log.LogError("DailyAvailability(): Badly formed dates in query string");
                    return(new BadRequestResult());
                }
            }
            else
            {
                // Dates were not provided, use default of next 7 days
                startDate = DateTime.Today;
                endDate   = DateTime.Today.AddDays(7);
            }

            // Protect from too large of a range
            if ((endDate - startDate).TotalDays > 100)
            {
                endDate = startDate.AddDays(100);
            }

            var result = await AppointmentData.GetAppointmentsAsync(repo, handle, startDate, endDate);

            var expert = await ExpertData.GetExpertAsync(handle);

            if (expert == null)
            {
                return(new NotFoundResult());
            }

            var availableTimeslots = new List <AvailabilityEntity>();

            var dayLoop = startDate;

            while (dayLoop <= endDate)
            {
                if (expert.ExcludeWeekends == true &&
                    (dayLoop.DayOfWeek == DayOfWeek.Saturday || dayLoop.DayOfWeek == DayOfWeek.Sunday))
                {
                    dayLoop = dayLoop.AddDays(1);
                    continue;
                }

                // Build slots for this day and mark as available or filled
                var timeSlotLoop = expert.StartTime.TimeOfDay;
                while (timeSlotLoop <= expert.EndTime.TimeOfDay)
                {
                    var starttime   = new DateTime(dayLoop.Year, dayLoop.Month, dayLoop.Day, timeSlotLoop.Hours, timeSlotLoop.Minutes, timeSlotLoop.Seconds);
                    var appointment = result.Where(x => x.DateTime.Date == dayLoop.Date && x.DateTime.TimeOfDay == timeSlotLoop).FirstOrDefault();

                    var availability = new AvailabilityEntity
                    {
                        StartDate = starttime,
                        EndDate   = starttime.AddMinutes(30),
                    };

                    availability.Available = appointment == null;

                    availableTimeslots.Add(availability);

                    // increment by 30 minutes
                    timeSlotLoop = timeSlotLoop.Add(new TimeSpan(0, 30, 0));
                }

                dayLoop = dayLoop.AddDays(1);
            }

            return(new OkObjectResult(availableTimeslots));
        }