public async Task <int> CreateNewTeam(DTONewTeam team)
        {
            try
            {
                var teamAlreadyCreated = _context.Team.Where(x => x.Captain.Id == team.PlayerId && x.Game.Id == team.GameId).FirstOrDefault();

                if (teamAlreadyCreated != null)
                {
                    return(teamAlreadyCreated.Id);
                }
                else
                {
                    var newTeam = new Team()
                    {
                        Captain     = _context.Player.Find(team.PlayerId),
                        Game        = _context.Game.Find(team.GameId),
                        TeamPlayers = new List <TeamPlayer>(),
                        TeamName    = team.TeamName
                    };
                    await _context.Team.AddAsync(newTeam);

                    await _context.SaveChangesAsync();

                    return(newTeam.Id);
                }
            }
            catch (Exception e)
            {
                return(0);
            }
        }
Beispiel #2
0
        public async Task <bool> CreateNewGame(DTONewGame game)
        {
            try
            {
                var course = await _context.Course.SingleAsync(c => c.Id == game.CourseId);

                var gameType = await _context.GameType.SingleAsync(g => g.Id == game.GameTypeId);

                var newGame = new Game
                {
                    GameName = game.GameName,
                    Course   = course,
                    // Hash Password ?
                    Password  = game.GamePassword,
                    GameType  = gameType,
                    CreatedOn = DateTime.Now
                };
                await _context.Game.AddAsync(newGame);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Beispiel #3
0
        public async Task <bool> CreateNewGameByPlayerId(DTONewPlayerGame newPlayerGame)
        {
            try
            {
                var game = await _context.Game.Include("Course.Holes").SingleAsync(g => g.Id == newPlayerGame.GameId);

                //    var courseHole = await _context.Course.Include(h => h.Holes).FirstOrDefaultAsync(c => c.Id == game.Course);
                var player = await _context.Player.SingleAsync(p => p.Id == newPlayerGame.PlayerId);

                var playerHoleScoreList = new List <PlayerHoleScore>();
                var pscors = _context.Score.Where(x => x.Player.Id == newPlayerGame.PlayerId && x.Game.Id == newPlayerGame.GameId).SingleOrDefault();

                if (pscors == null)
                {
                    foreach (var hole in game.Course.Holes)
                    {
                        var playerScore = new PlayerHoleScore
                        {
                            Player = player,
                            Hole   = hole,
                            Points = 0,
                            Score  = 0
                        };
                        playerHoleScoreList.Add(playerScore);
                    }
                    var score = new Score
                    {
                        Player = player,
                        Game   = game,
                        Holes  = playerHoleScoreList
                    };

                    await _context.AddAsync(score);

                    await _context.SaveChangesAsync();

                    return(true);
                }
                else
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                return(false);
            }
        }