diff --git a/PiwigoDotnet/Piwigo.Client.Tests/CategoryApiTests.cs b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs similarity index 53% rename from PiwigoDotnet/Piwigo.Client.Tests/CategoryApiTests.cs rename to PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs index fdf1d4c..2d0cce9 100644 --- a/PiwigoDotnet/Piwigo.Client.Tests/CategoryApiTests.cs +++ b/PiwigoDotnet/Piwigo.Client.Tests/AlbumApiTests.cs @@ -4,27 +4,45 @@ using Piwigo.Client.Contract; namespace Piwigo.Client.Tests; [TestFixture] -public class CategoryApiTests : ApiTestsBase +public class AlbumApiTests : ApiTestsBase { - private CategoryApi _categoryApi = null!; + private AlbumApi _albumApi = null!; protected override void OnSetUp() { base.OnSetUp(); - _categoryApi = new CategoryApi(Context, new NullLogger()); + _albumApi = new AlbumApi(Context, new NullLogger()); } [Test] - public async Task GetAllCategories_should_return_all_existing_categories() + public async Task Add_should_create_album_and_return_id() { await LoginAsync(); - - var serverResponse = new PiwigoResponse + var serverResponse = new PiwigoResponse { - Result = new PiwigoCategoryList + Result = new AlbumAdded { - Categories = new List + Id = 1, Info = "Album added" + } + }; + + SetJsonResult(serverResponse); + var response = await _albumApi.AddAsync("UnittestMain2", null, "comment", true, CategoryStatus.Public, true, CategoryPosition.Last); + + response.Should().BeGreaterOrEqualTo(1); + } + + [Test] + public async Task GetAll_should_return_all_existing_albums() + { + await LoginAsync(); + + var serverResponse = new PiwigoResponse + { + Result = new AlbumList + { + Albums = new List { new() { Id = 1, Name = "UnitTestMain" }, new() { Id = 3, Name = "UnitTestSub2", IdUpperCat = 1 }, @@ -33,7 +51,7 @@ public class CategoryApiTests : ApiTestsBase } }; SetJsonResult(serverResponse); - var response = await _categoryApi.GetAllAsync(); + var response = await _albumApi.GetAllAsync(); response.Should().HaveCount(3); response.Should().SatisfyRespectively(c => diff --git a/PiwigoDotnet/Piwigo.Client/CategoryApi.cs b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs similarity index 65% rename from PiwigoDotnet/Piwigo.Client/CategoryApi.cs rename to PiwigoDotnet/Piwigo.Client/AlbumApi.cs index 4f88462..c0b2f16 100644 --- a/PiwigoDotnet/Piwigo.Client/CategoryApi.cs +++ b/PiwigoDotnet/Piwigo.Client/AlbumApi.cs @@ -4,35 +4,12 @@ using Piwigo.Client.Contract; namespace Piwigo.Client; -internal static class DictionaryExtensions -{ - public static IDictionary AddIfValueNotNull(this IDictionary dictionary, string key, string? value) - { - if (dictionary == null) - { - throw new ArgumentNullException(nameof(dictionary)); - } - - if (string.IsNullOrWhiteSpace(key)) - { - throw new ArgumentException("Value cannot be null or whitespace.", nameof(key)); - } - - if (value is not null) - { - dictionary.Add(key, value); - } - - return dictionary; - } -} - -public class CategoryApi : ICategoryApi +public class AlbumApi : IAlbumApi { private readonly IPiwigoContext _context; - private readonly ILogger _logger; + private readonly ILogger _logger; - public CategoryApi(IPiwigoContext context, ILogger logger) + public AlbumApi(IPiwigoContext context, ILogger logger) { _context = context ?? throw new ArgumentNullException(nameof(context)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); @@ -70,11 +47,11 @@ public class CategoryApi : ICategoryApi return response.Result.Id.Value; } - public async Task> GetAllAsync() + public async Task> GetAllAsync() { _logger.LogInformation("Getting all existing categories from server"); var formParams = new Dictionary { { "recursive", "true" } }; - var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams); - return new ReadOnlyCollection(response.Result.Categories); + var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams); + return new ReadOnlyCollection(response.Result.Albums); } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategory.cs b/PiwigoDotnet/Piwigo.Client/Contract/Album.cs similarity index 98% rename from PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategory.cs rename to PiwigoDotnet/Piwigo.Client/Contract/Album.cs index 842255a..574b0a2 100644 --- a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategory.cs +++ b/PiwigoDotnet/Piwigo.Client/Contract/Album.cs @@ -4,7 +4,7 @@ using Newtonsoft.Json; namespace Piwigo.Client.Contract; [SuppressMessage("ReSharper", "StringLiteralTypo")] -public class PiwigoCategory +public class Album { [JsonProperty("id")] public int Id { get; init; } diff --git a/PiwigoDotnet/Piwigo.Client/Contract/AlbumList.cs b/PiwigoDotnet/Piwigo.Client/Contract/AlbumList.cs new file mode 100644 index 0000000..7d12ac1 --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/Contract/AlbumList.cs @@ -0,0 +1,9 @@ +using Newtonsoft.Json; + +namespace Piwigo.Client.Contract; + +public class AlbumList +{ + [JsonProperty("Categories")] + public IList Albums { get; init; } = null!; +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategoryList.cs b/PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategoryList.cs deleted file mode 100644 index 86b8b16..0000000 --- a/PiwigoDotnet/Piwigo.Client/Contract/PiwigoCategoryList.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Newtonsoft.Json; - -namespace Piwigo.Client.Contract; - -public class PiwigoCategoryList -{ - [JsonProperty("Categories")] - public IList Categories { get; init; } = null!; -} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/DictionaryExtensions.cs b/PiwigoDotnet/Piwigo.Client/DictionaryExtensions.cs new file mode 100644 index 0000000..ead5786 --- /dev/null +++ b/PiwigoDotnet/Piwigo.Client/DictionaryExtensions.cs @@ -0,0 +1,24 @@ +namespace Piwigo.Client; + +internal static class DictionaryExtensions +{ + public static IDictionary AddIfValueNotNull(this IDictionary dictionary, string key, string? value) + { + if (dictionary == null) + { + throw new ArgumentNullException(nameof(dictionary)); + } + + if (string.IsNullOrWhiteSpace(key)) + { + throw new ArgumentException("Value cannot be null or whitespace.", nameof(key)); + } + + if (value is not null) + { + dictionary.Add(key, value); + } + + return dictionary; + } +} \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/ICategoryApi.cs b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs similarity index 74% rename from PiwigoDotnet/Piwigo.Client/ICategoryApi.cs rename to PiwigoDotnet/Piwigo.Client/IAlbumApi.cs index a6a0b42..6d44a56 100644 --- a/PiwigoDotnet/Piwigo.Client/ICategoryApi.cs +++ b/PiwigoDotnet/Piwigo.Client/IAlbumApi.cs @@ -2,10 +2,10 @@ using Piwigo.Client.Contract; namespace Piwigo.Client; -public interface ICategoryApi +public interface IAlbumApi { Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, CategoryPosition? position = null); - Task> GetAllAsync(); + Task> GetAllAsync(); } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs b/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs index a1e6176..83ef54e 100644 --- a/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs +++ b/PiwigoDotnet/Piwigo.Client/IPiwigoClient.cs @@ -8,5 +8,5 @@ public interface IPiwigoClient ITagApi Tag { get; } IUserApi User { get; } ISessionApi Session { get; } - ICategoryApi Category { get; } + IAlbumApi Album { get; } } \ No newline at end of file diff --git a/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs b/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs index acea905..aba1d3f 100644 --- a/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs +++ b/PiwigoDotnet/Piwigo.Client/PiwigoClient.cs @@ -2,7 +2,7 @@ namespace Piwigo.Client; public class PiwigoClient : IPiwigoClient { - public PiwigoClient(IGroupApi group, IImageApi image, IPermissionApi permission, ITagApi tag, IUserApi user, ISessionApi session, ICategoryApi category) + public PiwigoClient(IGroupApi group, IImageApi image, IPermissionApi permission, ITagApi tag, IUserApi user, ISessionApi session, IAlbumApi album) { Group = group ?? throw new ArgumentNullException(nameof(group)); Image = image ?? throw new ArgumentNullException(nameof(image)); @@ -10,7 +10,7 @@ public class PiwigoClient : IPiwigoClient Tag = tag ?? throw new ArgumentNullException(nameof(tag)); User = user ?? throw new ArgumentNullException(nameof(user)); Session = session ?? throw new ArgumentNullException(nameof(session)); - Category = category ?? throw new ArgumentNullException(nameof(category)); + Album = album ?? throw new ArgumentNullException(nameof(album)); } public IGroupApi Group { get; } @@ -19,5 +19,5 @@ public class PiwigoClient : IPiwigoClient public ITagApi Tag { get; } public IUserApi User { get; } public ISessionApi Session { get; } - public ICategoryApi Category { get; } + public IAlbumApi Album { get; } } \ No newline at end of file