예제 #1
0
        public async Task <IResponse> AssignTeamToLeagueAsync(int leagueId, int teamId)
        {
            Logger?.LogInformation($"{nameof(AssignTeamToLeagueAsync)} has been invoked");

            var response = new SimpleResponse();

            try
            {
                var league = await GetAndValidateItem(leagueId);

                var team = await TeamRepository.GetItemByIdAsync(teamId);

                if (team == null)
                {
                    throw new FlmException($"Team with id={teamId} doesn't exist");
                }

                var existingAssignment = await LeagueRepository.GetTeamLeagueAssignments()
                                         .FirstOrDefaultAsync(tla => tla.TeamId == teamId && tla.LeagueId == leagueId);

                if (existingAssignment != null)
                {
                    throw new FlmException("Team is already assigned to this league");
                }

                await LeagueRepository.AddTeamAssignmentAsync(new TeamLeagueAssignment()
                {
                    LeagueId = leagueId,
                    TeamId   = teamId
                });

                // TODO: As we really do not need a result of this task call it in background. Consider using Hangfire.
                await RecalculateLeagueStandingsAsync(leagueId);
            }
            catch (Exception ex)
            {
                response.SetError(ex, Logger);
            }

            return(response);
        }