Exemplo n.º 1
0
        public void GetAllTest()
        {
            IProductRepository productRepository = new MongoDbProductRepository(MongoDBConfigFile.GetDBCollection());

            List <Product> products = productRepository.GetAll();

            Assert.NotEmpty(products);
        }
Exemplo n.º 2
0
        public void CalltoGetAllWithAnExistingProduct()
        {
            IProductRepository productRepository = new MongoDbProductRepository(MongoDBConfigFile.GetDBCollection());
            List <Product>     products          = productRepository.GetAll();

            string temp = products[0].Name;

            Assert.Equal("Apple", temp);
        }
Exemplo n.º 3
0
        public void CalltoGetAllWithNumberOfProducts()
        {
            IProductRepository productRepository = new MongoDbProductRepository(MongoDBConfigFile.GetDBCollection());

            List <Product> products = productRepository.GetAll();
            var            num      = products.Count;


            Assert.Equal(10, num); //The number of products in database is 10
        }
Exemplo n.º 4
0
        public static void ListManufactures()
        {
            IProductRepository repository = new MongoDbProductRepository(MongoDBConfigFile.GetDBCollection());

            IEnumerable <Product>     list  = repository.GetAll();
            IEnumerable <Manufacture> list1 = list.Select(p => p.Manufacture).ToList();
            IEnumerable <string>      list2 = list1.Select(m => m.Name).ToList();

            list2 = list2.Distinct();

            Manufacture manufacture = new Manufacture();

            foreach (string s in list2)
            {
                manufacture.Products = list.Where(p => p.Manufacture.Name == s).ToList();
                manufacture.Name     = s;
                Console.WriteLine("Manufacture: " + manufacture.Name + "  Number of products: " + manufacture.Products.Count());
            }
        }
Exemplo n.º 5
0
        //public static void Main(string[] args)
        public static void Run()
        {
            //Read configuration files
            //using Microsoft.Extensions.Configuration; which must be added as nuget package.
            //Change connection string for database in file appsettings.json or appsettings.dev.json for a special developer
            //database.
            IConfiguration configuration = new ConfigurationBuilder()
                                           .AddJsonFile("appsettings.json", false, true)
                                           .AddJsonFile("appsettings.dev.json", true, true)
                                           .Build();

            var mongoConnectionString = configuration["ConnectionStrings:DefaultConnection"];

            //Using Entity Framework as ORM for accessing Relational database.
            // var context = new WareHouseContext();
            //
            // context.Products.Add(new Entities.Product());
            // context.SaveChanges();
            //
            // PrintList("",context.Products);
            //


            //Composition Root. Create all objects we need.
            var _client     = new MongoClient(mongoConnectionString);
            var _database   = _client.GetDatabase("Warehouse");
            var _collection = _database.GetCollection <Product>("Products");

            IProductRepository  productRepository   = new MongoDbProductRepository(_collection); //new FileProductRepository();
            IShopRepository     shopRepository      = new FileShopRepository();
            ManufacturerService manufacturerService = new ManufacturerService();

            ProductFilter productFilter = new ProductFilter(productRepository, shopRepository);

            var shop = new Shop()
            {
                Id = "1", Name = "Ica Maxi"
            };
            var shop1 = new Shop()
            {
                Id = "2", Name = "Coop"
            };

            //Create new product
            var product = ConsoleHelper.NewProduct();

            //Add shops to product
            shopRepository.Insert(shop);
            shopRepository.Insert(shop1);

            product.AddShop(shop);
            product.AddShop(shop1);

            //Insert new product into repository
            productRepository.Insert(product);

            PrintList("Products for each Manufacturer", productFilter.ListOfManufacturersWithProductCount());
            PrintList("Products costing less than 20", productFilter.ProductsCostingLessThan(20));

            //Do some changes
            PrintList("All products", productRepository.GetAll().Select(p => p.Price));

            product.Price = (decimal)123.45;
            ((MongoDbProductRepository)productRepository).Update(product);

            PrintList("All products", productRepository.GetAll().Select(p => p.Price));
        }