0
0
RhSolutions-Api/RhSolutions.Api/Services/SearchService.cs

49 lines
1.5 KiB
C#

using Grpc.Core;
using RhSolutions.Models;
using Microsoft.EntityFrameworkCore;
using RhSolutions.QueryModifiers;
namespace RhSolutions.Api.Services;
public class SearchService : ProductSearch.ProductSearchBase
{
private RhSolutionsContext _dbContext;
private IProductTypePredicter _typePredicter;
private ProductQueryModifierFactory _productQueryModifierFactory;
public SearchService(RhSolutionsContext dbContext, IProductTypePredicter typePredicter, ProductQueryModifierFactory productQueryModifierFactory)
{
_dbContext = dbContext;
_typePredicter = typePredicter;
_productQueryModifierFactory = productQueryModifierFactory;
}
public override async Task<ProductReply?> GetProduct(ProductRequest request, ServerCallContext context)
{
var productType = _typePredicter.GetPredictedProductType(request.Query);
var modifier = _productQueryModifierFactory.GetModifier(productType!);
string query = request.Query;
if (modifier.TryQueryModify(query, out var modified))
{
query = modified;
}
var product = await _dbContext.Products
.Where(p => EF.Functions.ToTsVector(
"russian", string.Join(' ', new[] { p.Name, string.Join(' ', p.ProductLines) }))
.Matches(EF.Functions.WebSearchToTsQuery("russian", query)))
.OrderByDescending(p => p.IsOnWarehouse)
.FirstOrDefaultAsync();
if (product != null)
{
return new ProductReply()
{
Id = product.Id,
Name = product.Name,
Price = (double)product.Price
};
}
return null;
}
}