diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2020-12-30 21:16:50 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2020-12-30 21:16:50 -0600 |
| commit | 8f1068ac3135bcccf09374bc787ef22049b43461 (patch) | |
| tree | 5d80a254f5ee7b7c6b3956f6a7307591bc5a93e2 /src/slskd/Controllers/ServerController.cs | |
| parent | ec4a85e4e8b64a0ae91c494e3c8ef520f0a8b8c3 (diff) | |
| download | slskd-8f1068ac3135bcccf09374bc787ef22049b43461.tar.gz | |
copy files from example
Diffstat (limited to 'src/slskd/Controllers/ServerController.cs')
| -rw-r--r-- | src/slskd/Controllers/ServerController.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/slskd/Controllers/ServerController.cs b/src/slskd/Controllers/ServerController.cs new file mode 100644 index 00000000..c2393dcb --- /dev/null +++ b/src/slskd/Controllers/ServerController.cs @@ -0,0 +1,74 @@ +namespace slskd.Controllers +{ + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + using Soulseek; + using System.Threading.Tasks; + using slskd.DTO; + + /// <summary> + /// Server + /// </summary> + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("1")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class ServerController : ControllerBase + { + private ISoulseekClient Client { get; } + + public ServerController(ISoulseekClient client) + { + Client = client; + } + + /// <summary> + /// Disconnects the client. + /// </summary> + /// <param name="message"></param> + /// <returns></returns> + [HttpDelete] + [Authorize] + public IActionResult Disconnect([FromBody]string message) + { + Client.Disconnect(message); + return NoContent(); + } + + /// <summary> + /// Connects the client. + /// </summary> + /// <param name="req"></param> + /// <returns></returns> + [HttpPost] + [Authorize] + public async Task<IActionResult> Connect([FromBody]ConnectRequest req) + { + 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) + { + await Client.ConnectAsync(req.Address, req.Port.Value); + return Ok(); + } + + if (!addr && !port && un && pw) + { + await Client.ConnectAsync(req.Username, req.Password); + return Ok(); + } + + return BadRequest("Provide one of the following: address and port, username and password, or address, port, username and password"); + } + } +} |
