예제 #1
0
        public async Task <ActionResult> Create([FromForm] Player collection)
        {
            try
            {
                // account number can't be blank, so if it is, that means we need
                // to generate a new account number for this player
                if (string.IsNullOrEmpty(collection.AccountNumber))
                {
                    var rnd             = new Random();
                    var generatedNumber = rnd.Next(100000, 999999);

                    // check the players table to see if this number is already in-use
                    var accountNumberUsed = _dbContext.Players.FirstOrDefault(p => p.AccountNumber.Equals(generatedNumber));
                    if (accountNumberUsed == null)
                    {
                        collection.AccountNumber = generatedNumber.ToString();
                    }
                }

                _dbContext.Players.Add(collection);
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }
예제 #2
0
        public async Task <ActionResult> NewGame([FromForm] CreatePoolGameModel collection) //IFormCollection collection)
        {
            try
            {
                var postObj = collection.poolGame;
                postObj.PlayerOne = _dbContext.Players.Find(collection.poolGame.PlayerOne.Id);
                postObj.PlayerTwo = _dbContext.Players.Find(collection.poolGame.PlayerTwo.Id);
                postObj.Winner    = _dbContext.Players.Find(collection.poolGame.Winner.Id);

                _dbContext.PoolGames.Add(postObj);
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(NewGame)));
            }
            catch
            {
                return(View());
            }
        }