Exemplo n.º 1
0
        private HomeViewModel BuildHomeViewModel(DateTime date)
        {
            HomeViewModel      model        = new HomeViewModel();
            DateTime           baseTime     = new DateTime(date.Year, date.Month, date.Day, 9, 0, 0);
            List <Appointment> appointments = _service.FindAppointments(date).ToList();
            List <AppUser>     mechanics    = _service.Mechanics.ToList();

            Dictionary <int, int> columns = new Dictionary <int, int>();

            foreach (AppUser m in mechanics)
            {
                columns.Add(m.Id, mechanics.IndexOf(m));
            }

            model.Date      = date;
            model.Mechanics = mechanics.Select(m => m.Name).ToList();

            for (int i = 0; i < 8; ++i)
            {
                foreach (AppUser m in mechanics)
                {
                    DateTime          start = baseTime.AddHours(i);
                    TimeslotViewModel item  = new TimeslotViewModel(start, m.Id, m.Name);
                    model.Timeslots[i].Add(item);
                }
            }

            foreach (Appointment appointment in appointments)
            {
                TimeslotStatus status;
                if (appointment.Partner.UserName.Equals(User.Identity.Name))
                {
                    status = TimeslotStatus.OWN;
                    model.Timeslots[appointment.Time.Hour - 9][columns.GetValueOrDefault(appointment.Mechanic.Id)].Id = appointment.Id;
                }
                else
                {
                    status = TimeslotStatus.BOOKED;
                }
                model.Timeslots[appointment.Time.Hour - 9][columns.GetValueOrDefault(appointment.Mechanic.Id)].Status = status;
            }

            for (int i = 0; i < 8; ++i)
            {
                foreach (AppUser m in mechanics)
                {
                    TimeslotViewModel timeslot = model.Timeslots[i][columns.GetValueOrDefault(m.Id)];
                    if (timeslot.Start < DateTime.Now || HolidayChecker.IsHoliday(timeslot.Start))
                    {
                        timeslot.Status = TimeslotStatus.DISABLED;
                    }
                }
            }

            return(model);
        }
Exemplo n.º 2
0
        public async Task <JsonResult> SaveTimeslotValidation(TimeslotViewModel data)
        {
            var timeslots = await _prepareService.GetTimeslots();

            if (data.TimeslotId != 0)
            {
                timeslots = timeslots.Where(t => t.TimeslotId != data.TimeslotId);
            }
            string validationMessage = "";
            bool   goodData          = true;

            if ((data.StartTime == data.EndTime) && (data.StartTime != TimeSpan.Zero || data.EndTime != TimeSpan.Zero))
            {
                goodData          = false;
                validationMessage = "Les heures de début et de fin ne peuvent pas être identiques";
                return(Json(new { goodData, Message = validationMessage }, JsonRequestBehavior.AllowGet));
            }
            if (data.StartTime == null || data.EndTime == null)
            {
                goodData          = false;
                validationMessage = "Les heures de début et de fin ne peuvent être nulles";
                return(Json(new { goodData, Message = validationMessage }, JsonRequestBehavior.AllowGet));
            }
            //Overlap validation
            foreach (var timeslot in timeslots)
            {
                //timeslot
                var startTime = timeslot.StartTime;
                var end       = timeslot.EndTime < timeslot.StartTime ? timeslot.EndTime.Add(TimeSpan.FromDays(1)) : timeslot.EndTime;
                var duration  = end - startTime;
                var endTime   = startTime + duration;

                var tsStart = DateTime.Today.Add(startTime);
                var tsEnd   = DateTime.Today.Add(endTime);

                //data
                startTime = data.StartTime;
                end       = data.EndTime < data.StartTime ? data.EndTime.Add(TimeSpan.FromDays(1)) : data.EndTime;
                duration  = end - startTime;
                endTime   = startTime + duration;
                var dataStart = DateTime.Today.Add(startTime);
                var dataEnd   = DateTime.Today.Add(endTime);


                if ((dataStart < tsEnd && tsStart < dataEnd) || (dataStart.AddDays(-1) < tsEnd && tsStart < dataEnd.AddDays(-1)) || (dataStart.AddDays(1) < tsEnd && tsStart < dataEnd.AddDays(1)))
                {
                    goodData          = false;
                    validationMessage = "La plage horaire ne peut pas déborder sur une autre plage horaire";
                    break;
                }
            }
            var result = new { goodData, Message = validationMessage };

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 3
0
        private Entities.Timeslot CreateTestTimeslot(TimeslotViewModel model)
        {
            var created = new Entities.Timeslot
            {
                Id    = (timeslots.Count + 1).ToString(),
                End   = DateTime.Now,
                Start = DateTime.Now,
                Name  = "Test Timeslot 2"
            };

            timeslots.Add(created);

            return(created);
        }
Exemplo n.º 4
0
        public async Task <Timeslot> CreateTimeslotAsync(TimeslotViewModel model)
        {
            try
            {
                var timeslot = mapper.Map <Timeslot>(model);

                var created = await repository.CreateTimeslotAsync(timeslot);

                return(await SaveAndReturn(created));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 5
0
        public async Task <ActionResult <Timeslot> > CreateTimeslot([FromBody] TimeslotViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var created = await timeslotService.CreateTimeslotAsync(model);

                return(CreatedAtAction(nameof(GetTimeslotById), new { timeslotId = created.Id }, created));
            }
            catch (Exception e)
            {
                return(await HandleControllerException(e));
            }
        }
Exemplo n.º 6
0
        public async Task CanAddALocation()
        {
            var toAdd = new TimeslotViewModel
            {
                End   = DateTime.Now,
                Start = DateTime.Now,
                Name  = "Test Timeslot 3"
            };

            timeslotService.Setup(repo => repo.CreateTimeslotAsync(toAdd)).ReturnsAsync(CreateTestTimeslot(toAdd));
            timeslotService.Setup(repo => repo.GetTimeslotsAsync()).ReturnsAsync(GetTestTimeslots());

            var result = await controller.GetTimeslots();

            // Assertions
            Assert.IsInstanceOf <ActionResult <IList <Entities.Timeslot> > >(result);
            var resultObject = GetObjectResultContent(result);

            Assert.AreEqual(3, resultObject.Count);
        }
Exemplo n.º 7
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (isNewInstance)
            {
                if (viewModel == null)
                {
                    if (State.Count > 0)
                    {
                        viewModel = (TimeslotViewModel)State["viewModel"];
                    }
                    else
                    {
                        viewModel = new TimeslotViewModel();
                        viewModel.Load(this.QueryStringInt("timeslotid"));
                    }
                    DataContext = viewModel;
                }
            }
            isNewInstance = false;
        }