aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Controllers/ServerController.cs
blob: c2393dcb7aa45855fb7ead9fad24d9a2edae5188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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");
        }
    }
}