public async Task <List <MindfightResultDto> > GetTeamResults(long teamId) { var currentTeam = await _teamRepository .GetAll() .FirstOrDefaultAsync(x => x.Id == teamId); if (currentTeam == null) { throw new UserFriendlyException("Komanda su nurodytu id neegzistuoja!"); } var registeredTeamResults = await _resultRepository .GetAllIncluding(result => result.Team, result => result.Mindfight) .OrderByDescending(result => result.Mindfight.StartTime) .Where(x => x.TeamId == teamId) .ToListAsync(); var teamResultsDto = new List <MindfightResultDto>(); foreach (var teamResult in registeredTeamResults) { var currentMindfight = await _mindfightRepository .GetAllIncluding(mindfight => mindfight.Tours) .FirstOrDefaultAsync(x => x.Id == teamResult.MindfightId); if (currentMindfight == null) { continue; } var mindfightQuestions = await _questionRepository .GetAllIncluding(question => question.Tour) .Where(question => question.Tour.MindfightId == currentMindfight.Id) .ToListAsync(); var totalPoints = mindfightQuestions.Sum(question => question.Points); var resultDto = new MindfightResultDto(); _objectMapper.Map(teamResult, resultDto); resultDto.MindfightStartTime = currentMindfight.StartTime; resultDto.MindfightName = currentMindfight.Title; resultDto.MindfightId = currentMindfight.Id; resultDto.TeamId = teamResult.TeamId; resultDto.ToursCount = currentMindfight.Tours.Count; resultDto.TeamName = teamResult.Team.Name; resultDto.TotalPoints = totalPoints; resultDto.IsMindfightFinished = currentMindfight.IsFinished; teamResultsDto.Add(resultDto); } return(teamResultsDto); }
public async Task <List <MindfightResultDto> > GetUserResults(long userId) { var user = await _userManager.Users.IgnoreQueryFilters().FirstOrDefaultAsync(u => u.Id == userId); if (user == null) { throw new UserFriendlyException("Vartotojas neegzistuoja!"); } var mindfightResults = await _resultRepository .GetAllIncluding(result => result.Team, result => result.Mindfight) .OrderByDescending(result => result.Mindfight.StartTime) .Where(x => x.Users.Any(player => player.UserId == userId)) .ToListAsync(); var resultsDto = new List <MindfightResultDto>(); foreach (var result in mindfightResults) { var currentMindfight = await _mindfightRepository .GetAllIncluding(mindfight => mindfight.Tours) .FirstOrDefaultAsync(x => x.Id == result.MindfightId); if (currentMindfight == null) { continue; } var mindfightQuestions = await _questionRepository .GetAllIncluding(question => question.Tour) .Where(question => question.Tour.MindfightId == currentMindfight.Id) .ToListAsync(); var totalPoints = mindfightQuestions.Sum(question => question.Points); var resultDto = new MindfightResultDto(); _objectMapper.Map(result, resultDto); resultDto.MindfightStartTime = currentMindfight.StartTime; resultDto.MindfightName = currentMindfight.Title; resultDto.MindfightId = currentMindfight.Id; resultDto.TeamId = result.TeamId; resultDto.ToursCount = currentMindfight.Tours.Count; resultDto.TeamName = result.Team.Name; resultDto.TotalPoints = totalPoints; resultDto.IsMindfightFinished = currentMindfight.IsFinished; resultsDto.Add(resultDto); } return(resultsDto); }
public async Task <MindfightResultDto> GetMindfightTeamResult(long mindfightId, long teamId) { var currentMindfight = await _mindfightRepository .GetAllIncluding(mindfight => mindfight.Tours) .FirstOrDefaultAsync(x => x.Id == mindfightId); if (currentMindfight == null) { throw new UserFriendlyException("Protmūšis su nurodytu id neegzistuoja!"); } var currentTeam = await _teamRepository .GetAll() .FirstOrDefaultAsync(x => x.Id == teamId); if (currentTeam == null) { throw new UserFriendlyException("Komanda su nurodytu id neegzistuoja!"); } var currentResult = await _resultRepository .GetAll() .FirstOrDefaultAsync(x => x.MindfightId == mindfightId && x.TeamId == teamId); if (currentResult == null) { throw new UserFriendlyException("Protmūšio rezultatas neegizstuoja"); } var mindfightQuestions = await _questionRepository .GetAllIncluding(question => question.Tour) .Where(question => question.Tour.MindfightId == mindfightId) .ToListAsync(); var totalPoints = mindfightQuestions.Sum(question => question.Points); var resultDto = new MindfightResultDto(); _objectMapper.Map(currentResult, resultDto); resultDto.MindfightStartTime = currentMindfight.StartTime; resultDto.MindfightName = currentMindfight.Title; resultDto.MindfightId = mindfightId; resultDto.TeamId = teamId; resultDto.ToursCount = currentMindfight.Tours.Count; resultDto.TeamName = currentTeam.Name; resultDto.TotalPoints = totalPoints; resultDto.IsMindfightFinished = currentMindfight.IsFinished; return(resultDto); }
public async Task <List <MindfightResultDto> > GetMindfightResults(long mindfightId) { var currentMindfight = await _mindfightRepository .GetAllIncluding(mindfight => mindfight.Tours) .FirstOrDefaultAsync(x => x.Id == mindfightId); if (currentMindfight == null) { throw new UserFriendlyException("Protmūšis su nurodytu id neegzistuoja!"); } var mindfightQuestions = await _questionRepository .GetAllIncluding(question => question.Tour) .Where(question => question.Tour.MindfightId == mindfightId) .ToListAsync(); var totalPoints = mindfightQuestions.Sum(question => question.Points); var registeredTeamResults = await _resultRepository .GetAllIncluding(x => x.Team) .Where(x => x.MindfightId == mindfightId) .ToListAsync(); var teamResultsDto = new List <MindfightResultDto>(); foreach (var teamResult in registeredTeamResults) { var resultDto = new MindfightResultDto(); _objectMapper.Map(teamResult, resultDto); resultDto.MindfightStartTime = currentMindfight.StartTime; resultDto.MindfightName = currentMindfight.Title; resultDto.MindfightId = mindfightId; resultDto.TeamId = teamResult.TeamId; resultDto.ToursCount = currentMindfight.Tours.Count; resultDto.TeamName = teamResult.Team.Name; resultDto.TotalPoints = totalPoints; resultDto.IsMindfightFinished = currentMindfight.IsFinished; teamResultsDto.Add(resultDto); } teamResultsDto.Sort((x, y) => x.Place.CompareTo(y.Place)); return(teamResultsDto); }