public CategoryModel GetCategoryModel(int CategoryId) { using (var db = new TankshopDbContext()) { var c = db.Categories.Find(CategoryId); var productModels = new List<ProductModel>(); foreach (var product in c.Products) { var imageModels = new List<ImageModel>(); foreach (var image in product.Images) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productModels.Add(productModel); } var categoryModel = new CategoryModel() { CategoryId = c.CategoryId, CategoryName = c.Name, Products = productModels }; return categoryModel; } }
public List<CategoryModel> GetAllCategoryModels() { var db = new TankshopDbContext(); var dbCategories = db.Categories.ToList(); var categoryModels = new List<CategoryModel>(); foreach (var c in dbCategories) { var productModels = new List<ProductModel>(); foreach(var product in c.Products) { var imageModels = new List<ImageModel>(); foreach(var image in product.Images) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productModels.Add(productModel); } var categoryModel = new CategoryModel() { CategoryId = c.CategoryId, CategoryName = c.Name, Products = productModels }; categoryModels.Add(categoryModel); } return categoryModels; }
public List<ProductModel> GetAllProductModels() { var productModels = new List<ProductModel>(); try { using (var db = new TankshopDbContext()) { var dbProducts = db.Products.ToList(); foreach (var product in dbProducts) { var dbProductImages = db.Images.Where(i => i.ProductId == product.ProductId).ToList(); var imageModels = new List<ImageModel>(); foreach (var image in dbProductImages) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productModels.Add(productModel); } return productModels; } } catch (Exception e) { LogHandler.WriteToLog(e); return productModels; } }
public List<ProductModel> GetProductsByCategory(int categoryId) { using (var db = new TankshopDbContext()) { var dbProducts = db.Products.Where(p => p.CategoryId == categoryId).ToList(); var productModels = new List<ProductModel>(); foreach (var product in dbProducts) { var dbProductImages = db.Images.Where(i => i.ProductId == product.ProductId).ToList(); var imageModels = new List<ImageModel>(); foreach (var image in dbProductImages) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productModels.Add(productModel); } return productModels; } }
public List<ProductModel> GetProducts(List<int> productIdList) { var productList = new List<ProductModel>(); try { using (var db = new TankshopDbContext()) { foreach (var productId in productIdList) { var product = db.Products.Find(productId); if (product != null) { var dbProductImages = db.Images.Where(i => i.ProductId == product.ProductId).ToList(); var imageModels = new List<ImageModel>(); foreach (var image in dbProductImages) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productList.Add(productModel); } } return productList; } } catch (Exception) { return productList; } }
public List<ProductModel> GetProductModels(string searchstr) { using (var db = new TankshopDbContext()) { var productList = new List<ProductModel>(); try { var dbProducts = db.Products.Where(p => p.Name.Contains(searchstr) || p.Category.Name.Contains(searchstr) //|| p.Description.Contains(searchstr) ).ToList(); foreach (var product in dbProducts) { var dbProductImages = db.Images.Where(i => i.ProductId == product.ProductId).ToList(); var imageModels = new List<ImageModel>(); foreach (var image in dbProductImages) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; productList.Add(productModel); } return productList; } catch (Exception) { return productList; } } }
public ProductModel GetProductModel(int ProductId) { using (var db = new TankshopDbContext()) { var product = db.Products.Find(ProductId); if (product == null) return null; var dbProductImages = db.Images.Where(i => i.ProductId == product.ProductId).ToList(); var imageModels = new List<ImageModel>(); foreach (var image in dbProductImages) { var imageModel = new ImageModel() { ImageId = image.ImageId, ImageUrl = image.ImageUrl, ProductId = image.ProductId }; imageModels.Add(imageModel); } var productModel = new ProductModel() { CategoryId = product.CategoryId, CategoryName = product.Category.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, ProductName = product.Name, Stock = product.Stock, Images = imageModels }; return productModel; } }