public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            _logger.LogInformation("Entered CategoryExists filter");

            if (context.ActionArguments.ContainsKey("categoryId"))
            {
                _logger.LogInformation("Found id argument");
                if (context.ActionArguments["categoryId"] is int categoryId)
                {
                    _logger.LogInformation("Has integer-based id");
                    if (!_repository.Contains <Category>(c => c.CategoryId == categoryId))
                    {
                        _logger.LogInformation("Category associated to id is not found");
                        context.Result = new NotFoundObjectResult(categoryId);
                        return;
                    }
                }
            }
            else if (context.ActionArguments.ContainsKey("id"))
            {
                var categoryId = context.ActionArguments["id"] as string;
                _logger.LogInformation("Found expected id argument in action. Guid is {0}.", categoryId);
                var queryExpression = ApiQueryExpression.GenerateCategoryPredicate <Category>(categoryId, context.HttpContext);
                _logger.LogInformation("Generated Category query expression: {0}", queryExpression.ToString());
                if (!_repository.Contains(queryExpression))
                {
                    _logger.LogInformation("Category associated to id is not found");
                    context.Result = new NotFoundObjectResult(queryExpression.ToString());
                    return;
                }
            }
            await next();
        }
Example #2
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            _logger.LogInformation("Entered MaterialExists filter");

            if (context.ActionArguments.ContainsKey("materialId"))
            {
                _logger.LogInformation("Found material id argument");
                if (context.ActionArguments["materialId"] is int materialId)
                {
                    _logger.LogInformation("Has integer-based id");
                    if (!_repository.Contains <Material>(m => m.MaterialId == materialId))
                    {
                        _logger.LogInformation("Material associated to id is not found");
                        context.Result = new NotFoundObjectResult(materialId);
                        return;
                    }
                }
            }
            else if (context.ActionArguments.ContainsKey("id"))
            {
                if (context.ActionArguments.ContainsKey("id"))
                {
                    var materialId = context.ActionArguments["id"] as string;
                    _logger.LogInformation("Found expected id argument in action. Guid is {0}.", materialId);
                    var queryExpression = ApiQueryExpression.GenerateMaterialPredicate(materialId, context.HttpContext);
                    _logger.LogInformation("Generated Job query expression: {0}", queryExpression.ToString());
                    if (!_repository.Contains(queryExpression))
                    {
                        _logger.LogInformation("Job associated to id is not found");
                        context.Result = new NotFoundObjectResult(queryExpression.ToString());
                        return;
                    }
                }
            }
            await next();
        }