From 71d8c3d233458fde02d7b169380ad347aaf88342 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 20 Jan 2025 20:40:12 -0600 Subject: add a public Update() method, refactor to use it internally --- src/slskd/Search/SearchService.cs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'src/slskd/Search') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 809647b2..f91714f5 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -64,6 +64,15 @@ namespace slskd.Search /// The list of searches matching the specified expression, or all searches if no expression is specified. Task> ListAsync(Expression> expression = null); + /// + /// Updates the specified . + /// + /// + /// Round-trips the database; use accordingly. + /// + /// The search to update. + void Update(Search search); + /// /// Performs a search for the specified and . /// @@ -191,6 +200,17 @@ namespace slskd.Search .ToListAsync(); } + /// + /// Updates the specified . + /// + /// The search to update. + public void Update(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Update(search); + context.SaveChanges(); + } + /// /// Performs a search for the specified and . /// @@ -223,20 +243,13 @@ namespace slskd.Search List responses = new(); - void UpdateAndSaveChanges(Search search) - { - using var context = ContextFactory.CreateDbContext(); - context.Update(search); - context.SaveChanges(); - } - options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => { search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); }, responseReceived: (args) => rateLimiter.Invoke(() => { @@ -250,7 +263,7 @@ namespace slskd.Search // note that we're not actually doing anything with the response here, that's happening in the // response handler. we're only updating counts here. SearchHub.BroadcastUpdateAsync(search); - UpdateAndSaveChanges(search); + Update(search); })); var soulseekSearchTask = Client.SearchAsync( @@ -278,7 +291,7 @@ namespace slskd.Search search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - UpdateAndSaveChanges(search); + Update(search); // zero responses before broadcasting, as we don't want to blast this // data out over the SignalR socket -- cgit v1.2.3