Example #1
0
        public void Run()
        {
            var repo = new RemoteRepository(_url);

            Console.WriteLine("\nGET ALL ENTITIES:");
            foreach (var i in repo.Entities)
            {
                Console.WriteLine($"  {i.Id}: {i}");
            }


            Console.WriteLine("\nGET ALL ENTITIES OF TYPE PRODUCT:");
            foreach (var i in repo.Entities.OfType <Product>())
            {
                Console.WriteLine($"  {i.Id}: {i.Name}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var entityIdsQuery =
                from e in repo.Entities
                orderby e.Id ascending
                select new { EntityId = e.Id };

            foreach (var item in entityIdsQuery)
            {
                Console.WriteLine($"  {item.EntityId}");
            }


            Console.WriteLine("\nCOUNT:");
            var entitiesQuery =
                from p in repo.Entities
                select p;

            Console.WriteLine($"  Count = {entitiesQuery.Count()}");
        }
Example #2
0
        public void Run()
        {
            var repo = new RemoteRepository(_url);


            Console.WriteLine("\nGET ALL PRODUCTS:");
            foreach (var i in repo.Products)
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = productIdsQuery.ToList();

            foreach (var id in productIdsQuery)
            {
                Console.WriteLine($"  {id}");
            }


            Console.WriteLine("\nCOUNT:");
            var productsQuery =
                from p in repo.Products
                select p;

            Console.WriteLine($"  Count = {productsQuery.Count()}");


            Console.WriteLine("\nINVALID OPERATIONS:");
            try
            {
                var first = productsQuery.First(x => false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }

            try
            {
                var first = productsQuery.Single();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }


            Console.WriteLine("\nGET MARKETS WITH PRODUCTS:");
            var marketsQuery = repo.Products.SelectMany(x => x.Markets).Include(x => x.Products);
            var exp          = marketsQuery.Expression;

            foreach (var market in marketsQuery)
            {
                Console.WriteLine($"  {market.Name}");

                if (market.Products != null)
                {
                    foreach (var product in market.Products)
                    {
                        Console.WriteLine($"    {product.Name}");
                    }
                }
            }


            Console.WriteLine("\nGET ALL PRODUCTS AND THEIR MARKETS:");
            foreach (var i in repo.Products.Include(x => x.Markets))
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");

                foreach (var m in i.Markets)
                {
                    Console.WriteLine($"         {m.Name}");
                }
            }


            Console.WriteLine("\nGET ALL PRODUCTS HAVING MARKETS DEFINED:");
            var query = repo.Products.Where(p => p.Markets.Any());

            foreach (var i in query)
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }


            Console.WriteLine("\nGET ALL PRODUCTS HAVING MARKETS DEFINED (INCLUDING MARKETS):");
            query = query.Include(p => p.Markets);
            foreach (var i in query)
            {
                var markets = i.Markets.Select(x => x.Name);
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C} | {string.Join("; ", markets)}");
            }
        }
Example #3
0
        public void Run()
        {
            var repo = new RemoteRepository(_url);

            Console.WriteLine("\nGET ALL PRODUCTS:");
            foreach (object o in (IQueryable)repo.Products)
            {
                var i = o as Product;
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }


            Console.WriteLine("\nGET PRODUCTS FILTERED BY ID:");
            var idSelection = new List <int> {
                1, 11, 111
            };

            foreach (var i in repo.Products.Where(p => idSelection.Contains(p.Id) || p.Id % 3 == 0))
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }


            Console.WriteLine("\nCROSS JOIN:");
            Func <object, string> sufix = (x) => x + "ending";
            var crossJoinQuery          =
                from c in repo.ProductCategories
                from p in repo.Products
                select new { Category = "#" + c.Name + sufix("-"), p.Name };
            var crossJoinResult = crossJoinQuery.ToList();

            foreach (var i in crossJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nINNER JOIN:");
            var innerJoinQuery =
                from c in repo.ProductCategories
                join p in repo.Products on c.Id equals p.ProductCategoryId
                select new { c.Name, P = new { p.Price }, X = new { Y = string.Concat(c.Name, "-", p.Name) } };
            var innerJoinResult = innerJoinQuery.ToList();

            foreach (var i in innerJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = productIdsQuery.ToList();

            foreach (var id in productIdsQuery)
            {
                Console.WriteLine($"  {id}");
            }


            Console.WriteLine("\nCOUNT:");
            var productsQuery =
                from p in repo.Products
                select p;

            Console.WriteLine($"  Count = {productsQuery.Count()}");


            Console.WriteLine("\nTOTAL AMOUNT BY CATEGORY:");
            var totalAmountByCategoryQuery =
                from c in repo.ProductCategories
                join p in repo.Products
                on c.Id equals p.ProductCategoryId
                join i in repo.OrderItems
                on p.Id equals i.ProductId
                group new { c, p, i } by c.Name into g
                select new
            {
                Category = g.Key,
                Amount   = g.Sum(x => x.i.Quantity * x.p.Price),
                Amount2  = new { Amount = g.Sum(x => x.i.Quantity * x.p.Price) },
            };

            var totalAmountByCategroyResult = totalAmountByCategoryQuery.ToDictionary(x => x.Category);

            foreach (var i in totalAmountByCategroyResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nGET PRODUCT GROUPS:");
            foreach (var g in repo.ProductGroups)
            {
                Console.WriteLine($"  {g.Id} | {g.GroupName}");

                foreach (var p in g.Products)
                {
                    Console.WriteLine($"    | * {p.Name}");
                }
            }


            Console.WriteLine("\nEXPECTED INVALID OPERATION:");
            try
            {
                var first = totalAmountByCategoryQuery.First(x => false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }
        }
Example #4
0
 public Client(string ip, int port)
 {
     _repo = new RemoteRepository(ip, port);
 }
Example #5
0
        public void Run()
        {
            using var repo = new RemoteRepository(_url);

            Console.WriteLine("\nGET ALL PRODUCTS:");
            foreach (var i in repo.Products)
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }


            Console.WriteLine("\nCROSS JOIN:");
            var crossJoinQuery =
                from c in repo.ProductCategories
                from p in repo.Products
                select new { Category = c.Name, Product = p.Name };
            var crossJoinResult = crossJoinQuery.ToList();

            foreach (var i in crossJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nINNER JOIN:");
            var innerJoinQuery =
                from c in repo.ProductCategories
                join p in repo.Products on c.Id equals p.ProductCategoryId
                select new { c.Name, P = new { p.Price }, X = new { Y = string.Concat(c.Name, "-", p.Name) } };
            var innerJoinResult = innerJoinQuery.ToList();

            foreach (var i in innerJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = productIdsQuery.ToList();

            foreach (var id in productIdsQuery)
            {
                Console.WriteLine($"  {id}");
            }


            Console.WriteLine("\nCOUNT:");
            var productsQuery =
                from p in repo.Products
                select p;

            Console.WriteLine($"  Count = {productsQuery.Count()}");


            Console.WriteLine("\nTOTAL AMOUNT BY CATEGORY:");
            var totalAmountByCategoryQuery =
                from c in repo.ProductCategories
                join p in repo.Products
                on c.Id equals p.ProductCategoryId
                join i in repo.OrderItems
                on p.Id equals i.ProductId
                group new { c, p, i } by c.Name into g
                select new
            {
                Category = g.Key,
                Amount   = g.Sum(x => x.i.Quantity * x.p.Price),
                Amount2  = new { Amount = g.Sum(x => x.i.Quantity * x.p.Price) },
            };

            var totalAmountByCategroyResult = totalAmountByCategoryQuery.ToDictionary(x => x.Category);

            foreach (var i in totalAmountByCategroyResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nINVALID OPERATION:");
            try
            {
                var first = innerJoinQuery.First(x => false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }
        }
Example #6
0
        public async Task RunAsync()
        {
            var repo = new RemoteRepository(_url);

            Console.WriteLine("\nGET ALL PRODUCTS:");
            foreach (var i in await repo.Products.ToArrayAsync())
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }

            Console.WriteLine("\nCROSS JOIN:");
            Func <object, string> sufix = (x) => x + "ending";
            var crossJoinQuery          =
                from c in repo.ProductCategories
                from p in repo.Products
                select new { Category = "#" + c.Name + sufix("-"), p.Name };
            var crossJoinResult = await crossJoinQuery.ToListAsync();

            foreach (var i in crossJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nINNER JOIN:");
            var innerJoinQuery =
                from c in repo.ProductCategories
                join p in repo.Products on c.Id equals p.ProductCategoryId
                select new { c.Name, P = new { p.Price }, X = new { Y = string.Concat(c.Name, "-", p.Name) } };
            var innerJoinResult = await innerJoinQuery.ToListAsync();

            foreach (var i in innerJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = await productIdsQuery.ToListAsync();

            foreach (var id in productIdsQuery)
            {
                Console.WriteLine($"  {id}");
            }


            Console.WriteLine("\nCOUNT:");
            var productsQuery =
                from p in repo.Products
                select p;

            Console.WriteLine($"  Count = {await productsQuery.CountAsync()}");


            Console.WriteLine("\nTOTAL AMOUNT BY CATEGORY:");
            var totalAmountByCategoryQuery =
                from c in repo.ProductCategories
                join p in repo.Products
                on c.Id equals p.ProductCategoryId
                join i in repo.OrderItems
                on p.Id equals i.ProductId
                group new { c, p, i } by c.Name into g
                select new
            {
                Category = g.Key,
                Amount   = g.Sum(x => x.i.Quantity * x.p.Price),
                Amount2  = new { Amount = g.Sum(x => x.i.Quantity * x.p.Price) },
            };

            var totalAmountByCategroyResult = await totalAmountByCategoryQuery.ToDictionaryAsync(x => x.Category);

            foreach (var i in totalAmountByCategroyResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nINVALID OPERATION:");
            try
            {
                var first = await totalAmountByCategoryQuery.FirstAsync(x => false);
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }
        }
Example #7
0
        public async Task RunAsync()
        {
            using RemoteRepository repo = _repoProvider();

            PrintHeader("GET ALL PRODUCTS:");
            var list = await repo.Products
                       .ToListAsync()
                       .ConfigureAwait(false);

            foreach (var item in list)
            {
                PrintLine($"  {item.Id} | {item.Name} | {item.Price:C}");
            }

            PrintHeader("SELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = await productIdsQuery
                             .ToListAsync()
                             .ConfigureAwait(false);

            foreach (int id in productIdsQuery)
            {
                PrintLine($"  {id}");
            }

            var productsQuery =
                from p in repo.Products
                select p;

            PrintHeader("COUNT:");
            var asyncProductCount = productsQuery.CountAsync().ConfigureAwait(false);

            PrintLine($"  Count = {await asyncProductCount}");

            PrintHeader("INVALID OPERATIONS:");
            try
            {
                _ = await productsQuery
                    .FirstAsync(x => false)
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                PrintLine($"  {ex.GetType().Name}: {ex.Message}");
            }

            try
            {
                _ = await productsQuery
                    .SingleAsync()
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                PrintLine($"  {ex.GetType().Name}: {ex.Message}");
            }

            PrintHeader("GET MARKETS WITH PRODUCTS:");
            var marketsWithProducts = await repo.Markets
                                      .Include(x => x.Products)
                                      .Where(x => x.Products.Any())
                                      .ToListAsync()
                                      .ConfigureAwait(false);

            foreach (var market in marketsWithProducts)
            {
                PrintLine($"  {market.Name}");

                if (market.Products != null)
                {
                    foreach (var product in market.Products)
                    {
                        PrintLine($"    {product.Name}");
                    }
                }
            }

            PrintHeader("GET ALL PRODUCTS AND THEIR MARKETS:");
            var productsAndMarkets = await repo.Products
                                     .Include(x => x.Markets)
                                     .ToListAsync()
                                     .ConfigureAwait(false);

            foreach (var item in productsAndMarkets)
            {
                PrintLine($"  {item.Id} | {item.Name} | {item.Price:C}");

                foreach (var m in item.Markets)
                {
                    PrintLine($"         {m.Name}");
                }
            }

            PrintHeader("GET ALL PRODUCTS HAVING MARKETS DEFINED:");
            var query = repo.Products
                        .Where(p => p.Markets.Any());
            var queryResult = await query
                              .ToListAsync()
                              .ConfigureAwait(false);

            foreach (var item in queryResult)
            {
                PrintLine($"  {item.Id} | {item.Name} | {item.Price:C}");
            }

            PrintHeader("GET ALL PRODUCTS HAVING MARKETS DEFINED (INCLUDING MARKETS):");
            var queryResultWithMarketData = await query
                                            .Include(p => p.Markets)
                                            .ToListAsync()
                                            .ConfigureAwait(false);

            foreach (var item in queryResultWithMarketData)
            {
                var markets = item.Markets.Select(x => x.Name);
                PrintLine($"  {item.Id} | {item.Name} | {item.Price:C} | {string.Join("; ", markets)}");
            }
        }
Example #8
0
        public async Task RunAsync()
        {
            using RemoteRepository repo = _repoProvider();

            PrintHeader("GET ALL PRODUCTS:");
            var xl   = repo.Products.ToArray();
            var list = await repo.Products
                       .ToListAsync()
                       .ConfigureAwait(false);

            foreach (var item in list)
            {
                PrintLine($"  P#{item.Id}| {item.Name} | {item.Price:C}");
            }

            PrintHeader("SELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = await productIdsQuery
                             .ToListAsync()
                             .ConfigureAwait(false);

            foreach (int id in productIdsQuery)
            {
                PrintLine($"  P#{id}");
            }

            PrintHeader("COUNT:");
            var asyncProductCount = repo.Products.CountAsync().ConfigureAwait(false);

            PrintLine($"  Count = {await asyncProductCount}");

            PrintHeader("INVALID OPERATIONS:");
            try
            {
                _ = await repo.Products
                    .FirstAsync(x => false)
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                PrintLine($"  {ex.GetType().Name}: {ex.Message}");
            }

            try
            {
                _ = await repo.Products
                    .SingleAsync()
                    .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                PrintLine($"  {ex.GetType().Name}: {ex.Message}");
            }

            PrintHeader("GET MARKETS WITH PRODUCTS:");
            var marketsWithProducts = repo.Markets
                                      .Include(x => x.Products)
                                      .Where(x => x.Products.Any())
                                      .ToListAsync()
                                      .ConfigureAwait(false);

            foreach (var market in await marketsWithProducts)
            {
                PrintLine($"  M#{market.Id}| {market.Name}");

                foreach (var product in market.Products)
                {
                    PrintLine($"        P#{product.Id}| {product.Name}");
                }
            }

            PrintHeader("GET ALL PRODUCTS AND RELATED PRODUCTS OF CORRESPONDING MARKETS:");
            var productMarkets = await repo.Products
                                 .Include(x => x.Markets.Select(m => m.Products))
                                 .ToListAsync()
                                 .ConfigureAwait(false);

            foreach (var item in productMarkets)
            {
                PrintLine($"  P#{item.Id}| {item.Name} | {item.Price:C}");

                var relatedProducts = item.Markets
                                      .SelectMany(m => m.Products
                                                  .Where(p => p != item)
                                                  .Select(p => new { MarketId = m.Id, Product = p }));
                foreach (var p in relatedProducts)
                {
                    PrintLine($"         M#{p.MarketId}| P#{p.Product.Id}| {p.Product.Name}");
                }
            }

            PrintHeader("GET ALL PRODUCTS HAVING MARKETS DEFINED (USING INCLUDE):");
            var query = repo.Products
                        .Where(p => p.Markets.Any());
            var productIncludingMarketData = query
                                             .Include(p => p.Markets)
                                             .ToListAsync()
                                             .ConfigureAwait(false);

            foreach (var item in await productIncludingMarketData)
            {
                var markets = item.Markets.Select(x => x.Name);
                PrintLine($"  P#{item.Id}| {item.Name} | {string.Join("; ", markets)}");
            }

            PrintHeader("GET ALL PRODUCTS HAVING MARKETS DEFINED (USING PROJECTION):");
            var productSelectingMarketData = query
                                             .Select(x => new
            {
                x.Id,
                x.Name,
                x.Price,
                Markets = x.Markets.Select(m => m.Name),
            })
                                             .ToListAsync()
                                             .ConfigureAwait(false);

            foreach (var item in await productSelectingMarketData)
            {
                PrintLine($"  P#{item.Id}| {item.Name} | {string.Join("; ", item.Markets)}");
            }
        }
Example #9
0
        public void Run()
        {
            var repo = new RemoteRepository(_url);

            Console.WriteLine("\nGET ALL PRODUCTS:");
            foreach (var i in repo.Products)
            {
                Console.WriteLine($"  {i.Id} | {i.Name} | {i.Price:C}");
            }

            Console.WriteLine("\nINNER JOIN FOR FILTERING:");
            Func <object, string> sufix = (x) => x + "ending";
            var crossJoinQuery          =
                from c in repo.ProductCategories
                join p in repo.Products on c.Id equals p.ProductCategoryId
                where c.Name == "Fruits"
                select p;
            var crossJoinResult = crossJoinQuery.ToList();

            foreach (var i in crossJoinResult)
            {
                Console.WriteLine($"  {i.Id} - {i.Name}");
            }


            Console.WriteLine("\nINNER JOIN AND PROJECTION TO STRING:");
            var innerJoinQuery =
                from c in repo.ProductCategories
                join p in repo.Products on c.Id equals p.ProductCategoryId
                select string.Concat(c.Name, "-", p.Name);

            var innerJoinResult = innerJoinQuery.ToList();

            foreach (var i in innerJoinResult)
            {
                Console.WriteLine($"  {i}");
            }


            Console.WriteLine("\nSELECT IDs:");
            var productIdsQuery =
                from p in repo.Products
                orderby p.Price descending
                select p.Id;
            var productIds = productIdsQuery.ToList();

            foreach (var id in productIdsQuery)
            {
                Console.WriteLine($"  {id}");
            }


            Console.WriteLine("\nCOUNT:");
            var productsQuery =
                from p in repo.Products
                select p;

            Console.WriteLine($"  Count = {productsQuery.Count()}");


            Console.WriteLine("\nMAX TOTAL AMOUNT BY CATEGORY:");
            var totalAmountByCategoryQuery =
                from c in repo.ProductCategories
                join p in repo.Products
                on c.Id equals p.ProductCategoryId
                join i in repo.OrderItems
                on p.Id equals i.ProductId
                group new { c, p, i } by c.Name into g
            select g.Sum(x => x.i.Quantity *x.p.Price);

            Console.WriteLine($"  {totalAmountByCategoryQuery.Max()}");


            Console.WriteLine("\nINVALID OPERATION:");
            try
            {
                var first = totalAmountByCategoryQuery.First(x => false);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"  {ex.Message}");
            }
        }