Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Id,Name,MinutesPlayed")] Referee referee)
        {
            if (ModelState.IsValid)
            {
                _context.Add(referee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(referee));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("TeamName")] Team team)
        {
            if (ModelState.IsValid)
            {
                _context.Add(team);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(team));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Id,Name,YellowCards,RedCards,MinutesPlayed", "TeamId")] Manager manager)
        {
            if (ModelState.IsValid)
            {
                _context.Add(manager);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TeamId"] = new SelectList(_context.Set <Team>(), "Id", "Name", "TeamName");
            return(View(manager));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Name,HouseTeamManagerId,AwayTeamManagerId,RefereeId,Date")] Match match)
        {
            if (ModelState.IsValid)
            {
                _context.Add(match);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AwayTeamManagerId"]  = new SelectList(_context.Manager, "Id", "Name", match.AwayTeamManagerId);
            ViewData["HouseTeamManagerId"] = new SelectList(_context.Manager, "Id", "Name", match.HouseTeamManagerId);
            ViewData["RefereeId"]          = new SelectList(_context.Referee, "Id", "Name", match.RefereeId);
            return(View(match));
        }
Exemplo n.º 5
0
        public async Task <PlayerResponse> AddPlayer(PlayerRequest player)
        {
            // Important:
            // Validate Team, Add if not exists....
            await this.CheckTeamAsync(player.TeamName);

            var mapper = _mappingConfiguration.CreateMapper();

            var _player = mapper.Map <Player>(player);

            await _context.Player.AddAsync(_player);

            await _context.SaveChangesAsync();

            return(mapper.Map <PlayerResponse>(_player));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> PutManager(int id, ManagerRequest manager)
        {
            // manager = ManagerRequest(manager, id);

            if (manager == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var existingManager = await _context.Manager.FindAsync(Math.Abs(id));

            if (existingManager == null)
            {
                return(NotFound());
            }

            var mapper = _mappingConfiguration.CreateMapper();

            try
            {
                // Important:
                // Validate Team, Add if not exists....
                await this.CheckTeamAsync(manager.TeamName);

                mapper.Map(manager, existingManager);
                _context.Manager.Update(existingManager);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ManagerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
            // return NoContent();
        }
Exemplo n.º 7
0
        public async Task <IActionResult> PutReferee(int id, RefereeRequest referee)
        {
            // TODO: Refactor in Request and Reponce (Quico fix)
            // referee.Id = id;

            if (referee == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var existingReferee = await _context.Referee.FindAsync(id);

            if (existingReferee == null)
            {
                return(NotFound());
            }

            var mapper = _mappingConfiguration.CreateMapper();

            // _context.Entry(referee).State = EntityState.Modified;

            try
            {
                mapper.Map(referee, existingReferee);
                _context.Referee.Update(existingReferee);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RefereeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 8
0
        public async Task <MatchResponse> AddMatchAsync(MatchRequest match)
        {
            // TODO:
            // 2. Test
            // 3. Refacture
            ICollection <int> housePlayersId = match.HouseTeam.Where(c => c > 0).ToHashSet(); // To ensure unique players .ToHashSet();
            ICollection <int> awayPlayersId  = match.AwayTeam.Where(c => c > 0).ToHashSet();  // .ToHashSet();
            var houseManagerId = match.HouseTeam.Where(c => c < 0).FirstOrDefault();          // By convention, mnagers have negative id (TODO: Refacotr)
            var awayManagerId  = match.AwayTeam.Where(c => c < 0).FirstOrDefault();           // By convention, mnagers have negative id (TODO: Refacotr)

            houseManagerId = Math.Abs(houseManagerId);
            awayManagerId  = Math.Abs(awayManagerId);
            // Validate
            if (houseManagerId == awayManagerId)
            {
                return(null);
            }
            if (housePlayersId.Count() != PlayersPerMatch || awayPlayersId.Count() != PlayersPerMatch)
            {
                return(null);
            }

            var houseManager = await _context.Manager.FindAsync(houseManagerId);

            if (houseManager == null)
            {
                return(null);
            }
            var awayManager = await _context.Manager.FindAsync(awayManagerId);

            if (houseManager == null)
            {
                return(null);
            }
            var referee = await _context.Referee.FindAsync(match.Referee);

            if (referee == null)
            {
                return(null);
            }

            var _match = new Match
            {
                Name = match.Name,
                HouseTeamManagerId = houseManagerId,
                AwayTeamManagerId  = awayManagerId,
                RefereeId          = match.Referee,
                Date = match.Date,
            };

            // Cimented for in memeory DB
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    await _context.Match.AddAsync(_match);

                    await _context.SaveChangesAsync();

                    foreach (int playerId in housePlayersId)
                    {
                        var player = await _context.Player.FindAsync(playerId);

                        if (player == null)
                        {
                            throw new ArgumentException("Player not found");
                        }
                        if (player.TeamName != houseManager.TeamName)
                        {
                            throw new ArgumentException("Team not Match");
                        }

                        var matchInDataBase = _context.MatchPlayersHouse.Where(
                            s =>
                            s.Match.Id == _match.Id &&
                            s.Player.Id == playerId
                            ).SingleOrDefault();

                        if (matchInDataBase == null)
                        {
                            await _context.MatchPlayersHouse.AddAsync(new MatchPlayersHouse { MatchId = _match.Id, PlayerId = playerId });
                        }
                    }

                    foreach (int playerId in awayPlayersId)
                    {
                        var player = await _context.Player.FindAsync(playerId);

                        if (player == null)
                        {
                            throw new ArgumentException("Player not found");
                        }
                        if (player.TeamName != awayManager.TeamName)
                        {
                            throw new ArgumentException("Team not Match");
                        }

                        var matchInDataBase = _context.MatchPlayersAway.Where(
                            s =>
                            s.Match.Id == _match.Id &&
                            s.Player.Id == playerId
                            ).SingleOrDefault();

                        if (matchInDataBase == null)
                        {
                            await _context.MatchPlayersAway.AddAsync(new MatchPlayersAway { MatchId = _match.Id, PlayerId = playerId });
                        }
                    }

                    await _context.SaveChangesAsync();

                    // Commit transaction if all commands succeed, transaction will auto-rollback
                    // when disposed if either commands fails
                    transaction.Commit();
                }
                catch (Exception)
                {
                    // TODO: Handle failure
                    return(null);
                }
            }
            var mapper = _mappingConfiguration.CreateMapper();

            return(mapper.Map <MatchResponse>(_match));
        }