piwigodotnet/PiwigoDotnet/Piwigo.Client/AlbumApi.cs

70 lines
3.1 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using Microsoft.Extensions.Logging;
using Piwigo.Client.Contract;
namespace Piwigo.Client;
public class AlbumApi : IAlbumApi
{
private readonly IPiwigoContext _context;
private readonly ILogger<AlbumApi> _logger;
public AlbumApi(IPiwigoContext context, ILogger<AlbumApi> logger)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<AlbumOrphans> CalculateOrphansAsync(int albumId)
{
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() } };
var response = await _context.PostAsync<PiwigoResponse<AlbumOrphans[]>>(_logger, "pwg.categories.calculateOrphans", formParams);
// the API seems to only return one result but returns it as a list in json
return response.Result.First();
}
public async Task DeleteAsync(int albumId, string apiToken)
{
var formParams = new Dictionary<string, string> { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } };
var response = await _context.PostAsync<PiwigoResponse>(_logger, "pwg.categories.delete", formParams);
}
public async Task<int> AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, CategoryStatus? status = null, bool? commentable = null,
CategoryPosition? position = null)
{
var statusValue = status switch
{
CategoryStatus.Public => "public",
CategoryStatus.Private => "private",
null => null,
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
};
var positionValue = position switch
{
CategoryPosition.First => "first",
CategoryPosition.Last => "last",
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())
.AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", visible?.ToString()).AddIfValueNotNull("position", positionValue);
var response = await _context.PostAsync<PiwigoResponse<AlbumAdded>>(_logger, "pwg.categories.add", formParams);
if (!response.Result.Id.HasValue)
{
throw new PiwigoException($"Could not create album {name}: {response.Result.Info}");
}
return response.Result.Id.Value;
}
public async Task<IReadOnlyCollection<Album>> GetAllAsync()
{
var formParams = new Dictionary<string, string> { { "recursive", "true" } };
var response = await _context.PostAsync<PiwigoResponse<AlbumList>>(_logger, "pwg.categories.getList", formParams);
return new ReadOnlyCollection<Album>(response.Result.Albums);
}
}