public AdminSchedulesModule(ISessionProvider sessionProvider, IDispatchEvents eventDispatcher)
            : base(sessionProvider, eventDispatcher, "/manage/clients/{id}/scheduleItems")
        {
            Put["/{scheduleItemId}/move"] = parameters =>
                {
                    string ScheduleItemId = string.Format("{0}/scheduleItems/{1}", ClientId, parameters.scheduleItemId);

                    var scheduleItem = RavenSession.Load<ScheduleItem>(ScheduleItemId);

                    var command = this.Bind<MoveScheduleItemCommand>();

                    scheduleItem.VisitStart = command.VisitStart;
                    scheduleItem.VisitEnd = command.VisitEnd;
                    scheduleItem.Date = command.Date;

                    UncommitedEvents.Add(new ScheduleItemMoved
                        {
                            ScheduleItemId = scheduleItem.Id,
                            Date = scheduleItem.Date,
                            TeamId = scheduleItem.TeamId,
                            TeamName = scheduleItem.TeamName,
                            SiteId = scheduleItem.SiteId,
                            SiteName = scheduleItem.SiteName,
                            VisitStart = scheduleItem.VisitStart,
                            VisitEnd = scheduleItem.VisitEnd,
                        });

                    return HttpStatusCode.NoContent;
                };

            Delete["/{scheduleItemId}"] = parameters =>
                {
                    string ScheduleItemId = string.Format("{0}/scheduleItems/{1}", ClientId, parameters.scheduleItemId);

                    var scheduleItem = RavenSession.Load<ScheduleItem>(ScheduleItemId);

                    UncommitedEvents.Add(new ScheduleItemDeleted
                    {
                        ScheduleItemId = scheduleItem.Id,
                        Date = scheduleItem.Date,
                        TeamId = scheduleItem.TeamId,
                        TeamName = scheduleItem.TeamName,
                        SiteId = scheduleItem.SiteId,
                        SiteName = scheduleItem.SiteName,
                    });

                    RavenSession.Delete(scheduleItem);

                    return HttpStatusCode.NoContent;
                };

            Post["/create"] = paramaters =>
                {
                    var command = this.Bind<CreateScheduleItemCommand>();

                    var team = RavenSession.Load<Team>(command.TeamId);

                    if (team == null)
                        return Response.ReferenceMissing(string.Format("Team with Id '{0}' not found.", command.TeamId));

                    var site = RavenSession.Load<Site>(command.SiteId);
                    if (site == null)
                        return Response.ReferenceMissing(string.Format("Site with Id '{0}' not found.", command.SiteId));

                    var scheduleItem = new ScheduleItem
                        {
                            ClientId = ClientId,
                            TeamId = command.TeamId,
                            TeamName = team.Name,
                            SiteId = command.SiteId,
                            SiteName = site.Name,
                            Date = command.Date,
                            VisitStart = command.VisitStart,
                            VisitEnd = command.VisitEnd,
                            SiteAddress = site.Address,
                            SiteCity = site.City,
                            SiteDescription = site.Description,
                            SitePostCode = site.PostCode,
                            SiteLatitude = site.Latitude,
                            SiteLongitude = site.Longitude,
                            SiteLocationUrl = site.Url,
                        };

                    var sections = RavenSession.Query<SiteInfoSection>()
                                               .Where(x => x.SiteId == site.Id)
                                               .ToList();

                    foreach (var section in sections)
                    {
                        scheduleItem.AddSection(section.Grouping, section.Id, section.Heading, section.Content, site.Id);
                    }

                    var sectionDetails = RavenSession.Query<SectionDetail>()
                                                     .Where(x => x.SiteId == site.Id)
                                                     .ToList();

                    foreach (var detail in sectionDetails)
                    {
                        scheduleItem.AddSectionDetail(detail.SectionId, detail.Grouping, detail.Id, detail.ItemName,
                                                      detail.ItemIdentity, detail.VerificationQuestion, detail.Comments);
                    }

                    RavenSession.Store(scheduleItem);

                    UncommitedEvents.Add(new ScheduleItemCreated
                        {
                            ScheduledItemId = scheduleItem.Id,
                            ClientId = scheduleItem.ClientId,
                            TeamId = scheduleItem.TeamId,
                            TeamName = scheduleItem.TeamName,
                            SiteLatitude = scheduleItem.SiteLatitude,
                            SiteLongitude = scheduleItem.SiteLongitude,
                            SiteId = scheduleItem.SiteId,
                            SiteName = scheduleItem.SiteName,
                            StartTime = scheduleItem.VisitStart,
                            EndTime = scheduleItem.VisitEnd,
                            Date = scheduleItem.Date,
                        });

                    return HttpStatusCode.NoContent;
                };

            Get[@"/{year}/{month}/{day}"] = parameters =>
                {
                    var teams = RavenSession.Query<Team>()
                                            .Customize(x => x.Include<Team>(o => o.Users))
                                            .Where(x => x.ClientId == ClientId)
                                            .ToArray();

                    string date = string.Format("{0}{1:00}{2:00}", (int)parameters.year, (int)parameters.month, (int)parameters.day);

                    var schedules = RavenSession.Query<ScheduleItem>()
                                                .Where(x => x.ClientId == ClientId && x.Date == date)
                                                .ToList();

                    var viewModel = new
                        {
                            schedules = teams.ToArray().Select(x =>
                                    new
                                        {
                                            clientId = x.ClientId,
                                            teamId = x.Id,
                                            team = x.Name,
                                            teamMembers = string.Join(", ", x.Users.Select(u => RavenSession.Load<User>(u).Name)),
                                            status = "onTime",
                                            statusDetail = "",
                                            date,
                                            tasks = schedules.Where(z => z.TeamId == x.Id & z.Date == date)
                                                             .Select(o => new
                                                                    {
                                                                        scheduleId = o.Id,
                                                                        siteId = o.SiteId,
                                                                        name = o.SiteName,
                                                                        status = "open",
                                                                        visitStart = (o.VisitStart.Length == 4) ? "0" + o.VisitStart : o.VisitStart,
                                                                        visitEnd = (o.VisitEnd.Length == 4) ? "0" + o.VisitEnd : o.VisitEnd,
                                                                    }),
                                        }),
                        };

                    return Response.AsJson(viewModel);
                };

            Get[@"/{year}/{month}/{day}/liveroute"] = parameters =>
                {
                    string date = string.Format("{0}{1:00}{2:00}", (int)parameters.year, (int)parameters.month, (int)parameters.day);

                    var routes = RavenSession.Query<DaylyRoute>()
                                             .Where(x => x.Date == date && x.ClientId == ClientId)
                                             .ToList();

                    var viewModel = new
                        {
                            Route = routes,
                        };

                    return viewModel;
                };
        }
 public TaskStatusHolder(ScheduleItem scheduleItem)
 {
     _scheduleItem = scheduleItem;
 }