diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-05-17 10:48:25 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-17 10:48:25 -0500 |
| commit | daf5e72ad63109978eeef2d0c4377b942f6209cb (patch) | |
| tree | e8352bbd18330aa460f10f439f685020a532e763 /src/slskd/Search/API | |
| parent | 818fb3570a37fca031c3f1ec665cb6eda2619448 (diff) | |
| parent | c775d1415b393387b99c6e1ba108bb836d92ae2b (diff) | |
| download | slskd-daf5e72ad63109978eeef2d0c4377b942f6209cb.tar.gz | |
Merge pull request #467 from slskd/search-list
Redesign Search views
Diffstat (limited to 'src/slskd/Search/API')
| -rw-r--r-- | src/slskd/Search/API/Controllers/SearchesController.cs | 55 | ||||
| -rw-r--r-- | src/slskd/Search/API/Hubs/SearchHub.cs | 30 |
2 files changed, 76 insertions, 9 deletions
diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 5f740044..ae0f6570 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -129,7 +129,60 @@ namespace slskd.Search.API [Authorize] public async Task<IActionResult> GetAll() { - return Ok(await Searches.ListAsync()); + var searches = await Searches.ListAsync(); + return Ok(searches); + } + + /// <summary> + /// Stops the search corresponding to the specified <paramref name="id"/>. + /// </summary> + /// <param name="id">The unique id of the search.</param> + /// <response code="200">The search was stopped.</response> + /// <response code="304">The search was not in progress.</response> + /// <returns></returns> + [HttpPut("{id}")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(304)] + public async Task<IActionResult> Cancel([FromRoute] Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id); + + if (search == default) + { + return NotFound(); + } + + if (Searches.TryCancel(id)) + { + return Ok(); + } + + return StatusCode(304); + } + + /// <summary> + /// Deletes the search corresponding to the specified <paramref name="id"/>. + /// </summary> + /// <param name="id">The unique id of the search.</param> + /// <response code="204">The search was deleted.</response> + /// <response code="404">A search with the specified id could not be found.</response> + /// <returns></returns> + [HttpDelete("{id}")] + [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(404)] + public async Task<IActionResult> Delete([FromRoute] Guid id) + { + var search = await Searches.FindAsync(search => search.Id == id, includeResponses: true); + + if (search == default) + { + return NotFound(); + } + + await Searches.DeleteAsync(search); + return NoContent(); } } } diff --git a/src/slskd/Search/API/Hubs/SearchHub.cs b/src/slskd/Search/API/Hubs/SearchHub.cs index 86434669..aaf9a287 100644 --- a/src/slskd/Search/API/Hubs/SearchHub.cs +++ b/src/slskd/Search/API/Hubs/SearchHub.cs @@ -15,8 +15,6 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // </copyright> -using Microsoft.Extensions.Options; - namespace slskd.Search.API { using System; @@ -26,8 +24,11 @@ namespace slskd.Search.API public static class SearchHubMethods { + public static readonly string List = "LIST"; + public static readonly string Create = "CREATE"; public static readonly string Response = "RESPONSE"; public static readonly string Update = "UPDATE"; + public static readonly string Delete = "DELETE"; } /// <summary> @@ -57,6 +58,16 @@ namespace slskd.Search.API { return hub.Clients.All.SendAsync(SearchHubMethods.Response, new { searchId, response }); } + + public static Task BroadcastCreateAsync(this IHubContext<SearchHub> hub, Search search) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Create, search); + } + + public static Task BroadcastDeleteAsync(this IHubContext<SearchHub> hub, Search search) + { + return hub.Clients.All.SendAsync(SearchHubMethods.Delete, search); + } } /// <summary> @@ -66,14 +77,17 @@ namespace slskd.Search.API public class SearchHub : Hub { public SearchHub( - IStateMonitor<State> stateMonitor, - IOptionsMonitor<Options> optionsMonitor) + ISearchService searchService) { - StateMonitor = stateMonitor; - OptionsMonitor = optionsMonitor; + Searches = searchService; } - private IStateMonitor<State> StateMonitor { get; } - private IOptionsMonitor<Options> OptionsMonitor { get; } + private ISearchService Searches { get; } + + public override async Task OnConnectedAsync() + { + var searches = await Searches.ListAsync(); + await Clients.Caller.SendAsync(SearchHubMethods.List, searches); + } } }
\ No newline at end of file |
