public ActionResult Index(int? id)
 {
     Meeting meeting = null;
     using (var container = new MeetingPlannerContainer())
     {
         meeting = container.MeetingSet.First(m => m.Id == id);
     }
     return View(new ResultIndexModel { MeetingId = meeting.Id});
 }
        //
        // GET: /Meeting/
        public ActionResult New(string description, DateTime? from, DateTime? to)
        {
            using (var container = new MeetingPlannerContainer())
            {
                var newMeeting = new Meeting()
                {
                    Description = description,
                    MeetingStatusId = 1,
                    From = from,
                    To = to,
                };
                container.MeetingSet.Add(newMeeting);
                container.SaveChanges();

                Response.Cookies[MetingOwnerCookie].Value = newMeeting.Id.ToString();
                Response.Cookies[MetingOwnerCookie].Expires = DateTime.Now.AddDays(1);
                return Json(newMeeting.Id);
            }
        }
        public ActionResult Edit(int? id)
        {
            var model = new MeetingModel();
            if (id.HasValue)
            {
                using (var container = new MeetingPlannerContainer())
                {
                    var userNameCookie = Request.Cookies[UserNameCookie];
                    if (userNameCookie != null)
                        model.UserName = HttpUtility.UrlDecode(userNameCookie.Value);
                    var meeting = container.MeetingSet.FirstOrDefault(m => m.Id == id.Value);
                    if (meeting != null)
                    {
                        model.Description = meeting.Description;
                        model.MeetingId = meeting.Id;
                        model.From = meeting.From;
                        model.To = meeting.To;
                        var ownerCookie = Request.Cookies[MetingOwnerCookie];
                        if (ownerCookie != null)
                        {
                            model.IsOwner = Int32.Parse(ownerCookie.Value) == id;
                        }

                        var voteCookie = Request.Cookies[GetVoteCookieName(meeting.Id)];
                        if (voteCookie != null)
                        {
                            var dayIds = ParseVoteCookieAndGetIds(voteCookie);
                            model.MarkedDays = new Dictionary<DateTime, bool>();
                            container.UserMeetingDatesSet.Where(m => dayIds.Contains(m.Id))
                                     .ToList()
                                     .ForEach(d => model.MarkedDays.Add(d.Date, d.IsAvaliable));
                        }
                        return View(model);
                    }
                }
            }
            model.Description = "Встреча не найдена";
            return View(model);
        }
        public ActionResult GetResults(int? id, string currentRowVersion)
        {
            var model = new ResultModel();
            using (var container = new MeetingPlannerContainer())
            {
                var meeting = container.MeetingSet.FirstOrDefault(m => m.Id == id);
                if (meeting != null)
                {
                    model.RowVersion = meeting.RowVersion.ToString();
                    if (currentRowVersion != model.RowVersion)
                    {
                        model.Results = new List<ResultModel.DateCount>();
                        var userDates =
                            from user in container.UserMeetingDatesSet.Where(m => m.MeetingId == id)
                            join userName in container.CachedUserNames on user.CachedUserNameId equals userName.Id into
                                results
                            from result in results.DefaultIfEmpty()
                            select new {userDate = user, userName = result == null ? string.Empty : result.UserName};
                        userDates = userDates.OrderBy(key => key.userDate.Date);

                        foreach (var userDate in userDates)
                        {
                            string date = DateTimeHelper.ConvertToUtc(userDate.userDate.Date).ToShortDateString();
                            string userName = userDate.userName;
                            if (!model.HasKey(date))
                                model.Results.Add(new ResultModel.DateCount(date));
                            if (userDate.userDate.IsAvaliable)
                            {
                                if (!string.IsNullOrEmpty(userName) &&
                                    !model.GetDateCountByKey(date).UserNamesConvenient.Contains(userDate.userName))
                                    model.GetDateCountByKey(date).UserNamesConvenient.Add(userDate.userName);
                                model.GetDateCountByKey(date).Convenient++;
                            }
                            else
                            {
                                if (!string.IsNullOrEmpty(userName) &&
                                    !model.GetDateCountByKey(date).UserNamesInconvenient.Contains(userDate.userName))
                                    model.GetDateCountByKey(date).UserNamesInconvenient.Add(userDate.userName);
                                model.GetDateCountByKey(date).Inconvenient++;
                            }
                        }

                        var max = int.MinValue;
                        string finalDate = string.Empty;
                        foreach (var result in model.Results)
                        {
                            int value = result.Convenient - (result.Inconvenient*2);
                            if (value > max)
                            {
                                max = value;
                                finalDate = result.Key;
                            }
                        }
                        var maxConv = model.Results.Max(p => p.Convenient);
                        var maxInconv = model.Results.Max(p => p.Inconvenient);
                        model.MaxValue = Math.Max(maxConv, maxInconv);
                        model.Message = string.Format(Strings.CountResult, finalDate);

                    }
                }
            }
            return Json(model, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SendResults(DateTime[] greenDays, DateTime[] redDays, int meetingId, string userName)
        {
            if (greenDays != null || redDays != null)
            {
                using (var container = new MeetingPlannerContainer())
                {
                    var meeting = container.MeetingSet.First(m => m.Id == meetingId);
                    meeting.RowVersion = Guid.NewGuid();

                    CachedUserName user = null;
                    if (!string.IsNullOrEmpty(userName))
                    {
                        user = ProcessUserName(userName, container);
                    }

                    var voteCookieName = GetVoteCookieName(meetingId);
                    var cookie = Request.Cookies[voteCookieName];
                    if (cookie != null)
                    {
                        DeleteSetBeforeDates(cookie, container);
                    }

                    cookie = new HttpCookie(voteCookieName) { Expires = DateTime.Now.AddDays(1) };
                    var entities = new List<UserMeetingDates>();
                    entities.AddRange(MakeEntityUserMeetingDay(greenDays, meetingId, container, true, user));
                    entities.AddRange(MakeEntityUserMeetingDay(redDays, meetingId, container, false, user));
                    
                    container.SaveChanges();

                    entities.ForEach(e => cookie[e.Id.ToString()] = null);

                    if (Response.Cookies[voteCookieName] != null)
                        Response.Cookies.Remove(voteCookieName);
                    Response.Cookies.Add(cookie);

                    
                }
            }

            return Json(SaveResult.Ok);
        }
 private List<UserMeetingDates> MakeEntityUserMeetingDay(DateTime[] days, int meetingId, MeetingPlannerContainer container, bool isAvaliable, CachedUserName user)
 {
     var result = new List<UserMeetingDates>();
     if (days != null)
     {
         foreach (var day in days)
         {
             var entity = new UserMeetingDates
                 {
                     MeetingId = meetingId,
                     IsAvaliable = isAvaliable,
                     Date = DateTimeHelper.ConvertToUtc(day),
                     CachedUserName = user
                 };
             container.UserMeetingDatesSet.Add(entity);
             result.Add(entity);
         }
     }
     return result;
 }
        private CachedUserName ProcessUserName(string userName, MeetingPlannerContainer container)
        {
            var user = container.CachedUserNames.FirstOrDefault(u => u.UserName == userName);
            if (user == null)
            {
                user = new CachedUserName {UserName = userName};
                container.CachedUserNames.Add(user);
            }

            Response.Cookies.Add(new HttpCookie(UserNameCookie, HttpUtility.UrlEncode(userName)));
            return user;
        }
 private void DeleteSetBeforeDates(HttpCookie cookie, MeetingPlannerContainer container)
 {
     var dayIds = ParseVoteCookieAndGetIds(cookie);
     var days = container.UserMeetingDatesSet.Where(d => dayIds.Contains(d.Id)).ToList();
     days.ForEach(d => container.UserMeetingDatesSet.Remove(d));
 }