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 _logger; public AlbumApi(IPiwigoContext context, ILogger logger) { _context = context ?? throw new ArgumentNullException(nameof(context)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task DeleteRepresentativeAsync(int albumId, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() } }; await _context.PostAsync(_logger, "pwg.categories.deleteRepresentative", formParams, cancellationToken); } public async Task RefreshRepresentativeAsync(int albumId, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() } }; await _context.PostAsync(_logger, "pwg.categories.refreshRepresentative", formParams, cancellationToken); } public async Task SetRepresentativeAsync(int albumId, int imageId, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() }, { "image_id", imageId.ToString() } }; await _context.PostAsync(_logger, "pwg.categories.setRepresentative", formParams, cancellationToken); } public async Task SetRankAsync(int albumId, int rank, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() }, { "rank", rank.ToString() } }; await _context.PostAsync(_logger, "pwg.categories.setRank", formParams, cancellationToken); } public async Task CalculateOrphansAsync(int albumId, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() } }; var response = await _context.PostAsync>(_logger, "pwg.categories.calculateOrphans", formParams, cancellationToken); // 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, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() }, { "pwg_token", apiToken }, { "photo_deletion_mode", "delete_orphans" } }; await _context.PostAsync(_logger, "pwg.categories.delete", formParams, cancellationToken); } public async Task AddAsync(string name, int? parentId = null, string? comment = null, bool? visible = null, AlbumStatus? status = null, bool? commentable = null, AlbumPosition? position = null, CancellationToken cancellationToken = default) { var statusValue = GetAlbumStatusValue(status); var positionValue = GetPositionValue(position); var formParams = new Dictionary { { "name", name } }; formParams.AddIfValueNotNull("parent", parentId?.ToString()).AddIfValueNotNull("comment", comment).AddIfValueNotNull("visible", visible?.ToString()) .AddIfValueNotNull("status", statusValue).AddIfValueNotNull("commentable", commentable?.ToString()).AddIfValueNotNull("position", positionValue); var response = await _context.PostAsync>(_logger, "pwg.categories.add", formParams, cancellationToken); if (!response.Result.Id.HasValue) { throw new PiwigoException($"Could not create album {name}: {response.Result.Info}"); } return response.Result.Id.Value; } public async Task MoveAsync(int albumId, int parentAlbumId, string apiToken, CancellationToken cancellationToken = default) { var formParams = new Dictionary { { "category_id", albumId.ToString() }, { "parent", parentAlbumId.ToString() }, { "pwg_token", apiToken } }; await _context.PostAsync(_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 { { "category_id", albumId.ToString() }, { "name", name } }; formParams.AddIfValueNotNull("comment", comment).AddIfValueNotNull("status", statusValue); await _context.PostAsync(_logger, "pwg.categories.setInfo", formParams, cancellationToken); } public async Task> GetListAsync(int? albumId, bool? recursive, bool? fullName, ThumbnailSize? thumbnailSize, CancellationToken cancellationToken = default) { var thumbnailSizeValue = GetThumbnailSizeValue(thumbnailSize); var formParams = new Dictionary(); formParams.AddIfValueNotNull("cat_id", albumId?.ToString()).AddIfValueNotNull("recursive", recursive?.ToString()).AddIfValueNotNull("fullname", fullName?.ToString()) .AddIfValueNotNull("thumbnail_size", thumbnailSizeValue); var response = await _context.PostAsync>(_logger, "pwg.categories.getList", formParams, cancellationToken); return new ReadOnlyCollection(response.Result.Albums); } private static string? GetPositionValue(AlbumPosition? position) { return position switch { AlbumPosition.First => "first", AlbumPosition.Last => "last", null => null, _ => throw new ArgumentOutOfRangeException(nameof(position), position, null) }; } private static string? GetThumbnailSizeValue(ThumbnailSize? thumbnailSize) { return thumbnailSize switch { ThumbnailSize.Thumb => "thumb", ThumbnailSize.Square => "square", ThumbnailSize.Small => "small", ThumbnailSize.Medium => "medium", ThumbnailSize.Large => "large", ThumbnailSize.XxLarge => "xxlarge", null => null, _ => throw new ArgumentOutOfRangeException(nameof(thumbnailSize), thumbnailSize, null) }; } private static string? GetAlbumStatusValue(AlbumStatus? status) { return status switch { AlbumStatus.Public => "public", AlbumStatus.Private => "private", null => null, _ => throw new ArgumentOutOfRangeException(nameof(status), status, null) }; } }