From 007d1caacf7fd7b3aa1403e27701a422a03ac7b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Fri, 21 Oct 2022 21:29:08 +0200 Subject: [PATCH] renames category enums to album enums and adds setInfo to albumapi --- .../Piwigo.Client.Tests/AlbumApiTests.cs | 16 +++++++- PiwigoDotnet/Piwigo.Client/AlbumApi.cs | 41 ++++++++++++++----- .../{CategoryPosition.cs => AlbumPosition.cs} | 2 +- .../{CategoryStatus.cs => AlbumStatus.cs} | 2 +- PiwigoDotnet/Piwigo.Client/IAlbumApi.cs | 11 ++--- 5 files changed, 50 insertions(+), 22 deletions(-) rename PiwigoDotnet/Piwigo.Client/{CategoryPosition.cs => AlbumPosition.cs} (66%) rename PiwigoDotnet/Piwigo.Client/{CategoryStatus.cs => AlbumStatus.cs} (69%) diff --git a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs index 048f122..3bb2047 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs @@ -78,6 +78,20 @@ public class AlbumApiTests : ApiTestsBase CorrectParamShouldGetSent("pwg_token", "apiToken"); } + [Test] + public async Task SetInfoAsync_should_pass_all_infos_to_piwigo() + { + SetOkResult(); + await _albumApi.SetInfoAsync(1, "albumName", "comment", AlbumStatus.Public); + + CorrectMethodShouldGetCalled("pwg.categories.setInfo"); + CorrectParamShouldGetSent("category_id", "1"); + CorrectParamShouldGetSent("name", "albumName"); + CorrectParamShouldGetSent("comment", "comment"); + CorrectParamShouldGetSent("status", "public"); + } + + [Test] public async Task CalculateOrphansAsync_should_return_correct_values() { @@ -113,7 +127,7 @@ public class AlbumApiTests : ApiTestsBase }; SetJsonResult(serverResponse); - var response = await _albumApi.AddAsync("UnittestMain2", null, "comment", true, CategoryStatus.Public, true, CategoryPosition.Last); + var response = await _albumApi.AddAsync("UnittestMain2", null, "comment", true, AlbumStatus.Public, true, AlbumPosition.Last); CorrectMethodShouldGetCalled("pwg.categories.add"); CorrectParamShouldGetSent("name", "UnittestMain2"); diff --git a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs index 720476d..a766859 100644 --- a/PiwigoDotnet/Piwigo.Client/AlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs @@ -53,21 +53,15 @@ public class AlbumApi : IAlbumApi await _context.PostAsync(_logger, "pwg.categories.delete", formParams, cancellationToken); } - public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, - CategoryPosition? position = null, CancellationToken cancellationToken = default) + public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null, + AlbumPosition? position = null, CancellationToken cancellationToken = default) { - var statusValue = status switch - { - CategoryStatus.Public => "public", - CategoryStatus.Private => "private", - null => null, - _ => throw new ArgumentOutOfRangeException(nameof(status), status, null) - }; + var statusValue = GetAlbumStatusValue(status); var positionValue = position switch { - CategoryPosition.First => "first", - CategoryPosition.Last => "last", + AlbumPosition.First => "first", + AlbumPosition.Last => "last", null => null, _ => throw new ArgumentOutOfRangeException(nameof(position), position, null) }; @@ -91,10 +85,35 @@ public class AlbumApi : IAlbumApi await _context.PostAsync(_logger, "pwg.categories.move", formParams, cancellationToken); } + public async Task SetInfoAsync(int albumId, string name, string? comment, AlbumStatus? status, CancellationToken cancellationToken = default) + { + var statusValue = GetAlbumStatusValue(status); + + var formParams = new Dictionary + { + { "category_id", albumId.ToString() }, + { "name", name } + }; + + formParams.AddIfValueNotNull("comment", comment).AddIfValueNotNull("status", statusValue); + await _context.PostAsync(_logger, "pwg.categories.setInfo", formParams, cancellationToken); + } + public async Task> GetAllAsync(CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "recursive", "true" } }; var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams, cancellationToken); return new ReadOnlyCollection(response.Result.Albums); } + + private static string? GetAlbumStatusValue(AlbumStatus? status) + { + return status switch + { + AlbumStatus.Public => "public", + AlbumStatus.Private => "private", + null => null, + _ => throw new ArgumentOutOfRangeException(nameof(status), status, null) + }; + } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/CategoryPosition.cs b/PiwigoDotnet/Piwigo.Client/AlbumPosition.cs similarity index 66% rename from PiwigoDotnet/Piwigo.Client/CategoryPosition.cs rename to PiwigoDotnet/Piwigo.Client/AlbumPosition.cs index ace4bee..b65ecf1 100644 --- a/PiwigoDotnet/Piwigo.Client/CategoryPosition.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumPosition.cs @@ -1,6 +1,6 @@ namespace Piwigo.Client; -public enum CategoryPosition +public enum AlbumPosition { First = 0, Last = 1 diff --git a/PiwigoDotnet/Piwigo.Client/CategoryStatus.cs b/PiwigoDotnet/Piwigo.Client/AlbumStatus.cs similarity index 69% rename from PiwigoDotnet/Piwigo.Client/CategoryStatus.cs rename to PiwigoDotnet/Piwigo.Client/AlbumStatus.cs index b719acf..f82aa0f 100644 --- a/PiwigoDotnet/Piwigo.Client/CategoryStatus.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumStatus.cs @@ -1,6 +1,6 @@ namespace Piwigo.Client; -public enum CategoryStatus +public enum AlbumStatus { Public = 0, Private = 1 diff --git a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs index d594ea6..4e28c44 100644 --- a/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs @@ -5,20 +5,15 @@ namespace Piwigo.Client; public interface IAlbumApi { Task CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default); - Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default); Task MoveAsync(int albumId, int parentAlbumId, string apiToken, CancellationToken cancellationToken = default); - Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default); - Task RefreshRepresentativeAsync(int albumId, CancellationToken cancellationToken = default); - Task SetRepresentativeAsync(int albumId, int imageId, CancellationToken cancellationToken = default); - Task SetRankAsync(int albumId, int rank, CancellationToken cancellationToken = default); - - Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, - CategoryPosition? position = null, CancellationToken cancellationToken = default); + Task SetInfoAsync(int albumId, string name, string? comment, AlbumStatus? status, CancellationToken cancellationToken = default); + Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null, + AlbumPosition? position = null, CancellationToken cancellationToken = default); Task> GetAllAsync(CancellationToken cancellationToken = default); } \ No newline at end of file