From e68addbdaa68d4e155a637754ff479c2b1a19b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Fri, 28 Oct 2022 00:00:52 +0200 Subject: [PATCH] adds delete and duplicate of a tag --- .../Piwigo.Client.Tests.csproj | 3 ++ ...hould_pass_correct_parameters.verified.txt | 5 ++++ src/Piwigo.Client.Tests/TagApiTests.cs | 27 +++++++++++++++++ .../Tags/{TagAdded.cs => AddedTag.cs} | 2 +- src/Piwigo.Client/Tags/ITagApi.cs | 25 +++++++++++++++- src/Piwigo.Client/Tags/TagApi.cs | 29 +++++++++++++++++-- 6 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 src/Piwigo.Client.Tests/TagApiTests.Duplicate_should_pass_correct_parameters.verified.txt rename src/Piwigo.Client/Tags/{TagAdded.cs => AddedTag.cs} (93%) diff --git a/src/Piwigo.Client.Tests/Piwigo.Client.Tests.csproj b/src/Piwigo.Client.Tests/Piwigo.Client.Tests.csproj index 389510b..307113d 100644 --- a/src/Piwigo.Client.Tests/Piwigo.Client.Tests.csproj +++ b/src/Piwigo.Client.Tests/Piwigo.Client.Tests.csproj @@ -56,6 +56,9 @@ TagApiTests.cs + + TagApiTests.cs + diff --git a/src/Piwigo.Client.Tests/TagApiTests.Duplicate_should_pass_correct_parameters.verified.txt b/src/Piwigo.Client.Tests/TagApiTests.Duplicate_should_pass_correct_parameters.verified.txt new file mode 100644 index 0000000..50623ee --- /dev/null +++ b/src/Piwigo.Client.Tests/TagApiTests.Duplicate_should_pass_correct_parameters.verified.txt @@ -0,0 +1,5 @@ +{ + id: 4, + name: dup2, + url_name: dup2 +} \ No newline at end of file diff --git a/src/Piwigo.Client.Tests/TagApiTests.cs b/src/Piwigo.Client.Tests/TagApiTests.cs index a054d90..5a4c070 100644 --- a/src/Piwigo.Client.Tests/TagApiTests.cs +++ b/src/Piwigo.Client.Tests/TagApiTests.cs @@ -51,4 +51,31 @@ public class TagApiTests : ApiTestsBase await Verify(response); } + + [Test] + public async Task DeleteAsync_should_pass_correct_parameters() + { + SetOkResult(); + + await _tagApi.DeleteAsync(123, ApiToken); + + CorrectMethodShouldGetCalled("pwg.tags.delete"); + CorrectParamShouldGetSent("tag_id", "123"); + CorrectParamShouldGetSent("pwg_token", ApiToken); + } + + [Test] + public async Task DuplicateAsync_should_pass_correct_parameters() + { + SetJsonResult(@"{""stat"":""ok"",""result"":{""id"":4,""name"":""dup2"",""url_name"":""dup2"",""count"":1}}"); + + var response = await _tagApi.DuplicateAsync(123, "newTag", ApiToken); + + CorrectMethodShouldGetCalled("pwg.tags.duplicate"); + CorrectParamShouldGetSent("tag_id", "123"); + CorrectParamShouldGetSent("copy_name", "newTag"); + CorrectParamShouldGetSent("pwg_token", ApiToken); + + await Verify(response); + } } \ No newline at end of file diff --git a/src/Piwigo.Client/Tags/TagAdded.cs b/src/Piwigo.Client/Tags/AddedTag.cs similarity index 93% rename from src/Piwigo.Client/Tags/TagAdded.cs rename to src/Piwigo.Client/Tags/AddedTag.cs index 27cf591..cfb11d2 100644 --- a/src/Piwigo.Client/Tags/TagAdded.cs +++ b/src/Piwigo.Client/Tags/AddedTag.cs @@ -2,7 +2,7 @@ using Newtonsoft.Json; namespace Piwigo.Client.Tags; -public record TagAdded +public record AddedTag { [JsonProperty("info")] public string? Info { get; init; } diff --git a/src/Piwigo.Client/Tags/ITagApi.cs b/src/Piwigo.Client/Tags/ITagApi.cs index 2aa1ca6..33bf446 100644 --- a/src/Piwigo.Client/Tags/ITagApi.cs +++ b/src/Piwigo.Client/Tags/ITagApi.cs @@ -10,7 +10,30 @@ public interface ITagApi /// /// /// Information about the uploaded tag - Task AddAsync(string name, CancellationToken cancellationToken = default); + Task AddAsync(string name, CancellationToken cancellationToken = default); + + /// + /// Removes assignments and deletes the tag form the gallery + /// + /// the id of the tag + /// The API token that can be read from + /// + /// + /// + /// + Task DeleteAsync(int tagId, string apiToken, CancellationToken cancellationToken = default); + + /// + /// Duplicates an existing tag and returns the information of the newly added tag + /// + /// tag id to duplicate + /// the name of the new tag + /// The API token that can be read from + /// + /// + /// + /// The information about the added tag + Task DuplicateAsync(int tagId, string targetName, string apiToken, CancellationToken cancellationToken = default); /// /// Gets a list of all known tags. This call is only available for admins diff --git a/src/Piwigo.Client/Tags/TagApi.cs b/src/Piwigo.Client/Tags/TagApi.cs index 1b8385b..f027209 100644 --- a/src/Piwigo.Client/Tags/TagApi.cs +++ b/src/Piwigo.Client/Tags/TagApi.cs @@ -13,13 +13,13 @@ public class TagApi : ITagApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } - public async Task AddAsync(string name, CancellationToken cancellationToken = default) + public async Task AddAsync(string name, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "name", name } }; - var response = await _context.PostAsync>(_logger, "pwg.tags.add", formParams, cancellationToken); + var response = await _context.PostAsync>(_logger, "pwg.tags.add", formParams, cancellationToken); return response.Result; } @@ -37,4 +37,29 @@ public class TagApi : ITagApi var response = await _context.PostAsync>>(_logger, "pwg.tags.getList", formParams, cancellationToken); return response.Result.Tags; } + + public async Task DeleteAsync(int tagId, string apiToken, CancellationToken cancellationToken = default) + { + var formParams = new Dictionary + { + { "tag_id", tagId.ToString() }, + { "pwg_token", apiToken } + }; + + await _context.PostAsync(_logger, "pwg.tags.delete", formParams, cancellationToken); + } + + public async Task DuplicateAsync(int tagId, string targetName, string apiToken, CancellationToken cancellationToken = default) + { + var formParams = new Dictionary + { + { "tag_id", tagId.ToString() }, + { "copy_name", targetName }, + { "pwg_token", apiToken } + }; + + var response = await _context.PostAsync>(_logger, "pwg.tags.duplicate", formParams, cancellationToken); + + return response.Result; + } } \ No newline at end of file