Example #1
0
        public async Task Consume(ConsumeContext <IDeleteProductCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                var blobStorageSettings      = new BlobStorageSettings();

                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings);

                var dbContext  = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository = new RepositoryWrapper(dbContext);

                var product = repository.Product.GetProductById(Guid.Parse(context.Message.ProductId));
                if (product != null)
                {
                    var deleteblob = new PhotoBlob(blobStorageSettings);
                    await deleteblob.DeleteBlob(product.BlobName);
                }

                var productService = new ProductCatalogService(repository);
                var result         = await productService.DeleteProduct(context.Message.ProductId);

                var deletedEvent = new ProductDeletedEvent(result);
                await context.RespondAsync(deletedEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Example #2
0
        public void ProductCatalog_ShouldThrow_DuplicateProductException_WhenAdding_DuplicateProduct()
        {
            ProductCatalogService ProductCatalogService = new ProductCatalogService();

            ProductCatalogService.AddProduct('A', 30);
            ProductCatalogService.AddProduct('A', 30);
        }
Example #3
0
        public async Task Consume(ConsumeContext <IGetProductCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                var blobStorageSettings      = new BlobStorageSettings();


                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings);

                var dbContext  = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository = new RepositoryWrapper(dbContext);

                var productService = new ProductCatalogService(repository);
                var result         = await productService.GetProduct(context.Message.ProductId);

                var getProductEvent = new ProductRetrievedEvent(result);

                if (getProductEvent.ProductDto != null)
                {
                    var phtoBlob  = new PhotoBlob(blobStorageSettings);
                    var byteArray = await phtoBlob.DownloadBlob(getProductEvent.ProductDto.BlobName);

                    getProductEvent.ProductDto.Photo = byteArray;
                }

                await context.RespondAsync(getProductEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Example #4
0
        public async Task Consume(ConsumeContext <ICreateProductCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                var blobStorageSettings      = new BlobStorageSettings();
                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings);

                var dbContext      = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository     = new RepositoryWrapper(dbContext);
                var productService = new ProductCatalogService(repository);

                var product = repository.Product.GetByCondition(x => x.Code.Equals(context.Message.Code) || x.Name.Equals(context.Message.Name));
                if (product?.Count() == 0)
                {
                    //upload photo to blob
                    var repo       = BlobConfigurator.GetMessageDataRepository(blobStorageSettings);
                    var bytesArray = Convert.FromBase64String(context.Message.Photo);
                    var payload    = repo.PutBytes(bytesArray).Result;
                    context.Message.BlobName = payload.Address.AbsolutePath;
                }

                //create product
                var result = await productService.CreateProduct(context.Message);

                var createdEvent = new ProductCreatedEvent(result);
                await context.RespondAsync(createdEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task GetAllProducts_ReturnsDataAsync()
        {
            // Arrange
            var productList = new List <Product>()
            {
                new Product()
                {
                    Id = Guid.NewGuid(), Code = "123"
                },
                new Product()
                {
                    Id = Guid.NewGuid(), Code = "234"
                }
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(productList), HttpStatusCode.OK);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.GetAllProducts();

            // Assert
            Assert.Equal(productList.Count(), result.Count());
        }
Example #6
0
        public void ProductCatalog_ShouldReturn_ProductPrice_ByPassingProductName()
        {
            ProductCatalogService ProductCatalogService = new ProductCatalogService();

            ProductCatalogService.AddProduct('A', 30);
            var price = ProductCatalogService.GetPrice('A');

            Assert.AreEqual(price, 30);
        }
Example #7
0
        public void ProductCatalog_ShouldReturnZero_When_ProductNotFound()
        {
            ProductCatalogService ProductCatalogService = new ProductCatalogService();

            ProductCatalogService.AddProduct('A', 30);
            var price = ProductCatalogService.GetPrice('B');

            Assert.AreEqual(price, 0);
        }
Example #8
0
    public void acceptsSkuAsParameterOnGetRequest()
    {
        var mockCatalog    = new MockProductCatalog(); // Hand rolled mock here.
        var catalogService = new ProductCatalogService(mockCatalog);

        catalogService.find("some-sku-from-url")

        mockCatalog.assertFindWasCalledWith("some-sku-from-url");
    }
Example #9
0
    public void returnsJsonFromGetRequest()
    {
        var mockCatalog = new MockProductCatalog();    // Hand rolled mock here.

        mockCatalog.findShouldReturn(new Product("some-sku-from-url"));
        var mockResponse   = new MockHttpResponse();  // Hand rolled mock here.
        var catalogService = new ProductCatalogService(mockCatalog, mockResponse);

        catalogService.find("some-sku-from-url")

        mockCatalog.assertWriteWasCalledWith("{ 'sku': 'some-sku-from-url' }");
    }
        public async Task GetProductById_ReturnsNotFoundAsync()
        {
            // Arrange
            var httpClient = new TestHelper().CreateMockHttpClient("", HttpStatusCode.NotFound);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.GetProduct(Guid.NewGuid());

            // Assert
            Assert.Null(result);
        }
Example #11
0
        public DefaultViewModel(ProductCatalogService productCatalogService, ProductOrderService productOrderService)
        {
            this.productCatalogService = productCatalogService;
            ReorderDialog = new ReoderDialogViewModel(productOrderService);

            // refresh grid and display the alert when items are reordered
            ReorderDialog.ItemsReordered += () =>
            {
                AlertType = AlertType.Success;
                AlertText = "The product was reordered successfully.";
                Products.RequestRefresh();
            };
        }
        public override void OnViewLoad()
        {
            if (View.IsPostBack)
            {
                return;
            }

            var responseProducts = ProductCatalogService.GetFeaturedProducts();

            View.Categories = GetCategories();
            View.Products   = responseProducts.Products;
            View.DataBind();
        }
        private void SearchProductResultViewFrom()
        {
            var productSearchRequest = GenerateInitialProductSearchRequest();
            var response             = ProductCatalogService.GetProductsByCategory(productSearchRequest);

            View.Categories           = GetCategories();
            View.CurrentPage          = response.CurrentPage;
            View.NumberOfTitlesFound  = response.NumberOfTitlesFound;
            View.Products             = response.Products;
            View.RefinementGroups     = response.RefinementGroups;
            View.SelectedCategory     = response.SelectedCategory;
            View.SelectedCategoryName = response.SelectedCategoryName;
            View.TotalNumberOfPages   = response.TotalNumberOfPages;
        }
        public override void OnViewLoad()
        {
            base.OnViewLoad();
            if (View.IsPostBack)
            {
                return;
            }

            var request = new GetProductRequest {
                ProductId = View.ProductId
            };
            var response = ProductCatalogService.GetProduct(request);

            View.Product    = response.Product;
            View.Categories = GetCategories();
        }
        public DefaultViewModel(ProductCatalogService productCatalogService)
        {
            this.productCatalogService = productCatalogService;

            Products = new GridViewDataSet <ProductListDTO>()
            {
                PagingOptions =
                {
                    PageSize = 10
                },
                SortingOptions =
                {
                    SortExpression = nameof(ProductListDTO.Name),
                    SortDescending = false
                }
            };
        }
        public async Task Index_should_return_valid_model()
        {
            using (var context = GetContextWithData())
            {
                var repository = new RepositoryWrapper(context);
                //var mockRepository = new Mock<IRepositoryWrapper>();
                //mockRepository.Setup(x => x.Product)
                //var services = new ProductCatalogService(mockRepository.Object);

                var services = new ProductCatalogService(repository);
                //var mockProductObject = new Mock<ICreateProductCommand>();
                //mockProductObject.(x => x = context.Products[0])

                //await services.CreateProduct()


                //var dto = await services.CreateProduct(mockProductObject.Object);
            }
        }
        public async Task UpdateProduct_UpdatedAsync()
        {
            // Arrange
            var code    = "123";
            var product = new Product()
            {
                Code = code
            };

            var httpClient = new TestHelper().CreateMockHttpClient("", HttpStatusCode.NoContent);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.UpdateProduct(Guid.NewGuid(), product);

            // Assert
            Assert.Null(result);
        }
        public void DeleteProduct_Deleted()
        {
            // Arrange
            var code    = "123";
            var product = new Product()
            {
                Code = code
            };

            var httpClient = new TestHelper().CreateMockHttpClient("", HttpStatusCode.OK);

            var service = new ProductCatalogService(httpClient);

            // Act
            async Task Action() => await service.DeleteProduct(Guid.NewGuid());

            // Assert
            Assert.Null(Action().Exception);
        }
        public void DeleteProduct_ReturnsNotFound()
        {
            // Arrange
            var code    = "123";
            var product = new Product()
            {
                Code = code
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(product), HttpStatusCode.Created);

            var service = new ProductCatalogService(httpClient);

            // Act
            async Task Action() => await service.DeleteProduct(Guid.NewGuid());

            // Assert
            Assert.Null(Action().Exception);
        }
        public async Task CreateProduct_CreatedAsync()
        {
            // Arrange
            var code    = "123";
            var product = new Product()
            {
                Code = code
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(product), HttpStatusCode.Created);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.CreateProduct(product);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(code, result.Code);
        }
        public async Task GetProductById_ReturnsDataAsync()
        {
            // Arrange
            var id      = Guid.NewGuid();
            var product = new Product()
            {
                Id = id, Code = "123"
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(product), HttpStatusCode.OK);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.GetProduct(id);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(id, result.Id);
        }
        public async Task Consume(ConsumeContext <IGetAllProductsCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                var dbContext  = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository = new RepositoryWrapper(dbContext);

                var productService = new ProductCatalogService(repository);
                var result         = await productService.GetAllProducts(context.Message);

                var getProductEvent = new AllProductRetrievedEvent(result);
                await context.RespondAsync(getProductEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task Consume(ConsumeContext <IUpdateProductCommand> context)
        {
            try
            {
                var connectionStringSettings = new ConnectionStringSettings();
                var blobStorageSettings      = new BlobStorageSettings();
                _configurationRoot.GetSection("ConnectionStrings").Bind(connectionStringSettings);
                _configurationRoot.GetSection("BlobStorage").Bind(blobStorageSettings);

                var dbContext  = ProductDbContext.GetProductDbContext(connectionStringSettings.DefaultConnection);
                var repository = new RepositoryWrapper(dbContext);

                var product = repository.Product.GetProductById(context.Message.Id);
                if (product != null)
                {
                    //delete existing blob
                    var deleteblob = new PhotoBlob(blobStorageSettings);
                    await deleteblob.DeleteBlob(product.BlobName);

                    //upload photo to blob
                    var repo       = BlobConfigurator.GetMessageDataRepository(blobStorageSettings);
                    var bytesArray = Convert.FromBase64String(context.Message.Photo);
                    var payload    = repo.PutBytes(bytesArray).Result;
                    context.Message.BlobName = payload.Address.AbsolutePath;
                }

                var productService = new ProductCatalogService(repository);
                var result         = await productService.UpdateProduct(context.Message);

                var updatedEvent = new ProductUpdatedEvent(result);
                await context.RespondAsync(updatedEvent);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task GetProductsByFilter_ReturnsDataAsync()
        {
            // Arrange
            var code        = "123";
            var productList = new List <Product>()
            {
                new Product()
                {
                    Id = Guid.NewGuid(), Code = code
                }
            };

            var httpClient = new TestHelper().CreateMockHttpClient(JsonConvert.SerializeObject(productList), HttpStatusCode.OK);

            var service = new ProductCatalogService(httpClient);

            // Act
            var result = await service.GetProductsByFilter($"Code={code}");

            // Assert
            Assert.Equal(productList.Count(), result.Count());
            Assert.Equal(code, result.First().Code);
        }
Example #25
0
        public ResponseView <IList <NewProductView> > GetNewProducts(int size)
        {
            var data = ProductCatalogService.GetNewProducts(size);

            return(data);
        }
Example #26
0
        public ResponseView <IList <RecommendProductView> > GetRecommendProduct(int size)
        {
            var data = ProductCatalogService.GetRecommendProduct(size, "www.baidu.com");

            return(data);
        }
 public ProductDetailViewModel(ProductCatalogService productCatalogService)
 {
     this.productCatalogService = productCatalogService;
 }
 public ProductListViewModel(ProductCatalogService productCatalogService)
 {
     this.productCatalogService = productCatalogService;
 }