diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-24 09:42:34 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-24 09:42:34 -0500 |
| commit | d0ae5db8e0ebb989c6470c3b8f1c1793dafb1d8f (patch) | |
| tree | be15aee66b361efe6e6d2c97db59236be4c0202f /src/slskd/Application/Management/API/Controllers/ServerController.cs | |
| parent | 73f5c8a5cbe56745156d24b6c744cbe24567c23f (diff) | |
| parent | 7383d448e7481ca5dc046b8118e5709ab571b69f (diff) | |
| download | slskd-d0ae5db8e0ebb989c6470c3b8f1c1793dafb1d8f.tar.gz | |
Merge pull request #102 from slskd/connections
Add the ability to connect and disconnect the client from the API/UI
Diffstat (limited to 'src/slskd/Application/Management/API/Controllers/ServerController.cs')
| -rw-r--r-- | src/slskd/Application/Management/API/Controllers/ServerController.cs | 58 |
1 files changed, 35 insertions, 23 deletions
diff --git a/src/slskd/Application/Management/API/Controllers/ServerController.cs b/src/slskd/Application/Management/API/Controllers/ServerController.cs index ed911a13..6468063c 100644 --- a/src/slskd/Application/Management/API/Controllers/ServerController.cs +++ b/src/slskd/Application/Management/API/Controllers/ServerController.cs @@ -20,7 +20,6 @@ namespace slskd.Management.API using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; - using Soulseek; /// <summary> /// Server. @@ -32,40 +31,31 @@ namespace slskd.Management.API [Consumes("application/json")] public class ServerController : ControllerBase { - public ServerController(ISoulseekClient client) + public ServerController( + IManagementService managementService) { - Client = client; + Management = managementService; } - private ISoulseekClient Client { get; } + private IManagementService Management { get; } /// <summary> /// Connects the client. /// </summary> - /// <param name="req"></param> /// <returns></returns> - [HttpPost] + [HttpPut] + [Route("")] [Authorize] - public async Task<IActionResult> Connect([FromBody] ConnectRequest req) + [ProducesResponseType(200)] + [ProducesResponseType(403)] + public async Task<IActionResult> Connect() { - var addr = !string.IsNullOrEmpty(req.Address); - var port = req.Port.HasValue; - var un = !string.IsNullOrEmpty(req.Username); - var pw = !string.IsNullOrEmpty(req.Password); - - if (addr && port && un && pw) - { - await Client.ConnectAsync(req.Address, req.Port.Value, req.Username, req.Password); - return Ok(); - } - - if (!addr && !port && un && pw) + if (!Management.GetServerState().IsConnected) { - await Client.ConnectAsync(req.Username, req.Password); - return Ok(); + await Management.ConnectServerAsync(); } - return BadRequest("Provide one of the following: address and port, username and password, or address, port, username and password"); + return Ok(); } /// <summary> @@ -74,11 +64,33 @@ namespace slskd.Management.API /// <param name="message"></param> /// <returns></returns> [HttpDelete] + [Route("")] [Authorize] + [ProducesResponseType(204)] + [ProducesResponseType(403)] public IActionResult Disconnect([FromBody] string message) { - Client.Disconnect(message); + if (Management.GetServerState().IsConnected) + { + Management.DisconnectServer(message); + } + return NoContent(); } + + /// <summary> + /// Retrieves the current state of the server. + /// </summary> + /// <returns></returns> + /// <response code="200"></response> + [HttpGet] + [Route("")] + [Authorize] + [ProducesResponseType(typeof(ServerState), 200)] + [ProducesResponseType(403)] + public IActionResult Get() + { + return Ok(Management.GetServerState()); + } } }
\ No newline at end of file |
