diff --git a/sharp/content/domainservice/HistoryService.cs b/sharp/content/domainservice/HistoryService.cs index 570ecd2..fdb45f2 100644 --- a/sharp/content/domainservice/HistoryService.cs +++ b/sharp/content/domainservice/HistoryService.cs @@ -27,7 +27,7 @@ public async Task AddHistory(long userId, long videoId) public async Task> Recommendation(long userId, ulong page = 0, ulong size = 10) { var historyList = await history.GetHistorys(userId, 0, 0); - var positiveVectors = historyList.Count != 0 ? new ReadOnlyMemory(historyList.Select(x => new Vector(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray())).ToArray()) : null; + var positiveVectors = historyList.Count == 0 ? new Vector[] { new(Enumerable.Range(1, 1024).Select(_ => (float)random.NextDouble()).ToArray()) } : null; var result = await client.RecommendAsync("videos", positive: historyList.Select(x => (ulong)x).ToList(), /* history */ positiveVectors: positiveVectors, /* if empty positive point */ limit: size, offset: page * size diff --git a/sharp/content/endpoints/Endpoints.cs b/sharp/content/endpoints/Endpoints.cs index 7f21949..2e8d0c4 100644 --- a/sharp/content/endpoints/Endpoints.cs +++ b/sharp/content/endpoints/Endpoints.cs @@ -106,10 +106,8 @@ public static Task AddHistory(HistoryService service,ClaimsPrincipal user , AddV public static void MapEndpoints(this IEndpointRouteBuilder endpoints) { endpoints.MapGet("/users/{userId:long}/videos", UserVideos).WithName("getUserVideos"); - // endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes"); - endpoints.MapGet("/users/{userId:long}/likes", Likes).WithName("getUserLikes"); + endpoints.MapGet("/users/{userId:long}/likes", Likes).RequireAuthorization().WithName("getUserLikes"); endpoints.MapGet("/videos", Videos).WithName("getVideos"); - // todo: user id endpoints.MapGet("/videos/recommend", RecommendVideos).WithName("getRecommendVideos"); endpoints.MapGet("/videos/{id:long}", FindVideoById).WithName("getVideo"); endpoints.MapGet("/videos/{id:long}/similar", SimilarVideos).WithName("getSimilarVideos"); @@ -117,7 +115,6 @@ public static void MapEndpoints(this IEndpointRouteBuilder endpoints) endpoints.MapPost("/videos", CreateVideo).RequireAuthorization().WithName("createVideo"); endpoints.MapPost("/videos/history", AddHistory).RequireAuthorization().WithName("addVideoHistory"); endpoints.MapGet("/videos/history", HistoryVideos).RequireAuthorization().WithName("getVideoHistory"); - } diff --git a/sharp/content/repository/Client.cs b/sharp/content/repository/Client.cs index 5544846..60848b9 100644 --- a/sharp/content/repository/Client.cs +++ b/sharp/content/repository/Client.cs @@ -129,7 +129,8 @@ public async Task> VotedOfVideos(List videoIds) /// Scan voted videos, which means paging through all voted videos. public async Task<(long?, IReadOnlyList)> VotedVideos(long page, int size) { - using var req = new HttpRequestMessage(HttpMethod.Get, $"/graph/videos?page={page}&size={size}"); + var url = $"/graph/videos?page={page}&size={size}"; + using var req = new HttpRequestMessage(HttpMethod.Get, url); if (!string.IsNullOrEmpty(Token) && AuthenticationHeaderValue.TryParse(Token, out var auth)) { req.Headers.Authorization = auth; @@ -212,8 +213,8 @@ public static string GetConnString(this IServiceProvider sp, string name) => sp.GetRequiredService().ConnString(name); public static IServiceCollection AddVoteRepository(this IServiceCollection services) => services - .AddHttpClient("Vote", - (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("Vote"))).Services; + .AddScoped(sp => new VoteRepository(sp.GetRequiredService().CreateClient("Vote"))) + .AddHttpClient("Vote", (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("Vote"))).AddAsKeyed(ServiceLifetime.Scoped).Services; public static IServiceCollection AddSearchClient(this IServiceCollection services) => services .AddSingleton(sp => ConnectionMultiplexer.Connect(sp.GetRequiredService().GetConnectionString("Redis").EnsureNotNull("Redis connection string is null"))) @@ -225,9 +226,9 @@ public static IServiceCollection AddSearchClient(this IServiceCollection service client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); }).Services; - public static IServiceCollection AddUserRepository(this IServiceCollection services) => - services.AddHttpClient("User", - (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("User"))).Services; + public static IServiceCollection AddUserRepository(this IServiceCollection services) => services. + AddScoped(sp => new UserRepository(sp.GetRequiredService().CreateClient("User"))). + AddHttpClient("User", (sp, client) => client.BaseAddress = new Uri(sp.GetConnString("User"))).Services; public static IApplicationBuilder UseToken(this IApplicationBuilder app) => app.Use(async (context, next) =>