Ejemplo n.º 1
0
        public async Task <ActionResult <ProductDto> > UpdateProduct(int supplierId, int productId,
                                                                     [FromBody] UpdateProductViewModel updateProductViewModel)
        {
            var command = new UpdateProductForSupplierCommand
            {
                SupplierId = supplierId,
                ProductId  = productId,
                Label      = updateProductViewModel.Label,
                Price      = updateProductViewModel.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(Ok(ProductDto.FromCoreProductDto(resultData.Data)));
        }
Ejemplo n.º 2
0
        public async Task UpdateProductForSupplier()
        {
            await using var context = DatabaseHelper.CreateInMemoryDatabaseContext(nameof(UpdateProductForSupplier));
            var supplierRes = await context.AddAsync(ValidModelCreator.Supplier());

            var supplierId = supplierRes.Entity.Id;
            var productRes = await context.AddAsync(ValidModelCreator.Product(supplierId));

            var productId = productRes.Entity.Id;

            //test update
            var updateProductCmd1 = new UpdateProductForSupplierCommand
            {
                SupplierId = supplierId,
                ProductId  = productId,
                Label      = "Something new",
                Price      = 55.5m,
            };
            var updateProductForSupplierCommandHandler = new UpdateProductForSupplierCommandHandler(context);
            var response1 =
                await updateProductForSupplierCommandHandler.Handle(updateProductCmd1, CancellationToken.None);

            Assert.True(response1.Success);
            var getUpdatedProductQuery1 = new GetProductForSupplierByIdQuery
            {
                SupplierId = supplierId,
                ProductId  = productId,
            };
            var queryOneHandler = new GetProductForSupplierByIdQueryHandler(context);
            var queryResp1      =
                await queryOneHandler.Handle(getUpdatedProductQuery1,
                                             CancellationToken.None);

            Assert.True(queryResp1.Success);
            Assert.Equal(supplierId, queryResp1.Data.SupplierId);
            Assert.Equal(productId, queryResp1.Data.Id);
            Assert.Equal("Something new", queryResp1.Data.Label);
            Assert.Equal(55.5m, queryResp1.Data.Price);

            // test another update
            var updateProductCmd2 = new UpdateProductForSupplierCommand
            {
                SupplierId = supplierId,
                ProductId  = productId,
                Label      = "another thing",
                Price      = 66.45798m,
            };
            var response2 =
                await updateProductForSupplierCommandHandler.Handle(updateProductCmd2, CancellationToken.None);

            Assert.True(response2.Success);

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

            Assert.Equal(supplierId, queryResp2.Data.SupplierId);
            Assert.Equal(productId, queryResp2.Data.Id);
            Assert.Equal("another thing", queryResp2.Data.Label);
            Assert.Equal(66.45798m, queryResp2.Data.Price);

            // test no update if invalid data provided
            var reqInvalidSupplier = new UpdateProductForSupplierCommand
            {
                SupplierId = supplierId + 1,
                ProductId  = productId,
                Price      = 50.0m,
                Label      = "should not be changed",
            };
            var response3 =
                await updateProductForSupplierCommandHandler.Handle(reqInvalidSupplier, CancellationToken.None);

            Assert.False(response3.Success);
            var queryResponse3 = await queryOneHandler.Handle(
                new GetProductForSupplierByIdQuery { SupplierId = supplierId, ProductId = productId, },
                CancellationToken.None);

            Assert.True(queryResponse3.Success);
            Assert.Equal(supplierId, queryResponse3.Data.SupplierId);
            Assert.Equal(productId, queryResponse3.Data.Id);
            Assert.Equal("another thing", queryResponse3.Data.Label);
            Assert.Equal(66.45798m, queryResponse3.Data.Price);
        }