Exemplo n.º 1
0
        public IActionResult GetLeague(Guid id)
        {
            if (!_leagueRepository.LeagueExists(id))
            {
                _logger.LogWarning($"No league found for {id}.");
                return(NotFound());
            }

            var league = _leagueRepository.GetLeague(id);

            var leagueModel = _mapper.Map <LeagueModel>(league);

            return(Ok(leagueModel));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetLeague(int id)
        {
            var league = await leagueRepository.GetLeague(id);

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

            var leagueToReturn = mapper.Map <LeagueForListDto>(league);

            leagueToReturn.UserCount = await leagueRepository.CountUsers(league.Id);

            return(Ok(leagueToReturn));
        }
Exemplo n.º 3
0
        public IActionResult CreateDraft(Guid leagueId)
        {
            if (!_leagueRepository.LeagueExists(leagueId))
            {
                _logger.LogWarning($"No league found for {leagueId}.");
                return(NotFound());
            }

            var league = _leagueRepository.GetLeague(leagueId);

            if (league.Teams.Count() < league.MinTeams)
            {
                _logger.LogError("Does not exceed team minimum.");
                return(BadRequest());
            }

            using (var transaction = _context.Database.BeginTransaction()){
                try
                {
                    //Add draft
                    var draftModel = new DraftModel();
                    draftModel.StartDate = DateTime.Now;
                    draftModel.LeagueId  = leagueId;
                    var draftEntity = _mapper.Map <Draft>(draftModel);
                    _draftRepository.AddDraft(draftEntity);

                    if (!_mlbDraftRepository.Save())
                    {
                        throw new Exception("Creating a draft failed on save.");
                    }

                    //Create draft Selections and draft team rosters
                    CreateDraftSelectionsAndRosters(draftEntity);
                    var draftToReturn = _mapper.Map <DraftModel>(draftEntity);
                    transaction.Commit();

                    return(CreatedAtRoute("GetDraft",
                                          new { leagueId = leagueId,
                                                id = draftToReturn.Id },
                                          draftToReturn));
                }
                catch (Exception) {
                    transaction.Rollback();
                    throw;
                }
            }
        }
        public IActionResult Get(int id)
        {
            var leagueFromRepo = _leagueRepository.GetLeague(id);

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

            var league = Mapper.Map <LeagueDto>(leagueFromRepo);

            return(Ok(league));
        }
Exemplo n.º 5
0
        public IActionResult CreateTeamForLeague(Guid leagueId, [FromBody] TeamCreateModel teamCreateModel)
        {
            if (teamCreateModel == null)
            {
                return(BadRequest());
            }

            if (!_leagueRepository.LeagueExists(leagueId))
            {
                return(NotFound());
            }

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

            var leagueEntity = _leagueRepository.GetLeague(leagueId);


            var teamEntity = _mapper.Map <Team>(teamCreateModel);

            teamEntity.League   = leagueEntity;
            teamEntity.LeagueId = leagueEntity.Id;

            var owner = _userRepository.GetUser(teamCreateModel.Owner).Result;

            if (owner == null)
            {
                return(BadRequest());
            }

            teamEntity.Owner   = owner;
            teamEntity.OwnerId = owner.UserName;

            _teamRepository.AddTeam(teamEntity);

            if (!_mlbDraftRepository.Save())
            {
                throw new Exception("Creating a team failed on save.");
            }

            var teamToReturn = _mapper.Map <TeamModel>(teamEntity);

            return(CreatedAtRoute("GetTeam",
                                  new { leagueId = leagueId, id = teamToReturn.Id },
                                  teamToReturn));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> GetLeague()
        {
            var league = await _repo.GetLeague();

            var leagueState = await _repo.GetLeagueStateForId(league.StateId);

            LeagueDto leagueDto = new LeagueDto
            {
                Id      = league.Id,
                StateId = league.StateId,
                Day     = league.Day,
                State   = leagueState.State,
                Year    = league.Year
            };

            return(Ok(leagueDto));
        }
Exemplo n.º 7
0
 public async Task <League> GetLeague(int id)
 {
     return(await _Leagues.GetLeague(id));
 }