Ejemplo n.º 1
0
        public async Task <ActionResult> CopyGolfersFromLastGame(Guid matchid, Guid gameid)
        {
            Game existingGame = await _gameRepo.FindByID(gameid);

            IEnumerable <GameGolfer> lastGame = await _gameGolferRepo.FindLatestGameandGolfersInMatch(matchid, gameid);

            Guid id = matchid;

            foreach (GameGolfer item in lastGame)
            {
                bool isExists = await _gameGolferRepo.CheckGolferInGame(gameid, item.GolferId);

                if (!isExists)
                {
                    GameGolferViewModel newGolferInGame = new GameGolferViewModel
                    {
                        Game   = existingGame,
                        Golfer = item.Golfer,
                        Score  = 0,
                    };
                    var addGolfer = _mapper.Map <GameGolfer>(newGolferInGame);
                    await _gameGolferRepo.Create(addGolfer);
                }
            }
            return(RedirectToAction(nameof(Details), new { id }));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> AddGolferToGame(Guid id, Guid golferid)
        {
            var isExists = await _gameGolferRepo.CheckGolferInGame(id, golferid);

            if (!isExists)
            {
                Golfer golfer = await _golferRepo.FindByID(golferid);

                Game game = await _gameRepo.FindByID(id);

                GameGolferViewModel newGolferInGame = new GameGolferViewModel
                {
                    Game         = game,
                    Golfer       = golfer,
                    GameHandicap = golfer.Handicap,
                };
                var addGolfer = _mapper.Map <GameGolfer>(newGolferInGame);
                await _gameGolferRepo.Create(addGolfer);
            }

            return(RedirectToAction(nameof(ListGolfers), new { id }));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Create(GameViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Create Game: Model state is invalid");
                    return(View(model));
                }
                if (model.MatchId == null)
                {
                    ModelState.AddModelError("", "Create Game: No Match ID");
                    return(View(model));
                }

                model.Id = Guid.NewGuid();
                List <Game> gamesInMatch = await _gameRepo.GetGamesInMatch(model.MatchId);

                int numberOfGames = gamesInMatch.Count();
                var match         = await _matchRepo.FindByID(model.MatchId);

                //var game = _mapper.Map<Game>(model);
                var game = new Game
                {
                    Id       = model.Id,
                    GameDate = model.GameDate,
                    Match    = match,
                    Location = model.Location
                };

                var isSuccess = await _gameRepo.Create(game);

                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Create Game: Unable to Create a new game");
                    return(View(model));
                }
                bool gameGolfersExist = await _gameGolferRepo.CheckGame(game.Id);

                if (!gameGolfersExist && numberOfGames > 0)
                {
                    List <GameGolfer> lastGame = await _gameGolferRepo.FindLatestGameandGolfersInMatch(game.MatchId, game.Id);

                    foreach (GameGolfer item in lastGame)
                    {
                        bool isExists = await _gameGolferRepo.CheckGolferInGame(game.Id, item.GolferId);

                        if (!isExists)
                        {
                            GameGolferViewModel newGolferInGame = new GameGolferViewModel
                            {
                                Game         = game,
                                Golfer       = item.Golfer,
                                GameHandicap = item.GameHandicap,
                            };
                            var  addGolfer     = _mapper.Map <GameGolfer>(newGolferInGame);
                            bool golferSuccess = await _gameGolferRepo.Create(addGolfer);

                            if (!golferSuccess)
                            {
                                ModelState.AddModelError("", "Create Game: Unable to Copy a previous list of Golfers");
                                continue;
                            }
                        }
                    }
                }
                Guid id = game.MatchId;
                return(RedirectToAction(nameof(Details), "Match", new { id }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Create Game: Something went wrong in the game creation");
                return(RedirectToAction(nameof(Index), "Match"));
            }
        }