From 3c53e43bc9b91aafe93853417436bf761b2a076f Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 3 Nov 2022 09:24:23 -0500 Subject: add statistics to Share --- .../Shares/API/Controllers/SharesController.cs | 45 ++++------------------ src/slskd/Shares/API/DTO/SummarizedShare.cs | 31 --------------- src/slskd/Shares/Types/Share.cs | 14 ++++++- 3 files changed, 21 insertions(+), 69 deletions(-) delete mode 100644 src/slskd/Shares/API/DTO/SummarizedShare.cs (limited to 'src') diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 671e8992..e4f82848 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -53,29 +53,14 @@ namespace slskd.Shares.API /// [HttpGet("")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(IEnumerable), 200)] - public async Task List() + [ProducesResponseType(typeof(Dictionary>), 200)] + public IActionResult List() { - var shares = new List(); + var response = Shares.Hosts.ToDictionary( + keySelector: host => host.Name, + elementSelector: host => host.Shares); - foreach (var share in Shares.Hosts.SelectMany(host => host.Shares)) - { - var (directories, files) = await Shares.SummarizeShareAsync(share); - - shares.Add(new SummarizedShare() - { - Id = share.Id, - Alias = share.Alias, - IsExcluded = share.IsExcluded, - LocalPath = share.LocalPath, - Raw = share.Raw, - RemotePath = share.RemotePath, - Directories = directories, - Files = files, - }); - } - - return Ok(shares); + return Ok(response); } /// @@ -87,7 +72,7 @@ namespace slskd.Shares.API /// [HttpGet("{id}")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(SummarizedShare), 200)] + [ProducesResponseType(typeof(Share), 200)] [ProducesResponseType(404)] public async Task Get(string id) { @@ -98,21 +83,7 @@ namespace slskd.Shares.API return NotFound(); } - var (directories, files) = await Shares.SummarizeShareAsync(share); - - var summary = new SummarizedShare() - { - Id = share.Id, - Alias = share.Alias, - IsExcluded = share.IsExcluded, - LocalPath = share.LocalPath, - Raw = share.Raw, - RemotePath = share.RemotePath, - Directories = directories, - Files = files, - }; - - return Ok(summary); + return Ok(share); } /// diff --git a/src/slskd/Shares/API/DTO/SummarizedShare.cs b/src/slskd/Shares/API/DTO/SummarizedShare.cs deleted file mode 100644 index 4e296d1c..00000000 --- a/src/slskd/Shares/API/DTO/SummarizedShare.cs +++ /dev/null @@ -1,31 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Shares -{ - public record SummarizedShare - { - public string Id { get; init; } - public string Alias { get; init; } - public bool IsExcluded { get; init; } - public string LocalPath { get; init; } - public string Raw { get; init; } - public string RemotePath { get; init; } - public int Directories { get; init; } - public int Files { get; init; } - } -} \ No newline at end of file diff --git a/src/slskd/Shares/Types/Share.cs b/src/slskd/Shares/Types/Share.cs index 6a6d1f66..42a2e9e2 100644 --- a/src/slskd/Shares/Types/Share.cs +++ b/src/slskd/Shares/Types/Share.cs @@ -35,8 +35,10 @@ namespace slskd.Shares /// /// /// + /// + /// [JsonConstructor] - public Share (string id, string alias, bool isExcluded, string localPath, string raw, string remotePath) + public Share (string id, string alias, bool isExcluded, string localPath, string raw, string remotePath, int directories, int files) { Id = id; Alias = alias; @@ -44,6 +46,8 @@ namespace slskd.Shares LocalPath = localPath; Raw = raw; RemotePath = remotePath; + Directories = directories; + Files = files; } /// @@ -87,5 +91,13 @@ namespace slskd.Shares public string LocalPath { get; init; } public string Raw { get; init; } public string RemotePath { get; init; } + public int Directories { get; private set; } + public int Files { get; private set; } + + public void UpdateStatistics(int directories, int files) + { + Directories = directories; + Files = files; + } } } -- cgit v1.2.3