45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using Harmonia.Core.Data;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Harmonia.Core.Playlists;
|
|
|
|
public class PlaylistRepository : JsonFileRepository<Playlist>, IPlaylistRepository
|
|
{
|
|
private static readonly JsonSerializerOptions _options = new()
|
|
{
|
|
WriteIndented = true,
|
|
IgnoreReadOnlyProperties = true,
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
|
|
};
|
|
|
|
protected override string DirectoryName => Path.Combine("Playlists");
|
|
|
|
protected override async Task<Playlist> DeserializeAsync(Stream stream)
|
|
{
|
|
PlaylistDto dto = await JsonSerializer.DeserializeAsync<PlaylistDto>(stream, _options) ?? new();
|
|
|
|
return dto.ToPlaylist();
|
|
}
|
|
|
|
protected override Task<string> SerializeAsync(Playlist playlist)
|
|
{
|
|
PlaylistDto dto = PlaylistDto.FromPlaylist(playlist);
|
|
|
|
return Task.FromResult(JsonSerializer.Serialize(dto, _options));
|
|
}
|
|
|
|
protected override string GetNewFileName()
|
|
{
|
|
for (int i = 0; i < 1000; i++)
|
|
{
|
|
string shortFileName = $"Playlist{i.ToString().PadLeft(3, '0')}.{Extension}";
|
|
string filePath = Path.Combine(DirectoryName, shortFileName);
|
|
|
|
if (File.Exists(filePath) == false)
|
|
return shortFileName;
|
|
}
|
|
|
|
throw new Exception("Unable to determine new fileName");
|
|
}
|
|
} |