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>()); }); }
private void SetUpCostItemMutations(ICostItemsRepository costItemsRepository, ICostTypesRepository costTypesRepository) { FieldAsync <GuidGraphType>( "addCostItem", "Add Cost Item to the database.", new QueryArguments( new QueryArgument <NonNullGraphType <CostItemInputType> > { Name = "input", Description = "Cost item detail" }), async(context) => { var costItem = context.GetArgument <CostItem>("input"); var costType = await costTypesRepository.GetById(costItem.CostTypeId); if (costType == null) { context.Errors.Add(new ExecutionError($"cost Type {costItem.CostTypeId}: not found")); return(null); } if (await costItemsRepository.GetById(costItem.CostItemId) != null) { context.Errors.Add(new ExecutionError($"cost Item Id {costItem.CostItemId}: already exists. Please provide a new Unique Id")); return(null); } return((await costItemsRepository.Add(costItem)).CostItemId); }); }
public CostItemsController( ICostItemsRepository costItemsRepository, ICostTypesRepository costTypesRepository) { _costItemsRepository = costItemsRepository; _costTypesRepository = costTypesRepository; }
public RootMutation( ICostTypesRepository costTypesRepository, ICostItemsRepository costItemsRepository) { Name = "Mutation"; SetUpCostItemMutations(costItemsRepository, costTypesRepository); }
public RootQuery( ICostTypesRepository costTypesRepository, ICostItemsRepository costItemsRepository) { Name = "Query"; SetUpCostTypeQueries(costTypesRepository); SetUpCostItemQueries(costItemsRepository, costTypesRepository); SetUpSecuredQueries(); }