Example #1
0
        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);
        }
Example #2
0
 partial void DeleteUser(User instance);
Example #3
0
 partial void InsertUser(User instance);
Example #4
0
 partial void UpdateUser(User instance);
Example #5
0
 /// <summary>
 /// Adds a user to the database and returns the user object.
 /// </summary>
 /// <param name="username">The username for the user to add.</param>
 /// <returns>The added user object.</returns>
 private static User AddUser(string username)
 {
     var ip = new UserIP { CreatedDate = DateTime.UtcNow, HashedIP = Utilities.GetHash(HttpContext.Current.Request.UserHostAddress) };
     var user = new User { Username = username, RecordCreatedDate = DateTime.UtcNow };
     user.UserIPs.Add(ip);
     var db = new PiFDbDataContext();
     db.Users.InsertOnSubmit(user);
     db.SubmitChanges();
     return user;
 }