From 6023073b3ad3ce3960fc8645ea757848c3d37125 Mon Sep 17 00:00:00 2001 From: Kira Date: Fri, 16 Sep 2022 11:30:18 -0700 Subject: [PATCH] Dynamic Sidebar implemented, with appropriate icons and separately populated category pages --- .../Controllers/ProductController.cs | 37 ++ ShopOnline.Api/Data/ShopOnlineDbContext.cs | 13 +- ShopOnline.Api/Entities/ProductCategory.cs | 1 + ShopOnline.Api/Extensions/DtoConversions.cs | 11 + ...171516_AddProductCategoryIcons.Designer.cs | 420 ++++++++++++++++++ .../20220916171516_AddProductCategoryIcons.cs | 54 +++ .../ShopOnlineDbContextModelSnapshot.cs | 8 + .../Contracts/IProductRepository.cs | 2 + .../Repositories/ProductRepository.cs | 9 + ShopOnline.Models/Dtos/ProductCategoryDto.cs | 15 + .../Pages/DisplayCustomSpinner.razor | 2 +- ShopOnline.Web/Pages/Products.razor | 2 +- ShopOnline.Web/Pages/ProductsByCategory.razor | 24 + .../Pages/ProductsByCategoryBase.cs | 39 ++ .../Services/Contracts/IProductService.cs | 3 +- ShopOnline.Web/Services/ProductService.cs | 54 +++ ShopOnline.Web/Shared/NavMenu.razor | 19 +- .../Shared/ProductCategoriesNavMenu.razor | 21 + .../Shared/ProductCategoriesNavMenu.razor.css | 62 +++ .../Shared/ProductCategoriesNavMenuBase.cs | 29 ++ ShopOnline.Web/_Imports.razor | 1 + 21 files changed, 806 insertions(+), 20 deletions(-) create mode 100644 ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.Designer.cs create mode 100644 ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.cs create mode 100644 ShopOnline.Models/Dtos/ProductCategoryDto.cs create mode 100644 ShopOnline.Web/Pages/ProductsByCategory.razor create mode 100644 ShopOnline.Web/Pages/ProductsByCategoryBase.cs create mode 100644 ShopOnline.Web/Shared/ProductCategoriesNavMenu.razor create mode 100644 ShopOnline.Web/Shared/ProductCategoriesNavMenu.razor.css create mode 100644 ShopOnline.Web/Shared/ProductCategoriesNavMenuBase.cs diff --git a/ShopOnline.Api/Controllers/ProductController.cs b/ShopOnline.Api/Controllers/ProductController.cs index b666834..3bf66f9 100644 --- a/ShopOnline.Api/Controllers/ProductController.cs +++ b/ShopOnline.Api/Controllers/ProductController.cs @@ -74,5 +74,42 @@ namespace ShopOnline.Api.Controllers "Error retrieving data from the database."); } } + + [HttpGet] + [Route(nameof(GetProductCategories))] + public async Task>> GetProductCategories() + { + try + { + var productCategories = await productRepository.GetCategories(); + var productCategoryDtos = productCategories.ConvertToDto(); + + return Ok(productCategoryDtos); + } + catch (Exception) + { + return StatusCode(StatusCodes.Status500InternalServerError, + "Error retrieving data from the database."); + } + } + + [HttpGet] + [Route("{categoryId}/GetItemsByCategory")] + public async Task>> GetItemsByCategory(int categoryId) + { + try + { + var products = await productRepository.GetItemsByCategory(categoryId); + var productCategories = await productRepository.GetCategories(); + var productDtos = products.ConvertToDto(productCategories); + + return Ok(productDtos); + } + catch (Exception) + { + return StatusCode(StatusCodes.Status500InternalServerError, + "Error retrieving data from the database."); + } + } } } diff --git a/ShopOnline.Api/Data/ShopOnlineDbContext.cs b/ShopOnline.Api/Data/ShopOnlineDbContext.cs index b577965..c4e9eb6 100644 --- a/ShopOnline.Api/Data/ShopOnlineDbContext.cs +++ b/ShopOnline.Api/Data/ShopOnlineDbContext.cs @@ -292,22 +292,27 @@ namespace ShopOnline.Api.Data modelBuilder.Entity().HasData(new ProductCategory { Id = 1, - Name = "Beauty" + Name = "Beauty", + IconCSS = "fas fa-spa" }); modelBuilder.Entity().HasData(new ProductCategory { Id = 2, - Name = "Furniture" + Name = "Furniture", + IconCSS = "fas fa-couch" }); modelBuilder.Entity().HasData(new ProductCategory { Id = 3, - Name = "Electronics" + Name = "Electronics", + IconCSS = "fas fa-headphones" + }); modelBuilder.Entity().HasData(new ProductCategory { Id = 4, - Name = "Shoes" + Name = "Shoes", + IconCSS = "fas fa-shoe-prints" }); } diff --git a/ShopOnline.Api/Entities/ProductCategory.cs b/ShopOnline.Api/Entities/ProductCategory.cs index 8881a61..50803fe 100644 --- a/ShopOnline.Api/Entities/ProductCategory.cs +++ b/ShopOnline.Api/Entities/ProductCategory.cs @@ -4,5 +4,6 @@ { public int Id { get; set; } public string Name { get; set; } + public string IconCSS { get; set; } } } diff --git a/ShopOnline.Api/Extensions/DtoConversions.cs b/ShopOnline.Api/Extensions/DtoConversions.cs index 2d2ad06..4af1f0d 100644 --- a/ShopOnline.Api/Extensions/DtoConversions.cs +++ b/ShopOnline.Api/Extensions/DtoConversions.cs @@ -5,6 +5,17 @@ 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, IEnumerable productCategories) { diff --git a/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.Designer.cs b/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.Designer.cs new file mode 100644 index 0000000..4f1f762 --- /dev/null +++ b/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.Designer.cs @@ -0,0 +1,420 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using ShopOnline.Api.Data; + +#nullable disable + +namespace ShopOnline.Api.Migrations +{ + [DbContext(typeof(ShopOnlineDbContext))] + [Migration("20220916171516_AddProductCategoryIcons")] + partial class AddProductCategoryIcons + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("ShopOnline.Api.Entities.Cart", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("UserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Carts"); + + b.HasData( + new + { + Id = 1, + UserId = 1 + }, + new + { + Id = 2, + UserId = 2 + }); + }); + + modelBuilder.Entity("ShopOnline.Api.Entities.CartItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CartId") + .HasColumnType("int"); + + b.Property("ProductId") + .HasColumnType("int"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("CartItems"); + }); + + modelBuilder.Entity("ShopOnline.Api.Entities.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("CategoryId") + .HasColumnType("int"); + + b.Property("Description") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ImageURL") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("decimal(18,2)"); + + b.Property("Quantity") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Products"); + + b.HasData( + new + { + Id = 1, + CategoryId = 1, + Description = "A kit provided by Glossier, containing skin care, hair care and makeup products", + ImageURL = "/Images/Beauty/Beauty1.png", + Name = "Glossier - Beauty Kit", + Price = 100m, + Quantity = 100 + }, + new + { + Id = 2, + CategoryId = 1, + Description = "A kit provided by Curology, containing skin care products", + ImageURL = "/Images/Beauty/Beauty2.png", + Name = "Curology - Skin Care Kit", + Price = 50m, + Quantity = 45 + }, + new + { + Id = 3, + CategoryId = 1, + Description = "A kit provided by Curology, containing skin care products", + ImageURL = "/Images/Beauty/Beauty3.png", + Name = "Cocooil - Organic Coconut Oil", + Price = 20m, + Quantity = 30 + }, + new + { + Id = 4, + CategoryId = 1, + Description = "A kit provided by Schwarzkopf, containing skin care and hair care products", + ImageURL = "/Images/Beauty/Beauty4.png", + Name = "Schwarzkopf - Hair Care and Skin Care Kit", + Price = 50m, + Quantity = 60 + }, + new + { + Id = 5, + CategoryId = 1, + Description = "Skin Care Kit, containing skin care and hair care products", + ImageURL = "/Images/Beauty/Beauty5.png", + Name = "Skin Care Kit", + Price = 30m, + Quantity = 85 + }, + new + { + Id = 6, + CategoryId = 3, + Description = "Air Pods - in-ear wireless headphones", + ImageURL = "/Images/Electronic/Electronics1.png", + Name = "Air Pods", + Price = 100m, + Quantity = 120 + }, + new + { + Id = 7, + CategoryId = 3, + Description = "On-ear Golden Headphones - these headphones are not wireless", + ImageURL = "/Images/Electronic/Electronics2.png", + Name = "On-ear Golden Headphones", + Price = 40m, + Quantity = 200 + }, + new + { + Id = 8, + CategoryId = 3, + Description = "On-ear Black Headphones - these headphones are not wireless", + ImageURL = "/Images/Electronic/Electronics3.png", + Name = "On-ear Black Headphones", + Price = 40m, + Quantity = 300 + }, + new + { + Id = 9, + CategoryId = 3, + Description = "Sennheiser Digital Camera - High quality digital camera provided by Sennheiser - includes tripod", + ImageURL = "/Images/Electronic/Electronic4.png", + Name = "Sennheiser Digital Camera with Tripod", + Price = 600m, + Quantity = 20 + }, + new + { + Id = 10, + CategoryId = 3, + Description = "Canon Digital Camera - High quality digital camera provided by Canon", + ImageURL = "/Images/Electronic/Electronic5.png", + Name = "Canon Digital Camera", + Price = 500m, + Quantity = 15 + }, + new + { + Id = 11, + CategoryId = 3, + Description = "Gameboy - Provided by Nintendo", + ImageURL = "/Images/Electronic/technology6.png", + Name = "Nintendo Gameboy", + Price = 100m, + Quantity = 60 + }, + new + { + Id = 12, + CategoryId = 2, + Description = "Very comfortable black leather office chair", + ImageURL = "/Images/Furniture/Furniture1.png", + Name = "Black Leather Office Chair", + Price = 50m, + Quantity = 212 + }, + new + { + Id = 13, + CategoryId = 2, + Description = "Very comfortable pink leather office chair", + ImageURL = "/Images/Furniture/Furniture2.png", + Name = "Pink Leather Office Chair", + Price = 50m, + Quantity = 112 + }, + new + { + Id = 14, + CategoryId = 2, + Description = "Very comfortable lounge chair", + ImageURL = "/Images/Furniture/Furniture3.png", + Name = "Lounge Chair", + Price = 70m, + Quantity = 90 + }, + new + { + Id = 15, + CategoryId = 2, + Description = "Very comfortable Silver lounge chair", + ImageURL = "/Images/Furniture/Furniture4.png", + Name = "Silver Lounge Chair", + Price = 120m, + Quantity = 95 + }, + new + { + Id = 16, + CategoryId = 2, + Description = "White and blue Porcelain Table Lamp", + ImageURL = "/Images/Furniture/Furniture6.png", + Name = "Porcelain Table Lamp", + Price = 15m, + Quantity = 100 + }, + new + { + Id = 17, + CategoryId = 2, + Description = "Office Table Lamp", + ImageURL = "/Images/Furniture/Furniture7.png", + Name = "Office Table Lamp", + Price = 20m, + Quantity = 73 + }, + new + { + Id = 18, + CategoryId = 4, + Description = "Comfortable Puma Sneakers in most sizes", + ImageURL = "/Images/Shoes/Shoes1.png", + Name = "Puma Sneakers", + Price = 100m, + Quantity = 50 + }, + new + { + Id = 19, + CategoryId = 4, + Description = "Colorful trainsers - available in most sizes", + ImageURL = "/Images/Shoes/Shoes2.png", + Name = "Colorful Trainers", + Price = 150m, + Quantity = 60 + }, + new + { + Id = 20, + CategoryId = 4, + Description = "Blue Nike Trainers - available in most sizes", + ImageURL = "/Images/Shoes/Shoes3.png", + Name = "Blue Nike Trainers", + Price = 200m, + Quantity = 70 + }, + new + { + Id = 21, + CategoryId = 4, + Description = "Colorful Hummel Trainers - available in most sizes", + ImageURL = "/Images/Shoes/Shoes4.png", + Name = "Colorful Hummel Trainers", + Price = 120m, + Quantity = 120 + }, + new + { + Id = 22, + CategoryId = 4, + Description = "Red Nike Trainers - available in most sizes", + ImageURL = "/Images/Shoes/Shoes5.png", + Name = "Red Nike Trainers", + Price = 200m, + Quantity = 100 + }, + new + { + Id = 23, + CategoryId = 4, + Description = "Birkenstock Sandles - available in most sizes", + ImageURL = "/Images/Shoes/Shoes6.png", + Name = "Birkenstock Sandles", + Price = 50m, + Quantity = 150 + }); + }); + + modelBuilder.Entity("ShopOnline.Api.Entities.ProductCategory", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("IconCSS") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("ProductCategories"); + + b.HasData( + new + { + Id = 1, + IconCSS = "fas fa-spa", + Name = "Beauty" + }, + new + { + Id = 2, + IconCSS = "fas fa-couch", + Name = "Furniture" + }, + new + { + Id = 3, + IconCSS = "fas fa-headphones", + Name = "Electronics" + }, + new + { + Id = 4, + IconCSS = "fas fa-shoe-prints", + Name = "Shoes" + }); + }); + + modelBuilder.Entity("ShopOnline.Api.Entities.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("UserName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Users"); + + b.HasData( + new + { + Id = 1, + UserName = "Bob" + }, + new + { + Id = 2, + UserName = "Sarah" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.cs b/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.cs new file mode 100644 index 0000000..e71f15a --- /dev/null +++ b/ShopOnline.Api/Migrations/20220916171516_AddProductCategoryIcons.cs @@ -0,0 +1,54 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ShopOnline.Api.Migrations +{ + public partial class AddProductCategoryIcons : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "IconCSS", + table: "ProductCategories", + type: "nvarchar(max)", + nullable: false, + defaultValue: ""); + + migrationBuilder.UpdateData( + table: "ProductCategories", + keyColumn: "Id", + keyValue: 1, + column: "IconCSS", + value: "fas fa-spa"); + + migrationBuilder.UpdateData( + table: "ProductCategories", + keyColumn: "Id", + keyValue: 2, + column: "IconCSS", + value: "fas fa-couch"); + + migrationBuilder.UpdateData( + table: "ProductCategories", + keyColumn: "Id", + keyValue: 3, + column: "IconCSS", + value: "fas fa-headphones"); + + migrationBuilder.UpdateData( + table: "ProductCategories", + keyColumn: "Id", + keyValue: 4, + column: "IconCSS", + value: "fas fa-shoe-prints"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "IconCSS", + table: "ProductCategories"); + } + } +} diff --git a/ShopOnline.Api/Migrations/ShopOnlineDbContextModelSnapshot.cs b/ShopOnline.Api/Migrations/ShopOnlineDbContextModelSnapshot.cs index 7dcce58..8f374b8 100644 --- a/ShopOnline.Api/Migrations/ShopOnlineDbContextModelSnapshot.cs +++ b/ShopOnline.Api/Migrations/ShopOnlineDbContextModelSnapshot.cs @@ -345,6 +345,10 @@ namespace ShopOnline.Api.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + b.Property("IconCSS") + .IsRequired() + .HasColumnType("nvarchar(max)"); + b.Property("Name") .IsRequired() .HasColumnType("nvarchar(max)"); @@ -357,21 +361,25 @@ namespace ShopOnline.Api.Migrations new { Id = 1, + IconCSS = "fas fa-spa", Name = "Beauty" }, new { Id = 2, + IconCSS = "fas fa-couch", Name = "Furniture" }, new { Id = 3, + IconCSS = "fas fa-headphones", Name = "Electronics" }, new { Id = 4, + IconCSS = "fas fa-shoe-prints", Name = "Shoes" }); }); diff --git a/ShopOnline.Api/Repositories/Contracts/IProductRepository.cs b/ShopOnline.Api/Repositories/Contracts/IProductRepository.cs index 2d7a246..7166840 100644 --- a/ShopOnline.Api/Repositories/Contracts/IProductRepository.cs +++ b/ShopOnline.Api/Repositories/Contracts/IProductRepository.cs @@ -8,6 +8,8 @@ namespace ShopOnline.Api.Repositories.Contracts Task GetItem(int id); Task> GetCategories(); + Task GetCategory(int id); + Task> GetItemsByCategory(int id); } } diff --git a/ShopOnline.Api/Repositories/ProductRepository.cs b/ShopOnline.Api/Repositories/ProductRepository.cs index 8a668b4..a081775 100644 --- a/ShopOnline.Api/Repositories/ProductRepository.cs +++ b/ShopOnline.Api/Repositories/ProductRepository.cs @@ -36,5 +36,14 @@ namespace ShopOnline.Api.Repositories return products; } + + public async Task> GetItemsByCategory(int id) + { + var products = await (from product in shopOnlineDbContext.Products + where product.CategoryId == id + select product).ToListAsync(); + + return products; + } } } diff --git a/ShopOnline.Models/Dtos/ProductCategoryDto.cs b/ShopOnline.Models/Dtos/ProductCategoryDto.cs new file mode 100644 index 0000000..32fb2c2 --- /dev/null +++ b/ShopOnline.Models/Dtos/ProductCategoryDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShopOnline.Models.Dtos +{ + public class ProductCategoryDto + { + public int Id { get; set; } + public string Name { get; set; } + public string IconCSS { get; set; } + } +} diff --git a/ShopOnline.Web/Pages/DisplayCustomSpinner.razor b/ShopOnline.Web/Pages/DisplayCustomSpinner.razor index 77318ca..9a887b3 100644 --- a/ShopOnline.Web/Pages/DisplayCustomSpinner.razor +++ b/ShopOnline.Web/Pages/DisplayCustomSpinner.razor @@ -1 +1 @@ -
\ No newline at end of file +
\ No newline at end of file diff --git a/ShopOnline.Web/Pages/Products.razor b/ShopOnline.Web/Pages/Products.razor index 10ef51b..9768b83 100644 --- a/ShopOnline.Web/Pages/Products.razor +++ b/ShopOnline.Web/Pages/Products.razor @@ -22,7 +22,7 @@ else @foreach (var prodGroup in GetGroupedProductsByCategory()) {
-

@prodGroup.FirstOrDefault(pg=>pg.CategoryId == prodGroup.Key).CategoryName

+

@prodGroup.FirstOrDefault(pg=>pg.CategoryId == prodGroup.Key).CategoryName


diff --git a/ShopOnline.Web/Pages/ProductsByCategory.razor b/ShopOnline.Web/Pages/ProductsByCategory.razor new file mode 100644 index 0000000..13c4952 --- /dev/null +++ b/ShopOnline.Web/Pages/ProductsByCategory.razor @@ -0,0 +1,24 @@ +@page "/ProductsByCategory/{CategoryId:int}" +@inherits ProductsByCategoryBase + +@if (Products == null && ErrorMessage == null) +{ +
+
+ +
+
+} +else if (ErrorMessage != null) +{ + +} +else { +

@CategoryName

+ @if(Products.Count() > 0) + { +
+ +
+ } +} \ No newline at end of file diff --git a/ShopOnline.Web/Pages/ProductsByCategoryBase.cs b/ShopOnline.Web/Pages/ProductsByCategoryBase.cs new file mode 100644 index 0000000..7bc4fdf --- /dev/null +++ b/ShopOnline.Web/Pages/ProductsByCategoryBase.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Components; +using ShopOnline.Models.Dtos; +using ShopOnline.Web.Services.Contracts; + +namespace ShopOnline.Web.Pages +{ + public class ProductsByCategoryBase:ComponentBase + { + [Parameter] + public int CategoryId { get; set; } + + [Inject] + public IProductService ProductService { get; set; } + + public IEnumerable Products { get; set; } + public string CategoryName { get; set; } + public string ErrorMessage { get; set; } + + protected override async Task OnParametersSetAsync() + { + try + { + Products = await ProductService.GetItemsByCategory(CategoryId); + if(Products != null && Products.Count() > 0) + { + var productDto = Products.FirstOrDefault(p => p.CategoryId == CategoryId); + if(productDto != null) + { + CategoryName = productDto.CategoryName; + } + } + } + catch (Exception ex) + { + ErrorMessage = ex.Message; + } + } + } +} diff --git a/ShopOnline.Web/Services/Contracts/IProductService.cs b/ShopOnline.Web/Services/Contracts/IProductService.cs index acc5e31..373d22a 100644 --- a/ShopOnline.Web/Services/Contracts/IProductService.cs +++ b/ShopOnline.Web/Services/Contracts/IProductService.cs @@ -6,6 +6,7 @@ namespace ShopOnline.Web.Services.Contracts { Task> GetItems(); Task GetItem(int id); - + Task> GetProductCategories(); + Task> GetItemsByCategory(int categoryId); } } diff --git a/ShopOnline.Web/Services/ProductService.cs b/ShopOnline.Web/Services/ProductService.cs index 41b8cad..7ba0413 100644 --- a/ShopOnline.Web/Services/ProductService.cs +++ b/ShopOnline.Web/Services/ProductService.cs @@ -65,5 +65,59 @@ namespace ShopOnline.Web.Services throw; } } + + public async Task> GetItemsByCategory(int categoryId) + { + try + { + var response = await httpClient.GetAsync($"api/Product/{categoryId}/GetItemsByCategory"); + + if (response.IsSuccessStatusCode) + { + if (response.StatusCode == System.Net.HttpStatusCode.NoContent) + { + return Enumerable.Empty(); + } + return await response.Content.ReadFromJsonAsync>(); + } + else + { + var message = await response.Content.ReadAsStringAsync(); + throw new Exception($"Http Status Code - {response.StatusCode} Message - {message}"); + } + } + catch (Exception) + { + + throw; + } + } + + public async Task> GetProductCategories() + { + try + { + var response = await httpClient.GetAsync("api/Product/GetProductCategories"); + + if (response.IsSuccessStatusCode) + { + if(response.StatusCode == System.Net.HttpStatusCode.NoContent) + { + return Enumerable.Empty(); + } + return await response.Content.ReadFromJsonAsync>(); + } + else + { + var message = await response.Content.ReadAsStringAsync(); + throw new Exception($"Http Status Code - {response.StatusCode} Message - {message}"); + } + } + catch (Exception) + { + + throw; + } + } } } diff --git a/ShopOnline.Web/Shared/NavMenu.razor b/ShopOnline.Web/Shared/NavMenu.razor index ba8f970..f2e7fee 100644 --- a/ShopOnline.Web/Shared/NavMenu.razor +++ b/ShopOnline.Web/Shared/NavMenu.razor @@ -3,7 +3,7 @@