public async Task <ActionResult <CostItemViewModel> > Add([FromBody] CostItemCreateViewModel model)
        {
            var entity = new CostItem
            {
                Amount     = model.Amount,
                CostTypeId = model.CostTypeId,
                DateUsed   = model.DateUsed,
                ItemName   = model.ItemName
            };

            var costTypes = await _costTypesRepository.GetAll();

            if (costTypes.Any(x => x.CostTypeId == model.CostTypeId) == false)
            {
                return(NotFound("CostTypeId does not exist"));
            }

            var record = await _costItemsRepository.Add(entity);

            return(CreatedAtRoute("GetById",
                                  new { Id = record.CostItemId },
                                  new CostItemViewModel
            {
                CostItemId = record.CostItemId,
                ItemName = record.ItemName,
                CostType = AdaptToCostTypeViewModel(costTypes, record.CostTypeId),
                Amount = record.Amount,
                DateUsed = record.DateUsed
            }));
        }
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);
            });
        }