private void SetUpCostItemQueries(ICostItemsRepository costItemsRepository, ICostTypesRepository costTypesRepository) { FieldAsync <CostItemType>( "costItem", arguments: new QueryArguments( new QueryArgument <GuidGraphType> { Name = "id" } ), resolve: async(context) => { var costItemId = context.GetArgument <Guid?>("id"); var costItem = await costItemsRepository.GetById(costItemId.Value); var costType = await costTypesRepository.GetById(costItem.CostTypeId); return(new CostItemViewModel { CostItemId = costItem.CostItemId, ItemName = costItem.ItemName, CostType = ConvertEntityToViewModel(costType), Amount = costItem.Amount, DateUsed = costItem.DateUsed }); }); FieldAsync <ListGraphType <CostItemType> >( "costItems", arguments: new QueryArguments(new List <QueryArgument> { new QueryArgument <IntGraphType> { Name = "year" }, new QueryArgument <IntGraphType> { Name = "month" } }), resolve: async(context) => { var year = context.GetArgument <int?>("year"); var month = context.GetArgument <int?>("month"); if (year.HasValue && month.HasValue) { var results = await costItemsRepository.GetRecordsByFilter(year.Value, month.Value); var costTypes = (await costTypesRepository.GetAll()).ToList(); return(results.Select(x => ConvertEntityToViewModel(x, costTypes)).OrderByDescending(x => x.DateUsed)); } //throw new ExecutionError("year and month must be provided"); context.Errors.Add(new ExecutionError($"{string.Join('/', context.Path)}: year and month must be provided")); return(new List <CostItemType>()); }); }
public async Task <ActionResult <IEnumerable <CostItemViewModel> > > Filter([FromQuery] int year, [FromQuery] int month) { var costItems = await _costItemsRepository.GetRecordsByFilter(year, month); var costTypes = await _costTypesRepository.GetAll(); var viewModels = costItems.Select(item => new CostItemViewModel { CostItemId = item.CostItemId, ItemName = item.ItemName, CostType = AdaptToCostTypeViewModel(costTypes, item.CostTypeId), Amount = item.Amount, DateUsed = item.DateUsed }) .OrderBy(x => x.DateUsed) .ToList(); return(Ok(viewModels)); }