Example #1
0
        public Category Get(int id)
        {
            DataRow[] query = _table.Select($"Id = {id}");

            if (query.Length == 0)
            {
                return(null);
            }

            DataRow row      = query[0];
            var     category = new Category
            {
                Id       = (int)row["Id"],
                Name     = (string)row["Name"],
                Products = new List <Product>()
            };

            var productRows = db.GetChildRowsFor(row, "CategoryProduct");

            for (int i = 0; i < productRows.Length; i++)
            {
                category.Products.Add(new Product
                {
                    Id         = (int)productRows[i]["Id"],
                    Name       = (string)productRows[i]["Name"],
                    CategoryId = (int)productRows[i]["CategoryId"],
                    Price      = (double)productRows[i]["Price"]
                });
            }

            return(category);
        }
Example #2
0
        public IEnumerable <Product> GetAll()
        {
            var products = new List <Product>();

            for (int curRow = 0; curRow < _table.Rows.Count; curRow++)
            {
                var product = new Product
                {
                    Id         = (int)_table.Rows[curRow]["Id"],
                    Name       = (string)_table.Rows[curRow]["Name"],
                    CategoryId = (int)_table.Rows[curRow]["CategoryId"],
                    Price      = (double)_table.Rows[curRow]["Price"],

                    Category = new Category
                    {
                        Id = (int)_context.GetParentRowFor
                                 (_table.Rows[curRow], "CategoryProduct")["Id"],

                        Name = (string)_context.GetParentRowFor
                                   (_table.Rows[curRow], "CategoryProduct")["Name"]
                    },

                    Providers = new List <Provider>()
                };

                var providerRows = _context.GetChildRowsFor(_table.Rows[curRow], "ProductProviderProducts");

                for (int i = 0; i < providerRows.Length; i++)
                {
                    if ((int)providerRows[i]["Product_Id"] == product.Id)
                    {
                        int providerId = (int)providerRows[i]["Provider_Id"];

                        for (int j = 0; j < _context.Providers.Rows.Count; j++)
                        {
                            if ((int)_context.Providers.Rows[j]["Id"] == providerId)
                            {
                                product.Providers.Add(new Provider
                                {
                                    Id       = (int)_context.Providers.Rows[j]["Id"],
                                    Name     = (string)_context.Providers.Rows[j]["Name"],
                                    Location = (string)_context.Providers.Rows[j]["Location"]
                                });
                            }
                        }
                    }
                }

                products.Add(product);
            }
            return(products);
        }