예제 #1
0
        public async Task <IActionResult> CreateSquad([FromBody] SquadCreateDto squadCreateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var squadToCreate = _mapper.Map <Squad>(squadCreateDto);

            var existSquad = await _squadService
                             .GetDetailBySeasonIdAndClubIdAsync(squadCreateDto.SeasonId, squadCreateDto.ClubId);

            if (existSquad != null)
            {
                return(BadRequest());
            }

            await _squadService.CreateAsync(squadToCreate);

            var squad = await _squadService.GetDetailByIdAsync(squadToCreate.Id);

            var returnSquad = _mapper.Map <SquadDetailDto>(squad);

            return(Ok(returnSquad));
        }
        public async Task <IActionResult> GetPlayersInSquad(int squadId,
                                                            [FromQuery] SquadPlayerQuery squadPlayerQuery)
        {
            squadPlayerQuery.SquadId = squadId;
            if (squadPlayerQuery.SeasonId.HasValue && squadPlayerQuery.ClubId.HasValue)
            {
                var squad = await _squadService
                            .GetDetailBySeasonIdAndClubIdAsync(squadPlayerQuery.SeasonId.Value, squadPlayerQuery.ClubId.Value);

                squadPlayerQuery.SquadId = squad.Id;
            }

            var players = await _squadService.GetPlayersInSquadAsync(squadPlayerQuery);

            var returnPlayers = _mapper.Map <PaginatedList <SquadPlayerListDto> >(players);

            return(Ok(returnPlayers));
        }
예제 #3
0
        public async Task <IActionResult> CreateCard(int matchId, [FromBody] CardCreateDto cardCreateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var match = await _matchService.GetDetailByIdAsync(matchId);

            var player = await _playerService.GetByIdAsync(cardCreateDto.PlayerId);

            if (match == null || player == null)
            {
                return(BadRequest());
            }

            if (cardCreateDto.ClubId != match.HomeClubId && cardCreateDto.ClubId != match.AwayClubId)
            {
                return(BadRequest());
            }

            var cardToCreate = _mapper.Map <Card>(cardCreateDto);

            cardToCreate.MatchId = matchId;

            var homeClubSquad = await _squadService
                                .GetDetailBySeasonIdAndClubIdAsync(match.SeasonId, match.HomeClubId);

            var awayClubSquad = await _squadService
                                .GetDetailBySeasonIdAndClubIdAsync(match.SeasonId, match.AwayClubId);

            bool isHomePlayer;

            if (player.SquadPlayers.Any(sp => sp.SquadId == homeClubSquad.Id))
            {
                isHomePlayer = true;
            }
            else if (player.SquadPlayers.Any(sp => sp.SquadId == awayClubSquad.Id))
            {
                isHomePlayer = false;
            }
            else
            {
                return(BadRequest());
            }

            if ((isHomePlayer && cardCreateDto.ClubId != match.HomeClubId) ||
                (!isHomePlayer && cardCreateDto.ClubId != match.AwayClubId))
            {
                return(BadRequest());
            }

            var existRedCard = match.Cards
                               .SingleOrDefault(c => c.CardType == CardType.Red && c.PlayerId == player.Id);

            if (existRedCard != null)
            {
                return(BadRequest());
            }

            var existYellowCard = match.Cards
                                  .SingleOrDefault(c => c.CardType == CardType.Yellow && c.PlayerId == player.Id);

            Card redCardToCreate = null;

            if (existYellowCard != null && cardToCreate.CardType == CardType.Yellow)
            {
                redCardToCreate = new Card
                {
                    MatchId  = matchId,
                    ClubId   = cardToCreate.ClubId,
                    PlayerId = cardToCreate.PlayerId,
                    CardType = CardType.Red,
                    CardTime = cardToCreate.CardTime
                };
            }

            await _cardService.CreateAsync(cardToCreate);

            if (redCardToCreate != null)
            {
                await _cardService.CreateAsync(redCardToCreate);
            }

            var card = await _cardService.GetDetailByIdAsync(cardToCreate.Id);

            var returnCard = _mapper.Map <CardDetailDto>(card);

            return(Ok(returnCard));
        }
예제 #4
0
        public async Task <IActionResult> CreateGoal(int matchId, [FromBody] GoalCreateDto goalCreateDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var match = await _matchService.GetByIdAsync(matchId);

            var player = await _playerService.GetByIdAsync(goalCreateDto.PlayerId);

            if (match == null || player == null)
            {
                return(BadRequest());
            }

            if (goalCreateDto.ClubId != match.HomeClubId && goalCreateDto.ClubId != match.AwayClubId)
            {
                return(BadRequest());
            }

            var goalToCreate = _mapper.Map <Goal>(goalCreateDto);

            goalToCreate.MatchId = matchId;

            var homeClubSquad = await _squadService
                                .GetDetailBySeasonIdAndClubIdAsync(match.SeasonId, match.HomeClubId);

            var awayClubSquad = await _squadService
                                .GetDetailBySeasonIdAndClubIdAsync(match.SeasonId, match.AwayClubId);

            bool isHomePlayer;

            if (player.SquadPlayers.Any(sp => sp.SquadId == homeClubSquad.Id))
            {
                isHomePlayer = true;
            }
            else if (player.SquadPlayers.Any(sp => sp.SquadId == awayClubSquad.Id))
            {
                isHomePlayer = false;
            }
            else
            {
                return(BadRequest());
            }

            if ((isHomePlayer && goalCreateDto.ClubId != match.HomeClubId) ||
                (!isHomePlayer && goalCreateDto.ClubId != match.AwayClubId))
            {
                goalToCreate.IsOwnGoal = true;
            }
            else
            {
                goalToCreate.IsOwnGoal = false;
            }

            await _goalService.CreateAsync(goalToCreate);

            var goal = await _goalService.GetDetailByIdAsync(goalToCreate.Id);

            var returnGoal = _mapper.Map <GoalDetailDto>(goal);

            return(Ok(returnGoal));
        }