diff --git a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs index 8641ca3..3dcb51e 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/ImageApiTests.cs @@ -15,6 +15,22 @@ public class ImageApiTests : ApiTestsBase _imageApi = new ImageApi(Context, new NullLogger()); } + [Test] + public async Task AddComment_should_pass_data_to_piwigo_and_return_new_comment() + { + SetJsonResult(@"{""stat"":""ok"",""result"":{""comment"":{""id"":2,""validation"":true}}}"); + + var commentId = await _imageApi.AddCommentAsync(4, "admin", "test comment", "testKey"); + + CorrectMethodShouldGetCalled("pwg.images.addComment"); + CorrectParamShouldGetSent("image_id", "4"); + CorrectParamShouldGetSent("author", "admin"); + CorrectParamShouldGetSent("content", "test comment"); + CorrectParamShouldGetSent("key", "testKey"); + + commentId.Should().Be(2); + } + [Test] public async Task GetInfo_should_pass_request_and_return_data() { diff --git a/PiwigoDotnet/Piwigo.Client/Contract/CommentAdded.cs b/PiwigoDotnet/Piwigo.Client/Contract/CommentAdded.cs new file mode 100644 index 0000000..8d7c83d --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/Contract/CommentAdded.cs @@ -0,0 +1,9 @@ +using Newtonsoft.Json; + +namespace Piwigo.Client.Contract; + +public record CommentAdded +{ + [JsonProperty("comment")] + public Comment Comment { get; init; } = null!; +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IImageApi.cs b/PiwigoDotnet/Piwigo.Client/IImageApi.cs index 86f73e1..dfbfc06 100644 --- a/PiwigoDotnet/Piwigo.Client/IImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IImageApi.cs @@ -6,6 +6,7 @@ public interface IImageApi { Task AddAsync(ImageUpload imageUpload, CancellationToken cancellationToken = default); Task UpdateAsync(int imageId, ImageUpload imageUpload, CancellationToken cancellationToken = default); + Task AddCommentAsync(int imageId, string? author, string content, string key, CancellationToken cancellationToken = default); Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default); Task ReadyForUploadAsync(CancellationToken cancellationToken = default); Task GetInfoAsync(int imageId, int? commentsPage, int? commentsPerPage, CancellationToken cancellationToken = default); @@ -14,7 +15,6 @@ public interface IImageApi CancellationToken cancellationToken = default); /* -addComment checkFiles delete deleteOrphans diff --git a/PiwigoDotnet/Piwigo.Client/ImageApi.cs b/PiwigoDotnet/Piwigo.Client/ImageApi.cs index 0131e46..900b1cc 100644 --- a/PiwigoDotnet/Piwigo.Client/ImageApi.cs +++ b/PiwigoDotnet/Piwigo.Client/ImageApi.cs @@ -18,6 +18,21 @@ public class ImageApi : IImageApi _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } + public async Task AddCommentAsync(int imageId, string? author, string content, string key, CancellationToken cancellationToken = default) + { + var formParams = new Dictionary + { + { "image_id", imageId.ToString() }, + { "content", content }, + { "key", key } + }; + formParams.AddIfValueNotNull("author", author); + + var response = await _context.PostAsync>(_logger, "pwg.images.addComment", formParams, cancellationToken); + + return response.Result.Comment.Id; + } + public async Task AddChunkAsync(byte[] data, string originalSum, int position, CancellationToken cancellationToken = default) { var base64Data = Convert.ToBase64String(data);