public async Task <IHttpActionResult> GetProductOwnDetails() { CurrentIdentity identity = getIdentity(); long shopId = (await db.Shops.Where(p => p.UserId == identity.userId).FirstOrDefaultAsync()).Id; List <Product> products = await db.Products.Where(p => p.ShopId == shopId).ToListAsync(); if (products == null) { return(ResponseMessage(getHttpResponse(HttpStatusCode.NotFound))); } List <ProductOut> productsOut = new List <ProductOut>(); foreach (var product in products) { IList <Image> images = await db.Images.Where(p => p.ProductId == product.Id).ToListAsync(); IList <ProductsToCategory> productToCategories = await db.ProductsToCategories.Where(p => p.ProductId == product.Id).ToListAsync(); IList <Category> categories = new List <Category>(); foreach (var categoryEach in productToCategories) { categories.Add(await db.Categories.FirstOrDefaultAsync(p => p.Id == categoryEach.CategoryId)); } ProductOut productOut = new ProductOut(); productOut.Id = product.Id; productOut.Title = product.Title; productOut.Description = product.Description; productOut.DescriptionFull = product.DescriptionFull; productOut.Views = product.Views; productOut.IsActive = product.IsActive; productOut.CreatedAt = product.CreatedAt; productOut.UpdatedAt = product.UpdatedAt; productOut.ShopId = product.ShopId; productOut.Images = images; productOut.Categories = categories; productOut.Stock = product.Stock; productOut.Price = product.Price; productsOut.Add(productOut); } return(Ok(productsOut)); }
public async Task <IHttpActionResult> GetProductDetails(long id) { Product product = await db.Products.FindAsync(id); if (product == null) { return(ResponseMessage(getHttpResponse(HttpStatusCode.NotFound))); } IList <Image> images = await db.Images.Where(p => p.ProductId == id).ToListAsync(); IList <ProductsToCategory> productToCategories = await db.ProductsToCategories.Where(p => p.ProductId == id).ToListAsync(); IList <Category> categories = new List <Category>(); foreach (var categoryEach in productToCategories) { categories.Add(await db.Categories.FirstOrDefaultAsync(p => p.Id == categoryEach.CategoryId)); } ProductOut productOut = new ProductOut(); productOut.Id = product.Id; productOut.Title = product.Title; productOut.Description = product.Description; productOut.DescriptionFull = product.DescriptionFull; productOut.Views = product.Views; productOut.IsActive = product.IsActive; productOut.CreatedAt = product.CreatedAt; productOut.UpdatedAt = product.UpdatedAt; productOut.ShopId = product.ShopId; productOut.Images = images; productOut.Categories = categories; productOut.Stock = product.Stock; productOut.Price = product.Price; await IncrementView(id); return(Ok(productOut)); }
private async Task <ProductOut> GetProductsForShop(long id) { Product product = await db.Products.FindAsync(id); IList <Image> images = await db.Images.Where(p => p.ProductId == id).ToListAsync(); IList <ProductsToCategory> productToCategories = await db.ProductsToCategories.Where(p => p.ProductId == id).ToListAsync(); IList <Category> categories = new List <Category>(); foreach (var categoryEach in productToCategories) { categories.Add(await db.Categories.FirstOrDefaultAsync(p => p.Id == categoryEach.CategoryId)); } ProductOut productOut = new ProductOut(); productOut.Id = product.Id; productOut.Title = product.Title; productOut.Description = product.Description; productOut.DescriptionFull = product.DescriptionFull; productOut.Views = product.Views; productOut.IsActive = product.IsActive; productOut.CreatedAt = product.CreatedAt; productOut.UpdatedAt = product.UpdatedAt; productOut.ShopId = product.ShopId; productOut.Stock = product.Stock; productOut.Price = product.Price; if (images != null) { productOut.Images = images; } if (categories != null) { productOut.Categories = categories; } return(await Task.FromResult(productOut)); }