Beispiel #1
0
        public async Task CreateProductForSupplier()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(CreateProductForSupplier));
            var res = await context.AddAsync(ValidModelCreator.Supplier());

            var supplierId = res.Entity.Id;

            var createProductForSupplierCommand = new CreateProductForSupplierCommand
            {
                Label      = "Product 123",
                Price      = 12.5m,
                SupplierId = supplierId
            };
            var createProductForSupplierHandler = new CreateProductForSupplierCommandHandler(context);
            var response1 =
                await createProductForSupplierHandler.Handle(createProductForSupplierCommand, CancellationToken.None);

            Assert.True(response1.Success);
            var productId = response1.Data;

            var queryOne = new GetProductForSupplierByIdQuery
            {
                SupplierId = supplierId, ProductId = productId
            };
            var queryOneHandler  = new GetProductForSupplierByIdQueryHandler(context);
            var queryOneResponse = await queryOneHandler.Handle(queryOne, CancellationToken.None);

            Assert.True(queryOneResponse.Success);
            Assert.Equal(productId, queryOneResponse.Data.Id);
            Assert.Equal(createProductForSupplierCommand.Label, queryOneResponse.Data.Label);
            Assert.Equal(createProductForSupplierCommand.Price, queryOneResponse.Data.Price);
            Assert.Equal(createProductForSupplierCommand.SupplierId, queryOneResponse.Data.SupplierId);

            var queryAll = new GetAllProductsForSupplierQuery {
                SupplierId = supplierId
            };
            var queryAllHandler  = new GetAllProductsForSupplierQueryHandler(context);
            var queryAllResponse = await queryAllHandler.Handle(queryAll, CancellationToken.None);

            Assert.True(queryAllResponse.Success);
            var relevantProduct = queryAllResponse.Data.SingleOrDefault(p => p.Id == productId);

            Assert.NotNull(relevantProduct);
            Assert.Equal(productId, relevantProduct.Id);
            Assert.Equal(createProductForSupplierCommand.Label, relevantProduct.Label);
            Assert.Equal(createProductForSupplierCommand.Price, relevantProduct.Price);
            Assert.Equal(createProductForSupplierCommand.SupplierId, relevantProduct.SupplierId);
        }
Beispiel #2
0
        public async Task <ActionResult <ProductDto> > AddProduct(int supplierId,
                                                                  [FromBody] CreateProductViewModel createProductViewModel)
        {
            var command = new CreateProductForSupplierCommand
            {
                SupplierId = supplierId,
                Label      = createProductViewModel.Label,
                Price      = createProductViewModel.Price,
            };
            var result = await mediator.Send(command);

            if (!result.Success)
            {
                return(BadRequest(result.ErrorMessages));
            }

            var resultData = await mediator.Send(new GetProductForSupplierByIdQuery
                                                 { ProductId = result.Data, SupplierId = supplierId });

            return(CreatedAtAction(nameof(GetProduct),
                                   new { supplierId = supplierId, productId = result.Data },
                                   ProductDto.FromCoreProductDto(resultData.Data)));
        }