Exemplo n.º 1
0
        public PartialViewResult UserStatUpdate(int eventid, bool attend)
        {
            var db       = new CalendarEventContext();
            var userName = cookie.LoadStringFromCookie(Cookies.UserNameCookieKey);

            if (userName != "" || userName != null)
            {
                var           alreadyGoing = false;
                EventUserStat current      = null;
                foreach (var eus in db.EventUserStats)
                {
                    if (eus.UserName == userName && eus.EventID == eventid)
                    {
                        current      = eus;
                        alreadyGoing = true;
                        // here: project extension material - calculate score based on eus.GoingTime, add "score" to account.cs
                    }
                }
                if (attend)
                {
                    if (alreadyGoing)
                    {
                        current.Attend = true;
                    }
                    else
                    {
                        db.EventUserStats.Add(new EventUserStat
                        {
                            UserName  = userName,
                            EventID   = eventid,
                            GoingTime = DateTime.Now,
                            Attend    = true
                        });
                    }
                }
                else
                {
                    if (alreadyGoing)
                    {
                        db.EventUserStats.Remove(current);
                    }
                    else
                    {
                        db.EventUserStats.Add(new EventUserStat
                        {
                            UserName  = userName,
                            EventID   = eventid,
                            GoingTime = DateTime.Now
                        });
                    }
                }
                db.SaveChanges();
            }

            var modelWrapper = new ModelDataWrapper();

            modelWrapper.eventID = eventid;
            return(PartialView("_UserStatUpdate", modelWrapper));
        }