public async Task <Response> GetPredictionsForUserAsync(string urlBase, string servicePrefix, string controller, PredictionsForUserRequest predictionsForUserRequest, string tokenType, string accessToken) { try { string request = JsonConvert.SerializeObject(predictionsForUserRequest); StringContent content = new StringContent(request, Encoding.UTF8, "application/json"); HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken); string url = $"{servicePrefix}{controller}"; HttpResponseMessage response = await client.PostAsync(url, content); string result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = result }); } List <PredictionResponse> list = JsonConvert.DeserializeObject <List <PredictionResponse> >(result); return(new Response { IsSuccess = true, Result = list }); }catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public async Task <IActionResult> GetPredictionsForUser([FromBody] PredictionsForUserRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } CultureInfo cultureInfo = new CultureInfo(request.CultureInfo); Resource.Culture = cultureInfo; TournamentEntity tournament = await _context.Tournaments.FindAsync(request.TournamentId); if (tournament == null) { return(BadRequest(Resource.TournamentDoesntExists)); } UserEntity userEntity = await _context.Users .Include(u => u.Team) .Include(u => u.Predictions) .ThenInclude(p => p.Match) .ThenInclude(m => m.Local) .Include(u => u.Predictions) .ThenInclude(p => p.Match) .ThenInclude(m => m.Visitor) .Include(u => u.Predictions) .ThenInclude(p => p.Match) .ThenInclude(p => p.Group) .ThenInclude(p => p.Tournament) .FirstOrDefaultAsync(u => u.Id == request.UserId.ToString()); if (userEntity == null) { return(BadRequest(Resource.UserDoesntExists)); } // Add precitions already done List <PredictionResponse> predictionResponses = new List <PredictionResponse>(); foreach (PredictionEntity predictionEntity in userEntity.Predictions) { if (predictionEntity.Match.Group.Tournament.Id == request.TournamentId) { predictionResponses.Add(_converterHelper.ToPredictionResponse(predictionEntity)); } } // Add precitions undone List <MatchEntity> matches = await _context.Matches .Include(m => m.Local) .Include(m => m.Visitor) .Where(m => m.Group.Tournament.Id == request.TournamentId) .ToListAsync(); foreach (MatchEntity matchEntity in matches) { PredictionResponse predictionResponse = predictionResponses.FirstOrDefault(pr => pr.Match.Id == matchEntity.Id); if (predictionResponse == null) { predictionResponses.Add(new PredictionResponse { Match = _converterHelper.ToMatchResponse(matchEntity), }); } } return(Ok(predictionResponses.OrderBy(pr => pr.Id).ThenBy(pr => pr.Match.Date))); }
public async Task <IActionResult> GetPredictionsForUser([FromBody] PredictionsForUserRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TournamentEntity tournament = await _context.Tournaments.FindAsync(request.TournamentId); if (tournament == null) { return(BadRequest("Este Torneo no existe.")); } Player player = await _context.Players .Include(u => u.User.FavoriteTeam) .ThenInclude(l => l.League) .Include(pr => pr.Predictions) .ThenInclude(p => p.Match) .ThenInclude(m => m.Local) .ThenInclude(l => l.League) .Include(pr => pr.Predictions) .ThenInclude(p => p.Match) .ThenInclude(m => m.Visitor) .ThenInclude(l => l.League) .Include(pr => pr.Predictions) .ThenInclude(p => p.Match) .ThenInclude(p => p.Group) .ThenInclude(p => p.Tournament) .FirstOrDefaultAsync(u => u.User.Id == request.UserId.ToString()); if (player == null) { return(BadRequest("Este Usuario no existe.")); } // Add precitions already done List <PredictionResponse> predictionResponses = new List <PredictionResponse>(); foreach (PredictionEntity predictionEntity in player.Predictions) { if (predictionEntity.Match.Group.Tournament.Id == request.TournamentId) { predictionResponses.Add(_converterHelper.ToPredictionResponse(predictionEntity)); } } // Add precitions undone List <MatchEntity> matches = await _context.Matches .Include(m => m.Local) .ThenInclude(l => l.League) .Include(m => m.Visitor) .ThenInclude(l => l.League) .Include(g => g.Group) .Include(d => d.DateName) .Where(m => m.Group.Tournament.Id == request.TournamentId) .ToListAsync(); foreach (MatchEntity matchEntity in matches) { PredictionResponse predictionResponse = predictionResponses.FirstOrDefault(pr => pr.Match.Id == matchEntity.Id); if (predictionResponse == null) { predictionResponses.Add(new PredictionResponse { Match = _converterHelper.ToMatchResponse(matchEntity), }); } } return(Ok(predictionResponses.OrderBy(pr => pr.Id).ThenBy(pr => pr.Match.Date))); }