2022-10-15 00:07:59 +02:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Piwigo.Client.Contract;
|
|
|
|
|
|
|
|
namespace Piwigo.Client;
|
|
|
|
|
2022-10-16 23:00:03 +02:00
|
|
|
public class AlbumApi : IAlbumApi
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
|
|
|
private readonly IPiwigoContext _context;
|
2022-10-16 23:00:03 +02:00
|
|
|
private readonly ILogger<AlbumApi> _logger;
|
2022-10-15 00:07:59 +02:00
|
|
|
|
2022-10-16 23:00:03 +02:00
|
|
|
public AlbumApi(IPiwigoContext context, ILogger<AlbumApi> logger)
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
|
|
|
_context = context ?? throw new ArgumentNullException(nameof(context));
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:22:48 +02:00
|
|
|
public async Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default)
|
2022-10-16 23:53:17 +02:00
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
|
2022-10-17 23:22:48 +02:00
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.deleteRepresentative", formParams, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:54:38 +02:00
|
|
|
public async Task RefreshRepresentativeAsync(int albumId, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
|
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.refreshRepresentative", formParams, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SetRepresentativeAsync(int albumId, int imageId, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "image_id", imageId.ToString() } };
|
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.setRepresentative", formParams, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:56:45 +02:00
|
|
|
public async Task SetRankAsync(int albumId, int rank, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "rank", rank.ToString() } };
|
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.setRank", formParams, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:22:48 +02:00
|
|
|
public async Task<AlbumOrphans> CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
|
|
|
|
var response = await _context.PostAsync<PiwigoResponse<AlbumOrphans[]>>(_logger, "pwg.categories.calculateOrphans", formParams, cancellationToken);
|
2022-10-16 23:53:17 +02:00
|
|
|
// the API seems to only return one result but returns it as a list in json
|
|
|
|
return response.Result.First();
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:22:48 +02:00
|
|
|
public async Task DeleteAsync(int albumId, string apiToken, CancellationToken cancellationToken = default)
|
2022-10-17 00:16:30 +02:00
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } };
|
2022-10-17 23:22:48 +02:00
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams, cancellationToken);
|
2022-10-17 00:16:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:29:08 +02:00
|
|
|
public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null,
|
|
|
|
AlbumPosition? position = null, CancellationToken cancellationToken = default)
|
2022-10-16 22:58:38 +02:00
|
|
|
{
|
2022-10-21 21:29:08 +02:00
|
|
|
var statusValue = GetAlbumStatusValue(status);
|
2022-10-16 22:58:38 +02:00
|
|
|
|
|
|
|
var positionValue = position switch
|
|
|
|
{
|
2022-10-21 21:29:08 +02:00
|
|
|
AlbumPosition.First => "first",
|
|
|
|
AlbumPosition.Last => "last",
|
2022-10-16 22:58:38 +02:00
|
|
|
null => null,
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(position), position, null)
|
|
|
|
};
|
|
|
|
|
|
|
|
var formParams = new Dictionary<string, string> { { "name", name } };
|
|
|
|
formParams.AddIfValueNotNull("parent", parentId?.ToString()).AddIfValueNotNull("comment", comment).AddIfValueNotNull("visible", visible?.ToString())
|
2022-10-17 23:22:48 +02:00
|
|
|
.AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", commentable?.ToString()).AddIfValueNotNull("position", positionValue);
|
2022-10-16 22:58:38 +02:00
|
|
|
|
2022-10-17 23:22:48 +02:00
|
|
|
var response = await _context.PostAsync<PiwigoResponse<AlbumAdded>>(_logger, "pwg.categories.add", formParams, cancellationToken);
|
2022-10-16 22:58:38 +02:00
|
|
|
if (!response.Result.Id.HasValue)
|
|
|
|
{
|
|
|
|
throw new PiwigoException($"Could not create album {name}: {response.Result.Info}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Result.Id.Value;
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:22:53 +02:00
|
|
|
public async Task MoveAsync(int albumId, int parentAlbumId, string apiToken, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "parent", parentAlbumId.ToString() }, { "pwg_token", apiToken } };
|
|
|
|
await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.move", formParams, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:29:08 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-17 23:22:48 +02:00
|
|
|
public async Task<IReadOnlyCollection<Album>> GetAllAsync(CancellationToken cancellationToken = default)
|
2022-10-15 00:07:59 +02:00
|
|
|
{
|
2022-10-16 22:58:38 +02:00
|
|
|
var formParams = new Dictionary<string, string> { { "recursive", "true" } };
|
2022-10-17 23:22:48 +02:00
|
|
|
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams, cancellationToken);
|
2022-10-16 23:00:03 +02:00
|
|
|
return new ReadOnlyCollection<Album>(response.Result.Albums);
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|
2022-10-21 21:29:08 +02:00
|
|
|
|
|
|
|
private static string? GetAlbumStatusValue(AlbumStatus? status)
|
|
|
|
{
|
|
|
|
return status switch
|
|
|
|
{
|
|
|
|
AlbumStatus.Public => "public",
|
|
|
|
AlbumStatus.Private => "private",
|
|
|
|
null => null,
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
|
|
|
|
};
|
|
|
|
}
|
2022-10-15 00:07:59 +02:00
|
|
|
}
|