Beispiel #1
0
        public ActionResult <Game> Create(Game game)
        {
            using var gameDataContext = new GameDataContext();
            using var matchContext    = new MatchContext();
            long id = (gameDataContext.GameDatas.Max(m => (int?)m.Id) ?? 0) + 1;

            game.Id = id;
            if (game.Date == null)
            {
                game.Date = DateTime.Now.ToString("u", System.Globalization.CultureInfo.InvariantCulture);
            }
            gameDataContext.Add(new GameData {
                Id   = id,
                Date = game.Date,
            });
            gameDataContext.SaveChanges();
            var matches = new List <Match>();

            foreach (var player in game.Team1)
            {
                matches.Add(new Match
                {
                    GameId = id,
                    Player = player,
                    Team   = 1,
                });
            }
            foreach (var player in game.Team2)
            {
                matches.Add(new Match
                {
                    GameId = id,
                    Player = player,
                    Team   = 2,
                });
            }
            matchContext.AddRange(matches);
            matchContext.SaveChanges();
            using var scoreContext = new ScoreContext();
            float?result = null;

            if (game.Score == '1')
            {
                result = 1;
            }
            else if (game.Score == '2')
            {
                result = 0;
            }
            else if (game.Score == 'D')
            {
                result = 0.5f;
            }
            else if (game.Score == 'C')
            {
                result = -1;
            }
            scoreContext.Add(new Score
            {
                GameId = id,
                Result = result,
            });
            scoreContext.SaveChanges();
            return(CreatedAtAction(nameof(GetById), new { id = id }, game));
        }