Ejemplo n.º 1
0
        public async Task <IActionResult> GetRecs([FromBody] AnimeRecsInputJson input,
                                                  [FromServices] IOptionsSnapshot <Config.RecommendationsConfig> recConfig,
                                                  [FromServices] IMyAnimeListApiFactory malApiFactory, [FromServices] IAnimeRecsClientFactory recClientFactory,
                                                  [FromServices] IAnimeRecsDbConnectionFactory dbConnFactory, [FromServices] IRazorViewEngine viewEngine,
                                                  [FromServices] ITempDataProvider tempProvider)
        {
            if (!ModelState.IsValid)
            {
                AjaxError error = new AjaxError(ModelState);
                _logger.LogDebug("Invalid input received for GetRecs: {0}", error.Message);
                return(BadRequest(error));
            }

            if (input.RecSourceName == null)
            {
                input.RecSourceName = recConfig.Value.DefaultRecSource;
            }

            try
            {
                MalUserLookupResults userLookup = await GetUserLookupAsync(input, malApiFactory).ConfigureAwait(false);

                Dictionary <int, MalListEntry> animeList = new Dictionary <int, MalListEntry>();
                foreach (MyAnimeListEntry listEntry in userLookup.AnimeList)
                {
                    animeList[listEntry.AnimeInfo.AnimeId] = new AnimeRecs.RecEngine.MAL.MalListEntry((byte?)listEntry.Score, listEntry.Status, (short)listEntry.NumEpisodesWatched);
                }

                Dictionary <int, MalListEntry> animeWithheld = WithholdAnime(input, animeList);

                MalRecResults <IEnumerable <IRecommendation> > recResults = await GetRecommendationsAsync(input, recConfig.Value, animeList, animeWithheld, recClientFactory).ConfigureAwait(false);

                GetRecsViewModel viewModel = new GetRecsViewModel(
                    results: recResults,
                    userId: userLookup.UserId,
                    userName: userLookup.CanonicalUserName,
                    userLookup: userLookup,
                    userAnimeList: animeList,
                    maximumRecommendationsToReturn: recConfig.Value.MaximumRecommendationsToReturn,
                    maximumRecommendersToReturn: recConfig.Value.MaximumRecommendersToReturn,
                    animeWithheld: animeWithheld,
                    dbConnectionFactory: dbConnFactory
                    );

                RecResultsAsHtmlJson resultsJson = await GetResultHtmlAsync(viewModel, input, viewEngine, tempProvider).ConfigureAwait(false);

                return(Ok(resultsJson));
            }
            catch (ShortCircuitException ex)
            {
                return(ex.Result);
            }
        }
Ejemplo n.º 2
0
        public GetRecsModule(IConfig config, IMyAnimeListApiFactory malApiFactory, IAnimeRecsClientFactory recClientFactory, IAnimeRecsDbConnectionFactory dbConnectionFactory, IViewFactory viewFactory, IViewLocator viewLocator, RazorViewEngine viewEngine, IRenderContextFactory renderContextFactory)
        {
            _config = config;
            _malApiFactory = malApiFactory;
            _recClientFactory = recClientFactory;
            _dbConnectionFactory = dbConnectionFactory;
            _viewLocator = viewLocator;
            _viewEngine = viewEngine;
            _renderContextFactory = renderContextFactory;

            Post["/GetRecs"] = GetRecs;
        }
Ejemplo n.º 3
0
        private async Task <MalUserLookupResults> GetUserLookupAsync(AnimeRecsInputJson input, IMyAnimeListApiFactory malApiFactory)
        {
            using (IMyAnimeListApi malApi = malApiFactory.GetMalApi())
            {
                _logger.LogInformation("Getting MAL list for user {0}.", input.MalName);
                MalUserLookupResults userLookup;
                try
                {
                    userLookup = await malApi.GetAnimeListForUserAsync(input.MalName).ConfigureAwait(false);

                    _logger.LogInformation("Got MAL list for user {0}.", input.MalName);
                    return(userLookup);
                }
                catch (MalUserNotFoundException)
                {
                    _logger.LogInformation("User {0} not found.", input.MalName);
                    AjaxError  error  = new AjaxError(AjaxError.NoSuchMALUser, "No such MAL user.");
                    JsonResult result = Json(error);
                    result.StatusCode = 404;
                    throw new ShortCircuitException(result);
                }
            }
        }