public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Controller controller = (Controller)context.Controller; GameControllerServices controllerServices = (GameControllerServices)context.HttpContext.RequestServices.GetService(typeof(GameControllerServices)); if (!context.ModelState.IsValid) { GameViewModel viewModel = await controllerServices.GetGameViewModelForEditAsync(); viewModel.Game = (Models.Game)context.ActionArguments["Game"]; List <Image> images = await controllerServices.GetImagesAsync(viewModel.Game.Id); if (images is null) { context.Result = new RedirectToActionResult("Error", "Home", new { area = "Seller" }); } else { viewModel.Game.Images = images; context.Result = new ViewResult() { ViewData = new ViewDataDictionary(controller.ViewData) { Model = viewModel } }; } } else { await next(); } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { SellerLoginManager loginManager = (SellerLoginManager)context.HttpContext.RequestServices.GetService(typeof(SellerLoginManager)); Controller controller = (Controller)context.Controller; GameControllerServices controllerServices = (GameControllerServices)context.HttpContext.RequestServices.GetService(typeof(GameControllerServices)); int imgsPerGame = controllerServices.GetImgsPerGame(); Models.Game game = (Models.Game)context.ActionArguments["game"]; if (game.SellerId != loginManager.GetUserId()) { context.Result = new BadRequestResult(); } string[] tempImgPaths = ImageHandler.GetAllTempImageRelativePaths(controllerServices.GetImgsTempFolder()); if (!context.ModelState.IsValid || tempImgPaths.Length < imgsPerGame) { if (tempImgPaths.Length < imgsPerGame) { controller.ViewData["MSG_E"] = $"You need to have {imgsPerGame} for images a game"; } controller.ViewData["SellerId"] = loginManager.GetUserId(); UsedGamesAPIPlatformResponse response = await GetPlatformsAsync(context); if (!response.Success) { context.Result = new RedirectToActionResult("Error", "Home", new { area = "Seller" }); } SelectList platforms = new SelectList(response.Platforms, "Id", "Name"); GameViewModel viewModel = new GameViewModel(game, platforms, imgsPerGame, loginManager.GetUserId(), tempImgPaths); context.Result = controller.View(viewModel); } else { await next(); } }
public GameController(GameControllerServices services) { _controllerServices = services; }