mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-12 01:48:49 +00:00
changed the logic for product search and set up the integration test
This commit is contained in:
parent
a2b7f0cfd2
commit
77b14bf4c7
@ -1,10 +1,12 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using HotChocolate.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
||||||
|
|
||||||
namespace ShoppingAssistantApi.Api.Controllers;
|
namespace ShoppingAssistantApi.Api.Controllers;
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
public class ProductsSearchController : BaseController
|
public class ProductsSearchController : BaseController
|
||||||
{
|
{
|
||||||
private readonly IProductService _productService;
|
private readonly IProductService _productService;
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using MongoDB.Bson;
|
||||||
|
using ShoppingAssistantApi.Application.IRepositories;
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
||||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||||
@ -15,22 +17,23 @@ public class ProductService : IProductService
|
|||||||
private readonly IWishlistsService _wishlistsService;
|
private readonly IWishlistsService _wishlistsService;
|
||||||
|
|
||||||
private readonly IOpenAiService _openAiService;
|
private readonly IOpenAiService _openAiService;
|
||||||
|
|
||||||
|
|
||||||
public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService)
|
private readonly IMessagesRepository _messagesRepository;
|
||||||
|
|
||||||
|
public ProductService(IOpenAiService openAiService, IWishlistsService wishlistsService, IMessagesRepository messagesRepository)
|
||||||
{
|
{
|
||||||
_openAiService = openAiService;
|
_openAiService = openAiService;
|
||||||
_wishlistsService = wishlistsService;
|
_wishlistsService = wishlistsService;
|
||||||
|
_messagesRepository = messagesRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async IAsyncEnumerable<ServerSentEvent> SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken)
|
public async IAsyncEnumerable<ServerSentEvent> SearchProductAsync(string wishlistId, MessageCreateDto message, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var isFirstMessage = _wishlistsService
|
var isFirstMessage = await _messagesRepository.GetCountAsync(message=>message.WishlistId==ObjectId.Parse((wishlistId)), cancellationToken);
|
||||||
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result;
|
|
||||||
|
|
||||||
var chatRequest = new ChatCompletionRequest();
|
var chatRequest = new ChatCompletionRequest();
|
||||||
|
|
||||||
if (isFirstMessage==null)
|
if (isFirstMessage==0)
|
||||||
{
|
{
|
||||||
chatRequest = new ChatCompletionRequest
|
chatRequest = new ChatCompletionRequest
|
||||||
{
|
{
|
||||||
@ -38,7 +41,7 @@ public class ProductService : IProductService
|
|||||||
{
|
{
|
||||||
new OpenAiMessage
|
new OpenAiMessage
|
||||||
{
|
{
|
||||||
Role = OpenAiRole.System.ToString(),
|
Role = OpenAiRole.System.ToString().ToLower(),
|
||||||
Content = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed." +
|
Content = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed." +
|
||||||
"\nYou must return data with one of the prefixes:" +
|
"\nYou must return data with one of the prefixes:" +
|
||||||
"\n[Question] - return question" +
|
"\n[Question] - return question" +
|
||||||
@ -49,7 +52,7 @@ public class ProductService : IProductService
|
|||||||
|
|
||||||
new OpenAiMessage()
|
new OpenAiMessage()
|
||||||
{
|
{
|
||||||
Role = OpenAiRole.Assistant.ToString(),
|
Role = OpenAiRole.System.ToString().ToLower(),
|
||||||
Content = "What are you looking for?"
|
Content = "What are you looking for?"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -80,10 +83,10 @@ public class ProductService : IProductService
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isFirstMessage!=null)
|
if(isFirstMessage!=0)
|
||||||
{
|
{
|
||||||
var previousMessages = _wishlistsService
|
var previousMessages = _wishlistsService
|
||||||
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 1, cancellationToken).Result.Items.ToList();
|
.GetMessagesPageFromPersonalWishlistAsync(wishlistId, 1, 50, cancellationToken).Result.Items.ToList();
|
||||||
|
|
||||||
var messagesForOpenAI = new List<OpenAiMessage>();
|
var messagesForOpenAI = new List<OpenAiMessage>();
|
||||||
foreach (var item in previousMessages )
|
foreach (var item in previousMessages )
|
||||||
@ -91,7 +94,7 @@ public class ProductService : IProductService
|
|||||||
messagesForOpenAI.Add(
|
messagesForOpenAI.Add(
|
||||||
new OpenAiMessage()
|
new OpenAiMessage()
|
||||||
{
|
{
|
||||||
Role = item.Role,
|
Role = item.Role.ToLower(),
|
||||||
Content = item.Text
|
Content = item.Text
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -107,6 +107,8 @@ public class DbInitializer
|
|||||||
|
|
||||||
var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd");
|
var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd");
|
||||||
var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab");
|
var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab");
|
||||||
|
var wishlistId3 = ObjectId.Parse("ab7c8c2d9edf39abcd1ef9ab");
|
||||||
|
var wishlistId4 = ObjectId.Parse("ab8c8c2d9edf39abcd1ef9ab");
|
||||||
|
|
||||||
var wishlists = new Wishlist[]
|
var wishlists = new Wishlist[]
|
||||||
{
|
{
|
||||||
@ -125,7 +127,23 @@ public class DbInitializer
|
|||||||
Type = WishlistTypes.Product.ToString(),
|
Type = WishlistTypes.Product.ToString(),
|
||||||
CreatedById = user2.Id,
|
CreatedById = user2.Id,
|
||||||
CreatedDateUtc = DateTime.UtcNow
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
}
|
},
|
||||||
|
new Wishlist
|
||||||
|
{
|
||||||
|
Id = wishlistId3,
|
||||||
|
Name = "Test For Search",
|
||||||
|
Type = WishlistTypes.Product.ToString(),
|
||||||
|
CreatedById = user1.Id,
|
||||||
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
|
},
|
||||||
|
new Wishlist
|
||||||
|
{
|
||||||
|
Id = wishlistId4,
|
||||||
|
Name = "Test For Answer",
|
||||||
|
Type = WishlistTypes.Product.ToString(),
|
||||||
|
CreatedById = user1.Id,
|
||||||
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await wishlistsCollection.InsertManyAsync(wishlists);
|
await wishlistsCollection.InsertManyAsync(wishlists);
|
||||||
@ -142,6 +160,8 @@ public class DbInitializer
|
|||||||
|
|
||||||
var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd");
|
var wishlistId1 = ObjectId.Parse("ab79cde6f69abcd3efab65cd");
|
||||||
var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab");
|
var wishlistId2 = ObjectId.Parse("ab6c2c2d9edf39abcd1ef9ab");
|
||||||
|
var wishlistId3 = ObjectId.Parse("ab7c8c2d9edf39abcd1ef9ab");
|
||||||
|
var wishlistId4 = ObjectId.Parse("ab8c8c2d9edf39abcd1ef9ab");
|
||||||
|
|
||||||
var messages = new Message[]
|
var messages = new Message[]
|
||||||
{
|
{
|
||||||
@ -197,7 +217,28 @@ public class DbInitializer
|
|||||||
WishlistId = wishlistId2,
|
WishlistId = wishlistId2,
|
||||||
CreatedById = user2.Id,
|
CreatedById = user2.Id,
|
||||||
CreatedDateUtc = DateTime.UtcNow
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
}
|
},
|
||||||
|
new Message
|
||||||
|
{
|
||||||
|
Text = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed." +
|
||||||
|
"\nYou must return data with one of the prefixes:" +
|
||||||
|
"\n[Question] - return question" +
|
||||||
|
"\n[Suggestions] - return semicolon separated suggestion how to answer to a question" +
|
||||||
|
"\n[Message] - return text" +
|
||||||
|
"\n[Products] - return semicolon separated product names",
|
||||||
|
Role = "system",
|
||||||
|
WishlistId = wishlistId4,
|
||||||
|
CreatedById = user2.Id,
|
||||||
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
|
},
|
||||||
|
new Message
|
||||||
|
{
|
||||||
|
Text = "What are you looking for?",
|
||||||
|
Role = "system",
|
||||||
|
WishlistId = wishlistId4,
|
||||||
|
CreatedById = user2.Id,
|
||||||
|
CreatedDateUtc = DateTime.UtcNow
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
await messagesCollection.InsertManyAsync(messages);
|
await messagesCollection.InsertManyAsync(messages);
|
||||||
|
@ -18,12 +18,13 @@ public class ProductsTests : TestsBase
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task StreamDataToClient_ReturnsExpectedResponse()
|
public async Task StreamDataToClient_ReturnsExpectedResponse()
|
||||||
{
|
{
|
||||||
|
await LoginAsync("wishlists@gmail.com", "Yuiop12345");
|
||||||
// Arrange
|
// Arrange
|
||||||
var wishlistId = "ab79cde6f69abcd3efab65cd";
|
var wishlistId = "ab8c8c2d9edf39abcd1ef9ab";
|
||||||
var message = new MessageCreateDto { Text = "Your message text" };
|
var message = new MessageCreateDto { Text = "I want new powerful laptop" };
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var response = await _httpClient.PostAsJsonAsync($"http://127.0.0.1:5183//api/products/search/{wishlistId}", message);
|
var response = await _httpClient.PostAsJsonAsync($"http://127.0.0.1:5183/api/ProductsSearch/search/{wishlistId}", message);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using MongoDB.Bson;
|
using MongoDB.Bson;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using ShoppingAssistantApi.Application.IRepositories;
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
using ShoppingAssistantApi.Application.Models.CreateDtos;
|
||||||
using ShoppingAssistantApi.Application.Models.Dtos;
|
using ShoppingAssistantApi.Application.Models.Dtos;
|
||||||
@ -21,13 +22,16 @@ public class ProductTests
|
|||||||
|
|
||||||
private IProductService _productService;
|
private IProductService _productService;
|
||||||
|
|
||||||
public Mock<IWishlistsService> _wishListServiceMock;
|
private Mock<IWishlistsService> _wishListServiceMock;
|
||||||
|
|
||||||
public ProductTests()
|
private IMessagesRepository _messagesRepository;
|
||||||
|
|
||||||
|
public ProductTests(IMessagesRepository messagesRepository)
|
||||||
{
|
{
|
||||||
|
_messagesRepository = messagesRepository;
|
||||||
_openAiServiceMock = new Mock<IOpenAiService>();
|
_openAiServiceMock = new Mock<IOpenAiService>();
|
||||||
_wishListServiceMock = new Mock<IWishlistsService>();
|
_wishListServiceMock = new Mock<IWishlistsService>();
|
||||||
_productService = new ProductService(_openAiServiceMock.Object, _wishListServiceMock.Object);
|
_productService = new ProductService(_openAiServiceMock.Object, _wishListServiceMock.Object, _messagesRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@ -116,7 +120,7 @@ public class ProductTests
|
|||||||
var message = new MessageCreateDto { Text = "Your message text" };
|
var message = new MessageCreateDto { Text = "Your message text" };
|
||||||
var cancellationToken = new CancellationToken();
|
var cancellationToken = new CancellationToken();
|
||||||
|
|
||||||
var productService = new ProductService(_openAiServiceMock.Object, _wishListServiceMock.Object);
|
var productService = new ProductService(_openAiServiceMock.Object, _wishListServiceMock.Object, _messagesRepository);
|
||||||
|
|
||||||
var expectedSseData = new List<string>
|
var expectedSseData = new List<string>
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user