async Task <List <Game> > IRequestHandler <GetRecommendedGamesCommand, List <Game> > .Handle(GetRecommendedGamesCommand command, CancellationToken cancellationToken) { var result = new List <Game>(); var games = _context.Games .Include(u => u.Users) .Include(c => c.Categories) .Include(g => g.Genres) .ToList(); var sortedByReleaseDate = from d in games orderby d.ReleaseDate descending select d; var sortedByTopSellers = from d in games orderby d.CountOfBuy descending select d; foreach (var item in sortedByReleaseDate) { if (games.Count < 6) { item.Users = new List <User>(); result.Add(item); } else { break; } } foreach (var item in sortedByTopSellers) { if (games.Count < 11) { if (!result.Exists(g => g.Id == item.Id)) { item.Users = new List <User>(); result.Add(item); } } else { break; } } return(result); }
public async Task <IResult <IReadOnlyCollection <GameWithImageResponse> > > HandleAsync(GetRecommendedGamesCommand command) { var validationResult = await IsValidAsync(command); if (validationResult.HasFailed()) { return(validationResult.Map <IReadOnlyCollection <GameWithImageResponse> >()); } var userId = command.UserId ?? command.CurrentUserId; var yesterday = DateTime.Now.AddDays(-1); var existingActualRecommendation = await _dataContext.GamesRecommendations .AsNoTracking() .Include(x => x.RecommendedGames) .FirstOrDefaultAsync(x => x.UserId == userId && x.GeneratedAt > yesterday); if (existingActualRecommendation != null && existingActualRecommendation.RecommendedGames.Count > 0) { var recommendedGamesIds = existingActualRecommendation.RecommendedGames.Select(x => x.GameId); var games = await _dataContext.Games .Include(x => x.CoverGameImage) .Where(x => recommendedGamesIds.Contains(x.Id)) .ToListAsync(); return(games.Select(x => new GameWithImageResponse { Category = x.GameCategory, Id = x.Id, ImageBytes = x.CoverGameImage.Data.ToList(), Title = x.Name }) .ToList() .ToSuccessfulResult()); } PythonScriptRunner.RunScript("PythonScripts/recommender.py", userId.ToString()); var lines = File.ReadAllLines("PythonScripts/list_of_games.txt"); var recommendedGames = new List <Game>(); foreach (var line in lines) { var game = await _dataContext.Games .Include(x => x.CoverGameImage) .SingleOrDefaultAsync(x => x.Name == line); if (game != null) { recommendedGames.Add(game); } } var gamesRecommendation = new GamesRecommendation { GeneratedAt = DateTime.Now, UserId = userId, RecommendedGames = recommendedGames .Select(x => new RecommendationEntry { Game = x }).ToList() }; _dataContext.GamesRecommendations.Add(gamesRecommendation); await _dataContext.SaveChangesAsync(); return(recommendedGames.Select(x => new GameWithImageResponse { Category = x.GameCategory, Id = x.Id, ImageBytes = x.CoverGameImage.Data.ToList(), Title = x.Name }) .ToList() .ToSuccessfulResult()); }
public async Task <ActionResult <string> > GetRecommendedGames([FromQuery] GetRecommendedGamesCommand command) { var result = await Mediator.Send(command); return(new JsonResult(result)); }
private Task <IResult> IsValidAsync(GetRecommendedGamesCommand query) { _validator.ValidateUserIds(query.CurrentUserId, query.UserId); return(_validator.ValidateAsync()); }