示例#1
0
        public async Task <IActionResult> CreateOrderItemAsync(
            CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            CreateOrderItemModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await orderItemService.GetOrder(callOffId);

            if (order is null)
            {
                return(NotFound());
            }

            var validationResult = await createOrderItemService.CreateAsync(order, catalogueItemId, model);

            if (validationResult.Success)
            {
                return(CreatedAtAction(
                           nameof(GetAsync).TrimAsync(),
                           new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() },
                           null));
            }

            foreach ((string key, string errorMessage) in validationResult.ToModelErrors())
            {
                ModelState.AddModelError(key, errorMessage);
            }

            return(ValidationProblem(ModelState));
        }
        public async Task <ActionResult <CreateOrderItemResponseModel> > CreateOrderItemAsync(
            string orderId,
            CreateOrderItemModel model)
        {
            var order = await _orderRepository.GetOrderByIdAsync(orderId);

            if (order is null)
            {
                return(NotFound());
            }

            var primaryOrganisationId = User.GetPrimaryOrganisationId();

            if (primaryOrganisationId != order.OrganisationId)
            {
                return(Forbid());
            }

            var createOrderItemResponse = new CreateOrderItemResponseModel();

            var result = await _createOrderItemService.CreateAsync(model.ToRequest(order, CatalogueItemType.Solution));

            if (result.IsSuccess)
            {
                createOrderItemResponse.OrderItemId = result.Value;
                return(CreatedAtAction(nameof(GetOrderItemAsync).TrimAsync(), null, new { orderId, orderItemId = createOrderItemResponse.OrderItemId }, createOrderItemResponse));
            }

            createOrderItemResponse.Errors = result.Errors.Select(x => new ErrorModel(x.Id, x.Field));
            return(BadRequest(createOrderItemResponse));
        }