public async Task <IActionResult> PutHistoricPlayer(int id, HistoricPlayer historicPlayer)
        {
            if (id != historicPlayer.HistoricPlayerID)
            {
                return(BadRequest());
            }

            _context.Entry(historicPlayer).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HistoricPlayerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutNbaTeam(int id, NbaTeam nbaTeam)
        {
            if (id != nbaTeam.NbaTeamID)
            {
                return(BadRequest());
            }

            _context.Entry(nbaTeam).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!NbaTeamExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> Create([Bind("NbaTeamID,NbaTeamName,NbaTeamConference")] NbaTeam nbaTeam)
        {
            if (ModelState.IsValid)
            {
                _context.Add(nbaTeam);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(nbaTeam));
        }
        public async Task <IActionResult> Create([Bind("TeamId,TeamName,TeamChampionships,TeamFinaslAppearances,TeamConference")] Team team)
        {
            if (ModelState.IsValid)
            {
                _context.Add(team);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(team));
        }
Пример #5
0
        public async Task <IActionResult> Create([Bind("CurrentPlayerID,CurrentPlayerName,CurrentPlayerPosition,CurrentPlayerDOB,NbaTeamID")] CurrentPlayer currentPlayer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(currentPlayer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["NbaTeamID"] = new SelectList(_context.NbaTeams, "NbaTeamID", "NbaTeamName", currentPlayer.NbaTeamID);
            return(View(currentPlayer));
        }
        public async Task <IActionResult> Create([Bind("HistoricPlayerID,HistoricPlayerName,HistoricPlayerPosition,HistoricPlayerDOB,PlayerRetired,NbaTeamID")] HistoricPlayer historicPlayer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(historicPlayer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["NbaTeamID"] = new SelectList(_context.NbaTeams, "NbaTeamID", "NbaTeamName", historicPlayer.NbaTeamID);
            return(View(historicPlayer));
        }
Пример #7
0
        public async Task <IActionResult> Create([Bind("PlayerId,TeamId,LastName,FirstName,Position,PlayerPointsScored,AllStarGames,VideoLink,PhotoLink")] Player player)
        {
            if (ModelState.IsValid)
            {
                _context.Add(player);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TeamId"] = new SelectList(_context.Team, "TeamId", "TeamId", player.TeamId);
            return(View(player));
        }
Пример #8
0
        public async Task <IActionResult> PatchTeam([FromRoute] int id, [FromBody] TeamReqModel teamReq)
        {
            // Final request model validation
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var team = teamReq.ConvertToTeamModel();

            team.Id = id;

            var existingTeam = await TeamExistsAsync(team.Name);

            if (existingTeam)
            {
                return(BadRequest($"Team name: {team.Name} already exists."));
            }

            _context.Entry(team).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TeamExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok());
        }
Пример #9
0
        public async Task <IActionResult> PutPlayer([FromBody] PlayerListVm playerList)
        {
            // Final validation of request model
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var players = playerList.ConvertToListOfPlayerModels();

            foreach (Player p in players)
            {
                // Validate EF model
                TryValidateModel(p);
                if (!ModelState.IsValid)
                {
                    BadRequest(ModelState);
                }

                if (!PlayerExists(p.Id))
                {
                    return(NotFound($"Player Id: {p.Id} doesn't exist."));
                }
            }

            _context.Player.UpdateRange(players);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return(Ok(playerList));
        }
Пример #10
0
        public async Task <IActionResult> PostTeamPlayer([FromRoute] int teamId, [FromBody] TeamPlayerListVm teamPlayerList)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Validate that adding these requested players to a team would not pass the MAX_PLAYER limit.
            var teamPlayerListCount = teamPlayerList.Players.Count();
            var dbCount             = _context.TeamPlayer.AsNoTracking().Where(tp => tp.TeamId == teamId).Count();

            if (dbCount + teamPlayerListCount > MAX_PLAYERS)
            {
                return(BadRequest($"Adding {teamPlayerListCount} players from request would exceed the max player limit of {MAX_PLAYERS}."));
            }

            var teamPlayers = teamPlayerList.ConvertToListOfTeamPlayerModels();

            foreach (TeamPlayer tp in teamPlayers)
            {
                TryValidateModel(tp);
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
            }

            // Validation for duplicate PlayerIds
            var distinctPlayers = teamPlayers.Select(tp => tp.PlayerId).Distinct().ToArray();

            if (distinctPlayers.Count() != teamPlayers.Count())
            {
                return(BadRequest("At least 1 duplicate playerId in request."));
            }
            // Validation for multiple teamIds
            var distinctTeams = teamPlayers.Where(tp => tp.TeamId != teamId).Select(tp => tp.TeamId).ToArray();

            if (distinctTeams.Count() > 0)
            {
                return(BadRequest("More than 1 teamId in request."));
            }

            _context.TeamPlayer.AddRange(teamPlayers);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 2627)
            {
                var filteredPlayerIds = await _context.TeamPlayer.AsNoTracking().Where(tp => tp.TeamId == teamId).Select(tp => tp.PlayerId).ToArrayAsync();

                var filteredRequestPlayerIds = teamPlayers.Select(tp => tp.PlayerId).ToArray();
                var existing = filteredPlayerIds.Where(f => filteredRequestPlayerIds.Contains(f)).Select(f => f).ToArray();
                var message  = SqlExceptionHandlers.DuplicateCompositePrimaryKeyException(ex, existing);

                return(BadRequest(message));
            }
            catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 547)
            {
                return(BadRequest($"Team Id or Player Id doesn't exist."));
            }

            // Create the TeamPlayer list with all Player data from the database
            var playerIds = teamPlayers.Select(tp => tp.PlayerId).ToArray();
            var players   = await _context.TeamPlayer
                            .Where(tp => tp.TeamId == teamId && playerIds.Contains(tp.PlayerId))
                            .Include(tp => tp.Player)
                            .ToListAsync();

            return(CreatedAtAction("GetTeamPlayer", new { id = teamId }, new TeamPlayerListVm(players)));
        }