Exemple #1
0
        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>());
            });
        }
Exemple #2
0
        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 async Task <ActionResult <CostItemViewModel> > GetById(Guid id)
        {
            var record = await _costItemsRepository.GetById(id);

            if (record == null)
            {
                return(NotFound($"The given id: {id} is not found"));
            }

            var costTypes = await _costTypesRepository.GetAll();

            return(Ok(new CostItemViewModel
            {
                CostItemId = record.CostItemId,
                ItemName = record.ItemName,
                CostType = AdaptToCostTypeViewModel(costTypes, record.CostTypeId),
                Amount = record.Amount,
                DateUsed = record.DateUsed
            }));
        }