public CompletePiFModel(ThreadGame tg, string winnerUserName)
 {
     ID = tg.id;
     Name = tg.Game.Name;
     Count = 1;
     SteamID = tg.Game.SteamID;
     PointWorth = tg.Game.PointWorth;
     WinnerUserName = winnerUserName;
 }
        public ActionResult Complete(CompletePiFModel model)
        {
            Thread thread = AccountHelper.CurrentUser.Threads.SingleOrDefault(t => t.ThingID == model.ThingID);

            if (thread == null)
            {
                return RedirectToAction("Me", "Account");
            }

            if (ModelState.IsValid)
            {
                var db = new PiFDbDataContext();
                thread = db.Threads.Single(t => t.ThingID == model.ThingID);
                db.ThreadGames.DeleteAllOnSubmit(thread.ThreadGames);
                var newUsers = new List<User>();
                foreach (var pifgame in SessionCompleteGamesRepository.All())
                {
                    for (int i = 1; i <= pifgame.Count; i++)
                    {
                        if (pifgame.WinnerUserName == string.Empty)
                        {
                            ModelState.AddModelError("Winner", "All entries must have a winner selected");
                            break;
                        }

                        User user = db.Users.SingleOrDefault(u => u.Username == pifgame.WinnerUserName);
                        if (user == null && newUsers.Exists(u => u.Username == pifgame.WinnerUserName))
                        {
                            user = newUsers.Single(u => u.Username == pifgame.WinnerUserName);
                        }

                        if (user == null)
                        {
                            user = new User { Username = pifgame.WinnerUserName, RecordCreatedDate = DateTime.UtcNow };
                            db.Users.InsertOnSubmit(user);
                            newUsers.Add(user);
                        }

                        var tg = new ThreadGame { ThreadID = thread.id, GameID = pifgame.ID, User = user };
                        db.ThreadGames.InsertOnSubmit(tg);
                    }

                    if (!ModelState.IsValid)
                    {
                        break;
                    }
                }

                if (ModelState.IsValid)
                {
                    db.SubmitChanges();
                    return RedirectToAction("View", "PiF", new { thread.ThingID });
                }
            }

            return View(model);
        }
        public ActionResult Edit(EditPiFModel model)
        {
            Thread thread = AccountHelper.CurrentUser.Threads.SingleOrDefault(t => t.ThingID == model.ThingID);

            if (thread == null)
            {
                return RedirectToAction("Profile", "Account");
            }

            var db = new PiFDbDataContext();
            thread = db.Threads.Single(t => t.ThingID == model.ThingID);
            db.ThreadGames.DeleteAllOnSubmit(thread.ThreadGames);
            foreach (var pifgame in SessionEditGamesRepository.All())
            {
                for (int i = 1; i <= pifgame.Count; i++)
                {
                    var tg = new ThreadGame { Thread = thread, GameID = pifgame.ID };
                    db.ThreadGames.InsertOnSubmit(tg);
                }
            }

            db.SubmitChanges();

            return View(model);
        }
 partial void DeleteThreadGame(ThreadGame instance);
 partial void UpdateThreadGame(ThreadGame instance);
 partial void InsertThreadGame(ThreadGame instance);
		private void detach_ThreadGames(ThreadGame entity)
		{
			this.SendPropertyChanging();
			entity.User = null;
		}
		private void attach_ThreadGames(ThreadGame entity)
		{
			this.SendPropertyChanging();
			entity.User = this;
		}