renames category to album even if the api reference uses category. piwigo uses album everywhere and this seems more logical

This commit is contained in:
Philipp Häfelfinger 2022-10-16 23:00:03 +02:00
parent 62679b0c09
commit 37c262e74f
9 changed files with 73 additions and 54 deletions

View File

@ -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<CategoryApi>());
_albumApi = new AlbumApi(Context, new NullLogger<AlbumApi>());
}
[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<AlbumAdded>
{
Result = new AlbumAdded
{
Id = 1, Info = "Album added"
}
};
var serverResponse = new PiwigoResponse<PiwigoCategoryList>
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()
{
Result = new PiwigoCategoryList
await LoginAsync();
var serverResponse = new PiwigoResponse<AlbumList>
{
Categories = new List<PiwigoCategory>
Result = new AlbumList
{
Albums = new List<Album>
{
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 =>

View File

@ -4,35 +4,12 @@ using Piwigo.Client.Contract;
namespace Piwigo.Client;
internal static class DictionaryExtensions
{
public static IDictionary<string, string> AddIfValueNotNull(this IDictionary<string, string> 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<CategoryApi> _logger;
private readonly ILogger<AlbumApi> _logger;
public CategoryApi(IPiwigoContext context, ILogger<CategoryApi> logger)
public AlbumApi(IPiwigoContext context, ILogger<AlbumApi> 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<IReadOnlyCollection<PiwigoCategory>> GetAllAsync()
public async Task<IReadOnlyCollection<Album>> GetAllAsync()
{
_logger.LogInformation("Getting all existing categories from server");
var formParams = new Dictionary<string, string> { { "recursive", "true" } };
var response = await _context.PostAsync<PiwigoResponse<PiwigoCategoryList>>(_logger, "pwg.categories.getList", formParams);
return new ReadOnlyCollection<PiwigoCategory>(response.Result.Categories);
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams);
return new ReadOnlyCollection<Album>(response.Result.Albums);
}
}

View File

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

View File

@ -0,0 +1,9 @@
using Newtonsoft.Json;
namespace Piwigo.Client.Contract;
public class AlbumList
{
[JsonProperty("Categories")]
public IList<Album> Albums { get; init; } = null!;
}

View File

@ -1,9 +0,0 @@
using Newtonsoft.Json;
namespace Piwigo.Client.Contract;
public class PiwigoCategoryList
{
[JsonProperty("Categories")]
public IList<PiwigoCategory> Categories { get; init; } = null!;
}

View File

@ -0,0 +1,24 @@
namespace Piwigo.Client;
internal static class DictionaryExtensions
{
public static IDictionary<string, string> AddIfValueNotNull(this IDictionary<string, string> 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;
}
}

View File

@ -2,10 +2,10 @@ using Piwigo.Client.Contract;
namespace Piwigo.Client;
public interface ICategoryApi
public interface IAlbumApi
{
Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
CategoryPosition? position = null);
Task<IReadOnlyCollection<PiwigoCategory>> GetAllAsync();
Task<IReadOnlyCollection<Album>> GetAllAsync();
}

View File

@ -8,5 +8,5 @@ public interface IPiwigoClient
ITagApi Tag { get; }
IUserApi User { get; }
ISessionApi Session { get; }
ICategoryApi Category { get; }
IAlbumApi Album { get; }
}

View File

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