renames category enums to album enums and adds setInfo to albumapi

This commit is contained in:
Philipp Häfelfinger 2022-10-21 21:29:08 +02:00
parent 381fc9a436
commit 007d1caacf
5 changed files with 50 additions and 22 deletions

View File

@ -78,6 +78,20 @@ public class AlbumApiTests : ApiTestsBase
CorrectParamShouldGetSent("pwg_token", "apiToken"); CorrectParamShouldGetSent("pwg_token", "apiToken");
} }
[Test]
public async Task SetInfoAsync_should_pass_all_infos_to_piwigo()
{
SetOkResult();
await _albumApi.SetInfoAsync(1, "albumName", "comment", AlbumStatus.Public);
CorrectMethodShouldGetCalled("pwg.categories.setInfo");
CorrectParamShouldGetSent("category_id", "1");
CorrectParamShouldGetSent("name", "albumName");
CorrectParamShouldGetSent("comment", "comment");
CorrectParamShouldGetSent("status", "public");
}
[Test] [Test]
public async Task CalculateOrphansAsync_should_return_correct_values() public async Task CalculateOrphansAsync_should_return_correct_values()
{ {
@ -113,7 +127,7 @@ public class AlbumApiTests : ApiTestsBase
}; };
SetJsonResult(serverResponse); SetJsonResult(serverResponse);
var response = await _albumApi.AddAsync("UnittestMain2", null, "comment", true, CategoryStatus.Public, true, CategoryPosition.Last); var response = await _albumApi.AddAsync("UnittestMain2", null, "comment", true, AlbumStatus.Public, true, AlbumPosition.Last);
CorrectMethodShouldGetCalled("pwg.categories.add"); CorrectMethodShouldGetCalled("pwg.categories.add");
CorrectParamShouldGetSent("name", "UnittestMain2"); CorrectParamShouldGetSent("name", "UnittestMain2");

View File

@ -53,21 +53,15 @@ public class AlbumApi : IAlbumApi
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams, cancellationToken); await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams, cancellationToken);
} }
public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null,
CategoryPosition? position = null, CancellationToken cancellationToken = default) AlbumPosition? position = null, CancellationToken cancellationToken = default)
{ {
var statusValue = status switch var statusValue = GetAlbumStatusValue(status);
{
CategoryStatus.Public => "public",
CategoryStatus.Private => "private",
null => null,
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
};
var positionValue = position switch var positionValue = position switch
{ {
CategoryPosition.First => "first", AlbumPosition.First => "first",
CategoryPosition.Last => "last", AlbumPosition.Last => "last",
null => null, null => null,
_ => throw new ArgumentOutOfRangeException(nameof(position), position, null) _ => throw new ArgumentOutOfRangeException(nameof(position), position, null)
}; };
@ -91,10 +85,35 @@ public class AlbumApi : IAlbumApi
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.move", formParams, cancellationToken); await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.move", formParams, cancellationToken);
} }
public async Task SetInfoAsync(int albumId, string name, string? comment, AlbumStatus? status, CancellationToken cancellationToken = default)
{
var statusValue = GetAlbumStatusValue(status);
var formParams = new Dictionary<string, string>
{
{ "category_id", albumId.ToString() },
{ "name", name }
};
formParams.AddIfValueNotNull("comment", comment).AddIfValueNotNull("status", statusValue);
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.setInfo", formParams, cancellationToken);
}
public async Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default) public async Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default)
{ {
var formParams = new Dictionary<string, string> { { "recursive", "true" } }; var formParams = new Dictionary<string, string> { { "recursive", "true" } };
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams, cancellationToken); var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams, cancellationToken);
return new ReadOnlyCollection<Album>(response.Result.Albums); return new ReadOnlyCollection<Album>(response.Result.Albums);
} }
private static string? GetAlbumStatusValue(AlbumStatus? status)
{
return status switch
{
AlbumStatus.Public => "public",
AlbumStatus.Private => "private",
null => null,
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
};
}
} }

View File

@ -1,6 +1,6 @@
namespace Piwigo.Client; namespace Piwigo.Client;
public enum CategoryPosition public enum AlbumPosition
{ {
First = 0, First = 0,
Last = 1 Last = 1

View File

@ -1,6 +1,6 @@
namespace Piwigo.Client; namespace Piwigo.Client;
public enum CategoryStatus public enum AlbumStatus
{ {
Public = 0, Public = 0,
Private = 1 Private = 1

View File

@ -5,20 +5,15 @@ namespace Piwigo.Client;
public interface IAlbumApi public interface IAlbumApi
{ {
Task<AlbumOrphans> CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default); Task<AlbumOrphans> CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default);
Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default); Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default);
Task MoveAsync(int albumId, int parentAlbumId, string apiToken, CancellationToken cancellationToken = default); Task MoveAsync(int albumId, int parentAlbumId, string apiToken, CancellationToken cancellationToken = default);
Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default); Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default);
Task RefreshRepresentativeAsync(int albumId, CancellationToken cancellationToken = default); Task RefreshRepresentativeAsync(int albumId, CancellationToken cancellationToken = default);
Task SetRepresentativeAsync(int albumId, int imageId, CancellationToken cancellationToken = default); Task SetRepresentativeAsync(int albumId, int imageId, CancellationToken cancellationToken = default);
Task SetRankAsync(int albumId, int rank, CancellationToken cancellationToken = default); Task SetRankAsync(int albumId, int rank, CancellationToken cancellationToken = default);
Task SetInfoAsync(int albumId, string name, string? comment, AlbumStatus? status, CancellationToken cancellationToken = default);
Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null, Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null,
CategoryPosition? position = null, CancellationToken cancellationToken = default); AlbumPosition? position = null, CancellationToken cancellationToken = default);
Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default); Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default);
} }