adds delete and duplicate of a tag

This commit is contained in:
Philipp Häfelfinger 2022-10-28 00:00:52 +02:00
parent e29c1c7c03
commit e68addbdaa
6 changed files with 87 additions and 4 deletions

View File

@ -56,6 +56,9 @@
<None Update="TagApiTests.GetListAsync_should_return_all_tags.verified.txt">
<DependentUpon>TagApiTests.cs</DependentUpon>
</None>
<None Update="TagApiTests.Duplicate_should_pass_correct_parameters.verified.txt">
<DependentUpon>TagApiTests.cs</DependentUpon>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,5 @@
{
id: 4,
name: dup2,
url_name: dup2
}

View File

@ -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);
}
}

View File

@ -2,7 +2,7 @@ using Newtonsoft.Json;
namespace Piwigo.Client.Tags;
public record TagAdded
public record AddedTag
{
[JsonProperty("info")]
public string? Info { get; init; }

View File

@ -10,7 +10,30 @@ public interface ITagApi
/// <see cref="CancellationToken" />
/// </param>
/// <returns>Information about the uploaded tag</returns>
Task<TagAdded> AddAsync(string name, CancellationToken cancellationToken = default);
Task<AddedTag> AddAsync(string name, CancellationToken cancellationToken = default);
/// <summary>
/// Removes assignments and deletes the tag form the gallery
/// </summary>
/// <param name="tagId">the id of the tag</param>
/// <param name="apiToken">The API token that can be read from <see cref="Piwigo.Client.Session.SessionStatus" /></param>
/// <param name="cancellationToken">
/// <see cref="CancellationToken" />
/// </param>
/// <returns></returns>
Task DeleteAsync(int tagId, string apiToken, CancellationToken cancellationToken = default);
/// <summary>
/// Duplicates an existing tag and returns the information of the newly added tag
/// </summary>
/// <param name="tagId">tag id to duplicate</param>
/// <param name="targetName">the name of the new tag</param>
/// <param name="apiToken">The API token that can be read from <see cref="Piwigo.Client.Session.SessionStatus" /></param>
/// <param name="cancellationToken">
/// <see cref="CancellationToken" />
/// </param>
/// <returns>The information about the added tag</returns>
Task<AddedTag> DuplicateAsync(int tagId, string targetName, string apiToken, CancellationToken cancellationToken = default);
/// <summary>
/// Gets a list of all known tags. This call is only available for admins

View File

@ -13,13 +13,13 @@ public class TagApi : ITagApi
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<TagAdded> AddAsync(string name, CancellationToken cancellationToken = default)
public async Task<AddedTag> AddAsync(string name, CancellationToken cancellationToken = default)
{
var formParams = new Dictionary<string, string>
{
{ "name", name }
};
var response = await _context.PostAsync<PiwigoResponse<TagAdded>>(_logger, "pwg.tags.add", formParams, cancellationToken);
var response = await _context.PostAsync<PiwigoResponse<AddedTag>>(_logger, "pwg.tags.add", formParams, cancellationToken);
return response.Result;
}
@ -37,4 +37,29 @@ public class TagApi : ITagApi
var response = await _context.PostAsync<PiwigoResponse<TagList<AssignedTag>>>(_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<string, string>
{
{ "tag_id", tagId.ToString() },
{ "pwg_token", apiToken }
};
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.tags.delete", formParams, cancellationToken);
}
public async Task<AddedTag> DuplicateAsync(int tagId, string targetName, string apiToken, CancellationToken cancellationToken = default)
{
var formParams = new Dictionary<string, string>
{
{ "tag_id", tagId.ToString() },
{ "copy_name", targetName },
{ "pwg_token", apiToken }
};
var response = await _context.PostAsync<PiwigoResponse<AddedTag>>(_logger, "pwg.tags.duplicate", formParams, cancellationToken);
return response.Result;
}
}