public ActionResult Create([Bind(Include = "SessionId,SessionDate,Note")] Session session)
        {
            if (!ModelState.IsValid)
            {
                return(View(session));
            }

            session.CreateDateTime = DateTime.UtcNow;
            session.UpdateDateTime = DateTime.UtcNow;
            var newSession = _db.Sessions.Add(session);

            _db.SaveChanges();

            _db.AddActivity(newSession.SessionId, "Created Session");

            EmailSession(newSession);

            return(RedirectToAction("Index"));
        }
        public ActionResult Buy([Bind(Include = "BuySellId,SessionId,BuyerNote,SellerUserId")] BuySell buySell)
        {
            if (Request.Url == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var session = _db.Sessions.Find(buySell.SessionId);

            if (session == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            ViewBag.SessionDate = session.SessionDate;
            var buyer = UserManager.FindById(User.Identity.GetUserId());

            if (!buyer.Active)
            {
                ModelState.AddModelError("", "Your account is inactive.  Contact the commissioner.");
            }

            if (session.IsPast)
            {
                ModelState.AddModelError("", "You cannot buy from a session that has past.");
            }

            if (!session.CanBuy(buyer.Preferred, User.IsInRole("Admin")))
            {
                ModelState.AddModelError("", "Spots cannot be bought until " + session.BuyDateTime.AddDays(-1).ToString("dddd, MM/dd/yyyy, HH:mm") + " for Preferred players and " + session.BuyDateTime.ToString("dddd, MM/dd/yyyy, HH:mm") + " for all players.");
            }

            if (ModelState.IsValid)
            {
                var sessionurl = Url.Action("Details", "Sessions", new { id = session.SessionId }, Request.Url.Scheme);

                // Can't buy from self
                if (buySell.SellerUserId == buyer.Id)
                {
                    return(RedirectToAction("Details", "Sessions", new { id = buySell.SessionId }));
                }

                // Already bought in this session
                var alreadyBuyer = _db.BuySell.Where(q => q.SessionId == buySell.SessionId && !string.IsNullOrEmpty(q.BuyerUserId) && q.BuyerUserId == buyer.Id).OrderBy(d => d.CreateDateTime).FirstOrDefault();
                if (alreadyBuyer != null)
                {
                    return(RedirectToAction("Details", "Sessions", new { id = buySell.SessionId }));
                }

                string activity;
                IEnumerable <ApplicationUser> users;

                if (!string.IsNullOrEmpty(buySell.SellerUserId))
                {
                    var updateBuySell = _db.BuySell.FirstOrDefault(q => q.BuySellId == buySell.BuySellId);
                    if (updateBuySell == null)
                    {
                        return(RedirectToAction("Details", "Sessions", new { id = buySell.SessionId }));
                    }

                    // Make sure this spot is available to buy
                    if (!string.IsNullOrEmpty(updateBuySell.BuyerUserId))
                    {
                        TempData["Message"] = "The spot you attempted to buy has been sold.";
                        return(RedirectToAction("Details", "Sessions", new { id = buySell.SessionId }));
                    }

                    var seller = UserManager.FindById(updateBuySell.SellerUserId);

                    activity = buyer.FirstName + " " + buyer.LastName + " BOUGHT SPOT FROM " + seller.FirstName + " " + seller.LastName + ". Team assignment: " + updateBuySell.TeamAssignment;

                    updateBuySell.BuyerUserId      = buyer.Id;
                    updateBuySell.BuyerNote        = buySell.BuyerNote;
                    updateBuySell.UpdateDateTime   = DateTime.UtcNow;
                    _db.Entry(updateBuySell).State = EntityState.Modified;

                    SendSessionEmail(session, seller, buyer, SessionAction.Buy, updateBuySell);
                    users = UserManager.Users.ToList().Where(t => t.NotificationPreference == NotificationPreference.All && t.Active && t.Id != seller.Id && t.Id != buyer.Id);
                }
                else
                {
                    activity = buyer.FirstName + " " + buyer.LastName + " added to BUYING list";

                    buySell.BuyerUserId    = User.Identity.GetUserId();
                    buySell.BuyerNote      = buySell.BuyerNote;
                    buySell.UpdateDateTime = DateTime.UtcNow;

                    buySell.CreateDateTime = DateTime.UtcNow;

                    _db.BuySell.Add(buySell);

                    users = UserManager.Users.ToList().Where(t => t.NotificationPreference == NotificationPreference.All && t.Active && t.Id != buyer.Id);
                }

                _db.SaveChanges();

                _db.AddActivity(buySell.SessionId, activity);

                var subject = "Session " + session.SessionDate.ToString("dddd, MM/dd/yyyy, HH:mm") + " ACTIVITY";
                var body    = activity + "." + Environment.NewLine + Environment.NewLine;
                body += "Click here for the details: " + sessionurl + Environment.NewLine;
                foreach (var u in users)
                {
                    _emailServices.SendMail(subject, body, u.Email);
                }

                return(RedirectToAction("Details", "Sessions", new { id = buySell.SessionId }));
            }

            var newbuySell = new BuySell {
                SessionId = buySell.SessionId, BuyerUserId = User.Identity.GetUserId(), BuyerUser = buyer
            };

            return(View(newbuySell));
        }