using ShopOnline.Api.Entities; using ShopOnline.Models.Dtos; namespace ShopOnline.Api.Extensions { public static class DtoConversions { public static IEnumerable ConvertToDto(this IEnumerable productCategories) { return (from productCategory in productCategories select new ProductCategoryDto { Id = productCategory.Id, Name = productCategory.Name, IconCSS = productCategory.IconCSS }).ToList(); } public static IEnumerable ConvertToDto(this IEnumerable products) { return (from product in products select new ProductDto { Id = product.Id, Name = product.Name, Description = product.Description, ImageURL = product.ImageURL, Price = product.Price, Quantity = product.Quantity, CategoryId = product.ProductCategory.Id, CategoryName = product.ProductCategory.Name }).ToList(); } public static ProductDto ConvertToDto(this Product product) { return new ProductDto { Id = product.Id, Name = product.Name, Description= product.Description, ImageURL= product.ImageURL, Price= product.Price, Quantity= product.Quantity, CategoryId= product.CategoryId, CategoryName= product.ProductCategory.Name }; } public static IEnumerable ConvertToDto(this IEnumerable cartItems, IEnumerable products) { return (from cartItem in cartItems join product in products on cartItem.ProductId equals product.Id select new CartItemDto { Id = cartItem.Id, ProductId = cartItem.ProductId, ProductName = product.Name, ProductDescription = product.Description, ProductImageURL = product.ImageURL, Price = product.Price, CartId = cartItem.CartId, Quantity = cartItem.Quantity, TotalPrice = product.Price * cartItem.Quantity }).ToList(); } public static CartItemDto ConvertToDto(this CartItem cartItem, Product product) { return new CartItemDto { Id = cartItem.Id, ProductId = cartItem.ProductId, ProductName = product.Name, ProductDescription = product.Description, ProductImageURL = product.ImageURL, Price = product.Price, CartId = cartItem.CartId, Quantity = cartItem.Quantity, TotalPrice = product.Price * cartItem.Quantity }; } } }