Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@attribute [Route(Urls.ProductPage + "/{Id:guid}/{Name}")]
@attribute [Route("{culture?}" + Urls.ProductPage + "/{Id:guid}/{Name}")]
@attribute [Route(Urls.ProductPage + "/{Id:int}/{Name}")]
@attribute [Route("{culture?}" + Urls.ProductPage + "/{Id:int}/{Name}")]
@attribute [AppResponseCache(SharedMaxAge = 3600 * 24)]
@inherits AppPageBase

<PageTitle>@product?.Name</PageTitle>
<PageTitle>@Name</PageTitle>

<section>
<BitStack Gap="2rem" Class="root-stack">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public partial class ProductPage
protected override string? Subtitle => string.Empty;


[Parameter] public Guid Id { get; set; }
/// <summary>
/// <inheritdoc cref="ProductDto.ShortId"/>
/// </summary>
[Parameter] public int Id { get; set; }
[Parameter] public string? Name { get; set; }


Expand All @@ -29,14 +32,9 @@ public partial class ProductPage

protected override async Task OnInitAsync()
{
await base.OnInitAsync();

await LoadProduct();
await Task.WhenAll(LoadProduct(), LoadSimilarProducts(), LoadSiblingProducts());

if (InPrerenderSession is false)
{
await Task.WhenAll(LoadSimilarProducts(), LoadSiblingProducts());
}
await base.OnInitAsync();
}

private async Task LoadProduct()
Expand All @@ -54,13 +52,11 @@ private async Task LoadProduct()

private async Task LoadSimilarProducts()
{
if (product is null) return;

try
{
similarProducts = await productViewController
.WithQuery(new ODataQuery { Top = 10 })
.GetSimilar(product.Id, CurrentCancellationToken);
.GetSimilar(Id, CurrentCancellationToken);
}
finally
{
Expand All @@ -71,13 +67,11 @@ private async Task LoadSimilarProducts()

private async Task LoadSiblingProducts()
{
if (product is null || product.CategoryId.HasValue is false) return;

try
{
siblingProducts = await productViewController
.WithQuery(new ODataQuery { Top = 10, Filter = $"{nameof(ProductDto.Id)} ne {product.Id}" })
.GetSiblings(product.CategoryId.Value, CurrentCancellationToken);
.WithQuery(new ODataQuery { Top = 10 })
.GetSiblings(Id, CurrentCancellationToken);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"tools": {
"dotnet-ef": {
//#if (framework == 'net9.0')
"version": "9.0.1",
"version": "9.0.2",
//#else
"version": "8.0.12",
//#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async Task<ProductDto> Update(ProductDto dto, CancellationToken cancellat

await DbContext.SaveChangesAsync(cancellationToken);

await responseCacheService.PurgeCache("/", $"/product/{dto.Id}/{Uri.EscapeDataString(dto.Name!)}", $"/api/ProductView/Get/{dto.Id}" /*You can also use Url.Action to build urls.*/);
await responseCacheService.PurgeCache("/", $"/product/{dto.ShortId}/{Uri.EscapeDataString(dto.Name!)}", $"/api/ProductView/Get/{dto.ShortId}" /*You can also use Url.Action to build urls.*/);

//#if (signalR == true)
await PublishDashboardDataChanged(cancellationToken);
Expand All @@ -99,7 +99,7 @@ public async Task Delete(Guid id, string concurrencyStamp, CancellationToken can

await DbContext.SaveChangesAsync(cancellationToken);

await responseCacheService.PurgeCache("/", $"/product/{entityToDelete.Id}/{Uri.EscapeDataString(entityToDelete.Name!)}", $"/api/ProductView/Get/{entityToDelete.Id}" /*You can also use Url.Action to build urls.*/);
await responseCacheService.PurgeCache("/", $"/product/{entityToDelete.ShortId}/{Uri.EscapeDataString(entityToDelete.Name!)}", $"/api/ProductView/Get/{entityToDelete.ShortId}" /*You can also use Url.Action to build urls.*/);

//#if (signalR == true)
await PublishDashboardDataChanged(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public IQueryable<ProductDto> Get()

[HttpGet("{id}")]
[AppResponseCache(SharedMaxAge = 3600 * 24 * 7, MaxAge = 60 * 5, UserAgnostic = true)]
public async Task<ProductDto> Get(Guid id, CancellationToken cancellationToken)
public async Task<ProductDto> Get(int id, CancellationToken cancellationToken)
{
var product = await Get().FirstOrDefaultAsync(t => t.Id == id, cancellationToken)
var product = await Get().FirstOrDefaultAsync(t => t.ShortId == id, cancellationToken)
?? throw new ResourceNotFoundException(Localizer[nameof(AppStrings.ProductCouldNotBeFound)]);

return product;
Expand All @@ -26,19 +26,24 @@ public async Task<ProductDto> Get(Guid id, CancellationToken cancellationToken)

// This method needs to be implemented based on the logic required in each business.
[EnableQuery, HttpGet("{id}"), AppResponseCache(MaxAge = 60 * 5, SharedMaxAge = 0, UserAgnostic = true)]
public IQueryable<ProductDto> GetSimilar(Guid id)
public IQueryable<ProductDto> GetSimilar(int id)
{
var similarProducts = Get()
.OrderBy(p => EF.Functions.Random())
.Where(p => p.Id != id);
.Where(p => p.ShortId != id);

return similarProducts;
}

[EnableQuery, HttpGet("{id}"), AppResponseCache(MaxAge = 60 * 5, SharedMaxAge = 0, UserAgnostic = true)]
public IQueryable<ProductDto> GetSiblings(Guid id)
public async Task<IQueryable<ProductDto>> GetSiblings(int id, CancellationToken cancellationToken)
{
var siblings = Get().Where(t => t.CategoryId == id);
var categoryId = await DbContext.Products
.Where(p => p.ShortId == id)
.Select(p => p.CategoryId)
.FirstOrDefaultAsync(cancellationToken);

var siblings = Get().Where(t => t.ShortId != id && t.CategoryId == categoryId);

return siblings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

//#if (IsInsideProjectTemplate == true)
/*
//#endif
//#if (database == "PostgreSQL" || database == "SqlServer")
modelBuilder.HasSequence<int>("ProductShortId")
.StartsAt(10_000)
.IncrementsBy(1);
//#endif
//#if (IsInsideProjectTemplate == true)
*/
//#endif

modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);

ConfigureIdentityTableNames(modelBuilder);
Expand Down
Loading
Loading