Esempio n. 1
0
        public async Task <ActionResult <IAsyncEnumerable <ProductDto> > > GetAllProducts(int supplierId)
        {
            var query = new GetAllProductsForSupplierQuery
            {
                SupplierId = supplierId
            };
            var result = await mediator.Send(query);

            return(result.Success
                ? (ActionResult <IAsyncEnumerable <ProductDto> >)Ok(result.Data.Select(ProductDto.FromCoreProductDto))
                : BadRequest(result.ErrorMessages));
        }
Esempio n. 2
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);
        }