From 8f1068ac3135bcccf09374bc787ef22049b43461 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 30 Dec 2020 21:16:50 -0600 Subject: copy files from example --- src/slskd/Controllers/ServerController.cs | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/slskd/Controllers/ServerController.cs (limited to 'src/slskd/Controllers/ServerController.cs') 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; + + /// + /// Server + /// + [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; + } + + /// + /// Disconnects the client. + /// + /// + /// + [HttpDelete] + [Authorize] + public IActionResult Disconnect([FromBody]string message) + { + Client.Disconnect(message); + return NoContent(); + } + + /// + /// Connects the client. + /// + /// + /// + [HttpPost] + [Authorize] + public async Task 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"); + } + } +} -- cgit v1.2.3