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 static async Task CreateOrderItemAsync_OrderExists_ReturnsCreatedAtActionResult( [Frozen] Mock <ICreateOrderItemService> createOrderItemService, [Frozen] Mock <IOrderItemService> orderItemService, [Frozen] CallOffId callOffId, CatalogueItemId catalogueItemId, Order order, CreateOrderItemModel model, OrderItemsController controller) { Expression <Func <ICreateOrderItemService, Task <AggregateValidationResult> > > createAsync = s => s.CreateAsync( It.Is <Order>(o => o.Equals(order)), It.Is <CatalogueItemId>(i => i == catalogueItemId), It.Is <CreateOrderItemModel>(m => m == model)); orderItemService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order); createOrderItemService.Setup(createAsync).ReturnsAsync(new AggregateValidationResult()); var result = await controller.CreateOrderItemAsync(callOffId, catalogueItemId, model); result.Should().BeOfType <CreatedAtActionResult>(); result.As <CreatedAtActionResult>().Should().BeEquivalentTo(new { ActionName = "Get", RouteValues = new RouteValueDictionary { { nameof(callOffId), callOffId.ToString() }, { nameof(catalogueItemId), catalogueItemId.ToString() }, }, }); }
public static async Task AddOrUpdateAsync_Add_ReturnsExpectedStatusCode( [Frozen] Mock <IDefaultDeliveryDateService> service, [Frozen] CallOffId callOffId, [Frozen] CatalogueItemId catalogueItemId, Order order, DefaultDeliveryDateModel defaultDeliveryDate, [Frozen] Mock <IDefaultDeliveryDateValidator> validator, DefaultDeliveryDateController controller) { service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order); service.Setup(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)).Callback(() => { order.SetDefaultDeliveryDate(catalogueItemId, defaultDeliveryDate.DeliveryDate !.Value); }); validator.Setup(v => v.Validate(defaultDeliveryDate, order.CommencementDate)).Returns((true, null)); var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, defaultDeliveryDate); service.Verify(o => o.GetOrder(callOffId, catalogueItemId)); service.Verify(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)); response.Should().BeOfType <CreatedAtActionResult>(); response.As <CreatedAtActionResult>().Should().BeEquivalentTo(new { ActionName = "Get", RouteValues = new RouteValueDictionary { { nameof(callOffId), callOffId.ToString() }, { nameof(catalogueItemId), catalogueItemId.ToString() }, }, }); }
public static void ToString_ReturnsExpectedResult() { var id = new CatalogueItemId(1, "1"); var idAsString = id.ToString(); idAsString.Should().Be("1-1"); }
public async Task <IActionResult> AddOrUpdateAsync( CallOffId callOffId, CatalogueItemId catalogueItemId, DefaultDeliveryDateModel model) { if (model is null) { throw new ArgumentNullException(nameof(model)); } Order order = await defaultDeliveryDateService.GetOrder(callOffId, catalogueItemId); if (order is null) { return(NotFound()); } (bool isValid, ErrorsModel errors) = validator.Validate(model, order.CommencementDate); if (!isValid) { return(BadRequest(errors)); } // ReSharper disable once PossibleInvalidOperationException (covered by model validation) DeliveryDateResult addedOrUpdated = await defaultDeliveryDateService.SetDefaultDeliveryDate(callOffId, catalogueItemId, model.DeliveryDate.Value); return(addedOrUpdated == DeliveryDateResult.Added ? CreatedAtAction(nameof(GetAsync).TrimAsync(), new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() }, null) : Ok()); }