This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33e20cb
commit e978704
Showing
10 changed files
with
134 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
...front/Areas/Product/Views/Shared/Components/ListCategories/ListCategoriesViewComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using RookieShop.Storefront.Areas.Product.Models.Categories; | ||
using RookieShop.Storefront.Areas.Product.Services; | ||
using RookieShop.Storefront.Services; | ||
|
||
namespace RookieShop.Storefront.Areas.Product.Views.Shared.Components.ListCategories; | ||
|
||
[ViewComponent] | ||
public class ListCategoriesViewComponent(ICategoryService categoryService) : ViewComponent | ||
public class ListCategoriesViewComponent( | ||
ICategoryService categoryService, | ||
IMemoryCacheService memoryCacheService) : ViewComponent | ||
{ | ||
public async Task<IViewComponentResult> InvokeAsync() | ||
{ | ||
var categories = await categoryService.ListCategoriesAsync(); | ||
return View(categories); | ||
var data = await memoryCacheService.GetOrSetAsync(nameof(ListCategoriesViewModel), | ||
categoryService.ListCategoriesAsync); | ||
|
||
return View(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace RookieShop.Storefront.Services; | ||
|
||
public interface IMemoryCacheService | ||
{ | ||
public Task<T?> GetOrSetAsync<T>(string cacheKey, Func<Task<T>> getItemCallback); | ||
public void Remove(string cacheKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Ardalis.GuardClauses; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace RookieShop.Storefront.Services; | ||
|
||
public sealed class MemoryCacheService(IMemoryCache cache) : IMemoryCacheService | ||
{ | ||
private readonly MemoryCacheEntryOptions _cacheDuration = new() | ||
{ | ||
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) | ||
}; | ||
|
||
public async Task<T?> GetOrSetAsync<T>(string cacheKey, Func<Task<T>> getItemCallback) | ||
{ | ||
Guard.Against.NullOrEmpty(cacheKey); | ||
|
||
if (cache.TryGetValue(cacheKey, out T? cachedItem)) | ||
return cachedItem; | ||
|
||
var item = await getItemCallback(); | ||
|
||
if (item is null) | ||
return item; | ||
|
||
cache.Set(cacheKey, item, _cacheDuration); | ||
|
||
return item; | ||
} | ||
|
||
public void Remove(string cacheKey) | ||
{ | ||
Guard.Against.NullOrEmpty(cacheKey); | ||
cache.Remove(cacheKey); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
ui/storefront/Views/Shared/Components/CategoryDropDown/CategoryDropDownViewComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using RookieShop.Storefront.Areas.Product.Models.Categories; | ||
using RookieShop.Storefront.Areas.Product.Services; | ||
using RookieShop.Storefront.Services; | ||
|
||
namespace RookieShop.Storefront.Views.Shared.Components.CategoryDropDown; | ||
|
||
[ViewComponent] | ||
public sealed class CategoryDropDownViewComponent( | ||
ICategoryService categoryService, | ||
IMemoryCacheService memoryCacheService) : ViewComponent | ||
{ | ||
public async Task<IViewComponentResult> InvokeAsync() | ||
{ | ||
var data = await memoryCacheService.GetOrSetAsync(nameof(ListCategoriesViewModel), | ||
categoryService.ListCategoriesAsync); | ||
|
||
return View(data); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
ui/storefront/Views/Shared/Components/CategoryDropDown/Default.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@model RookieShop.Storefront.Areas.Product.Models.Categories.ListCategoriesViewModel | ||
|
||
@if(Model.Categories.Count > 0) | ||
{ | ||
<li x-data="{ open: false }" class="relative group" @@click.away=" open=false"> | ||
<button class="block py-2 px-3 rounded hover:bg-gray-100 md:hover:bg-transparent md:hover:text-blue-700 md:p-0 text-gray-900" @@click="open = !open"> | ||
Categories | ||
<img asp-append-version="true" loading="lazy" class="inline-block w-4 h-4 ml-2" src="~/imgs/filter/arrow-down.svg" alt="Sort" /> | ||
</button> | ||
|
||
<div x-show="open" class="absolute mt-2 w-48 bg-white rounded-md shadow-lg z-10"> | ||
@foreach(var category in Model.Categories) | ||
{ | ||
<a asp-area="Product" | ||
asp-controller="Product" | ||
asp-action="Index" | ||
asp-route-category="@category.Id" | ||
asp-route-page="1" | ||
class="block px-4 py-2 text-gray-800 hover:bg-gray-100"> | ||
@category.Name | ||
</a> | ||
} | ||
</div> | ||
</li> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters