aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/API/Controllers/ServerController.cs
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-02-13 07:58:15 -0600
committerJP Dillingham <jpdillingham@gmail.com>2021-02-13 07:58:15 -0600
commit936ed099a6e5258f9fd5a5ac026afc0630359b98 (patch)
treef406e1f1b9fc29391e471c65b708ba6d94b72027 /src/slskd/API/Controllers/ServerController.cs
parent87f024fa2561f751fc26c129bfaa373a966f1c67 (diff)
downloadslskd-936ed099a6e5258f9fd5a5ac026afc0630359b98.tar.gz
move controllers and DTOs to API namespace
Diffstat (limited to 'src/slskd/API/Controllers/ServerController.cs')
-rw-r--r--src/slskd/API/Controllers/ServerController.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/slskd/API/Controllers/ServerController.cs b/src/slskd/API/Controllers/ServerController.cs
new file mode 100644
index 00000000..fb194974
--- /dev/null
+++ b/src/slskd/API/Controllers/ServerController.cs
@@ -0,0 +1,68 @@
+namespace slskd.API.Controllers
+{
+ using Microsoft.AspNetCore.Authorization;
+ using Microsoft.AspNetCore.Mvc;
+ using Soulseek;
+ using System.Threading.Tasks;
+ using slskd.API.DTO;
+
+ /// <summary>
+ /// Server
+ /// </summary>
+ [Route("api/v{version:apiVersion}/[controller]")]
+ [ApiVersion("0")]
+ [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.Username, req.Password);
+ return Ok();
+ }
+
+ return BadRequest("Provide one of the following: address and port, username and password, or address, port, username and password");
+ }
+ }
+}