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
7525183
commit 3598ea2
Showing
34 changed files
with
422 additions
and
162 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
6 changes: 6 additions & 0 deletions
6
src/RookieShop.ApiService/Endpoints/Products/Search.Request.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,6 @@ | ||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed record SearchProductRequest( | ||
string Context, | ||
int PageIndex, | ||
int PageSize); |
10 changes: 10 additions & 0 deletions
10
src/RookieShop.ApiService/Endpoints/Products/Search.Response.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,10 @@ | ||
using Ardalis.Result; | ||
using RookieShop.ApiService.ViewModels.Products; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed class SearchProductResponse | ||
{ | ||
public PagedInfo? PagedInfo { get; set; } | ||
public List<ProductVm>? Products { get; set; } = []; | ||
} |
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,43 @@ | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using RookieShop.ApiService.ViewModels.Products; | ||
using RookieShop.Application.Products.Queries.Search; | ||
using RookieShop.Infrastructure.Endpoints.Abstractions; | ||
using RookieShop.Infrastructure.RateLimiter; | ||
|
||
namespace RookieShop.ApiService.Endpoints.Products; | ||
|
||
public sealed class Search(ISender sender) : IEndpoint<Ok<SearchProductResponse>, SearchProductRequest> | ||
{ | ||
public void MapEndpoint(IEndpointRouteBuilder app) => | ||
app.MapGet("/products/search", | ||
async ( | ||
string context, | ||
int pageNumber = 1, | ||
int pageSize = 0) => | ||
await HandleAsync(new(context, pageNumber, pageSize))) | ||
.Produces<Ok<SearchProductResponse>>() | ||
.WithTags(nameof(Products)) | ||
.WithName("Search Products") | ||
.MapToApiVersion(new(1, 0)) | ||
.RequirePerIpRateLimit(); | ||
|
||
public async Task<Ok<SearchProductResponse>> HandleAsync(SearchProductRequest request, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
SearchProductQuery query = new( | ||
request.Context, | ||
request.PageIndex, | ||
request.PageSize); | ||
|
||
var result = await sender.Send(query, cancellationToken); | ||
|
||
SearchProductResponse response = new() | ||
{ | ||
PagedInfo = result.PagedInfo, | ||
Products = result.Value.ToProductVm() | ||
}; | ||
|
||
return TypedResults.Ok(response); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,4 +18,4 @@ | |
"Port": 587, | ||
"Email": "nguyenxuannhan.dev@gmail.com" | ||
} | ||
} | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
src/RookieShop.Application/Products/Queries/Search/SearchProductHandler.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,35 @@ | ||
using Ardalis.Result; | ||
using RookieShop.Application.Products.DTOs; | ||
using RookieShop.Domain.Entities.ProductAggregator; | ||
using RookieShop.Domain.Entities.ProductAggregator.Specifications; | ||
using RookieShop.Domain.SharedKernel; | ||
using RookieShop.Infrastructure.Ai.Embedded; | ||
using RookieShop.Infrastructure.Storage.Azurite; | ||
|
||
namespace RookieShop.Application.Products.Queries.Search; | ||
|
||
public sealed class SearchProductHandler( | ||
IAiService aiService, | ||
IReadRepository<Product> repository, | ||
IAzuriteService azuriteService) : IQueryHandler<SearchProductQuery, PagedResult<IEnumerable<ProductDto>>> | ||
{ | ||
public async Task<PagedResult<IEnumerable<ProductDto>>> Handle(SearchProductQuery request, | ||
CancellationToken cancellationToken) | ||
{ | ||
var vector = await aiService.GetEmbeddingAsync(request.Context, cancellationToken); | ||
|
||
ProductsFilterSpec spec = new(vector, request.PageIndex, request.PageSize); | ||
|
||
var products = await repository.ListAsync(spec, cancellationToken); | ||
|
||
products.ForEach(p => p.ImageName = azuriteService.GetFileUrl(p.ImageName)); | ||
|
||
var totalRecords = await repository.CountAsync(spec, cancellationToken); | ||
|
||
var totalPages = (int)Math.Ceiling(totalRecords / (double)request.PageSize); | ||
|
||
PagedInfo pagedInfo = new(request.PageIndex, request.PageSize, totalPages, totalRecords); | ||
|
||
return new(pagedInfo, products.ToProductDto()); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/RookieShop.Application/Products/Queries/Search/SearchProductQuery.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,10 @@ | ||
using Ardalis.Result; | ||
using RookieShop.Application.Products.DTOs; | ||
using RookieShop.Domain.SharedKernel; | ||
|
||
namespace RookieShop.Application.Products.Queries.Search; | ||
|
||
public sealed record SearchProductQuery( | ||
string Context, | ||
int PageIndex, | ||
int PageSize) : IQuery<PagedResult<IEnumerable<ProductDto>>>; |
13 changes: 13 additions & 0 deletions
13
src/RookieShop.Application/Products/Queries/Search/SearchProductValidator.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,13 @@ | ||
using FluentValidation; | ||
|
||
namespace RookieShop.Application.Products.Queries.Search; | ||
|
||
public sealed class SearchProductValidator : AbstractValidator<SearchProductQuery> | ||
{ | ||
public SearchProductValidator() | ||
{ | ||
RuleFor(x => x.Context).NotEmpty(); | ||
RuleFor(x => x.PageIndex).GreaterThanOrEqualTo(1); | ||
RuleFor(x => x.PageSize).GreaterThanOrEqualTo(0); | ||
} | ||
} |
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
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
Oops, something went wrong.