Ejemplo n.º 1
0
        public async Task <IActionResult> GetProducts()
        {
            var products = await productService.GetProductsAsync();

            var role = User.Claims.FirstOrDefault(c => c.Type == "role")?.Value;

            var test = string.Join(", ", User.Claims.Select(c => c.Type));

            logger.LogInformation(test);
            if (role != "admin")
            {
                products = products.Take(1).ToList();
            }

            return(Ok(productModelMapper.Map(products)));
        }
Ejemplo n.º 2
0
        public void MapProducts_Always_ReturnExpectedResult()
        {
            var products = new Collection <Product>
            {
                new Product {
                    Id   = "1",
                    Name = "Product 1",
                    ProductCategoryProducts = new Collection <ProductCategoryProduct>  {
                        new ProductCategoryProduct
                        {
                            ProductId         = "1",
                            ProductCategoryId = "1",
                            ProductCategory   = new ProductCategory {
                                Name = "Cat 1"
                            }
                        }
                    }
                },
                new Product {
                    Id   = "2",
                    Name = "Product 2",
                    ProductCategoryProducts = new Collection <ProductCategoryProduct>  {
                        new ProductCategoryProduct
                        {
                            ProductId         = "2",
                            ProductCategoryId = "2",
                            ProductCategory   = new ProductCategory {
                                Name = "Cat 2"
                            }
                        }
                    }
                },
                new Product {
                    Id   = "3",
                    Name = "Product 3",
                    ProductCategoryProducts = new Collection <ProductCategoryProduct>  {
                        new ProductCategoryProduct
                        {
                            ProductId         = "3",
                            ProductCategoryId = "1",
                            ProductCategory   = new ProductCategory {
                                Name = "Cat 1"
                            }
                        }
                    }
                }
            };

            var result = sut.Map(products);

            result.Should().BeEquivalentTo(new Collection <ProductModel>
            {
                new ProductModel {
                    Id   = "1",
                    Name = "Product 1",
                    ProductCategories = new Collection <ProductCategoryModel>  {
                        new ProductCategoryModel {
                            Id = "1", Name = "Cat 1"
                        }
                    }
                }
                ,
                new ProductModel {
                    Id   = "2",
                    Name = "Product 2",
                    ProductCategories = new Collection <ProductCategoryModel>  {
                        new ProductCategoryModel {
                            Id = "2", Name = "Cat 2"
                        }
                    }
                },
                new ProductModel {
                    Id   = "3",
                    Name = "Product 3",
                    ProductCategories = new Collection <ProductCategoryModel>  {
                        new ProductCategoryModel {
                            Id = "1", Name = "Cat 1"
                        }
                    }
                }
            });
        }