Пример #1
0
        public async Task <ActionResult> Query([FromQuery] BlueprintQueryParameters queryParameters)
        {
            if (queryParameters == null)
            {
                return(this.Problem(ProblemFactory.BadParameters()));
            }

            var blueprintQuery = new BlueprintsQuery
            {
                MaxProductionEfford = queryParameters.MaxProductionEfford,
                NameContains        = queryParameters.ModelName
            };

            var result = await CommandHandler.Query(blueprintQuery);

            return(result.Match(
                       success =>
            {
                var resultHto = Ok(new BlueprintQueryResultHto(success.Result, queryParameters));
                return resultHto;
            },
                       notReachable => this.Problem(ProblemFactory.ServiceUnavailable()),
                       invalidQuery => this.Problem(ProblemFactory.BadParameters()),
                       error => this.Problem(ProblemFactory.Exception(error.Exception))
                       ));
        }
        public Task <QueryResult <RobotBlueprint> > Query(BlueprintsQuery query)
        {
            if (query.MaxProductionEfford.HasValue && query.MaxProductionEfford < 0)
            {
                return(Task.FromResult <QueryResult <RobotBlueprint> >(new QueryResult <RobotBlueprint> .InvalidQuery($"{nameof(query.MaxProductionEfford)} value < 0. Must be 0<=")));
            }

            if (query.NameContains != null && string.Equals(query.NameContains, string.Empty))
            {
                return(Task.FromResult <QueryResult <RobotBlueprint> >(new QueryResult <RobotBlueprint> .InvalidQuery($"{nameof(query.NameContains)} is empty")));
            }

            IList <RobotBlueprintModel> getAllResult;

            try
            {
                getAllResult = internalRepository.GetAll().ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(Task.FromResult <QueryResult <RobotBlueprint> >(new QueryResult <RobotBlueprint> .Error(e)));
            }


            var totalCount = getAllResult.Count;
            IEnumerable <RobotBlueprintModel> resultModels = getAllResult;

            if (query.MaxProductionEfford.HasValue)
            {
                resultModels = resultModels.Where(b => b.ProductionEfford <= query.MaxProductionEfford);
            }
            if (query.NameContains != null)
            {
                resultModels = resultModels.Where(b => b.HumanReadableName.Contains(query.NameContains));
            }

            var result = resultModels.Select(b => new RobotBlueprint(
                                                 new RobotBlueprintId(b.Id),
                                                 b.Version,
                                                 b.HumanReadableName,
                                                 b.Description,
                                                 b.ProductionEfford)).ToList();

            return(Task.FromResult <QueryResult <RobotBlueprint> >(new QueryResult <RobotBlueprint> .Success(result, totalCount)));
        }