using System.IO;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Radiosender.Models;
namespace Radiosender.Services;
///
/// The music library in a SQLite file, accessed through Entity Framework Core.
/// Every call uses its own short-lived context, so the repository can be used from any thread.
///
public class SongRepository
{
public static string DefaultDatabasePath { get; } = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Radiosender", "radiosender.db");
private readonly DbContextOptions _options;
public SongRepository(string databasePath)
{
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(databasePath))!);
_options = new DbContextOptionsBuilder()
.UseSqlite(new SqliteConnectionStringBuilder { DataSource = databasePath }.ToString())
.Options;
// Creates the file and the table on first start; an existing database is left as it is.
using var context = Open();
context.Database.EnsureCreated();
}
/// Ids of all provider results that are already in the library.
public IReadOnlySet GetExternalIds()
{
using var context = Open();
return context.Songs.Where(s => s.ExternalId != null).Select(s => s.ExternalId!).ToHashSet();
}
public IReadOnlyList GetAll()
{
using var context = Open();
return context.Songs.AsNoTracking().OrderBy(s => s.Artist).ThenBy(s => s.Title).ToList();
}
public Song? GetById(long id)
{
using var context = Open();
return context.Songs.AsNoTracking().FirstOrDefault(s => s.Id == id);
}
/// Inserts the song and sets its .
/// The song has no duration.
/// The database rejected the song, e.g. a second song with the same external id.
public void Add(Song song)
{
RequireDuration(song);
using var context = Open();
context.Songs.Add(song);
context.SaveChanges();
}
/// Stores everything except the external id, which never changes after the import.
/// false if no song with this id exists.
/// The song has no duration.
public bool Update(Song song)
{
RequireDuration(song);
using var context = Open();
return context.Songs.Where(s => s.Id == song.Id).ExecuteUpdate(s => s
.SetProperty(x => x.Title, song.Title)
.SetProperty(x => x.Artist, song.Artist)
.SetProperty(x => x.Duration, song.Duration)
.SetProperty(x => x.FilePath, song.FilePath)
.SetProperty(x => x.Source, song.Source)
.SetProperty(x => x.License, song.License)
.SetProperty(x => x.Attribution, song.Attribution)) == 1;
}
/// false if no song with this id exists.
public bool Delete(long id)
{
using var context = Open();
return context.Songs.Where(s => s.Id == id).ExecuteDelete() == 1;
}
private LibraryContext Open() => new(_options);
// Songs need a length for scheduling. Checked here, so the rule does not depend on the database schema.
private static void RequireDuration(Song song)
{
if (song.Duration <= TimeSpan.Zero)
throw new ArgumentException("A song needs a duration longer than zero.", nameof(song));
}
/// Maps to the table Songs.
private sealed class LibraryContext(DbContextOptions options) : DbContext(options)
{
public DbSet Songs => Set();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var song = modelBuilder.Entity();
song.ToTable("Songs");
// Stored as whole milliseconds in the column the first version of the app created.
song.Property(s => s.Duration)
.HasColumnName("DurationMs")
.HasConversion(duration => (long)duration.TotalMilliseconds, milliseconds => TimeSpan.FromMilliseconds(milliseconds));
// The column keeps the name it got when Openverse was the only provider.
song.Property(s => s.ExternalId).HasColumnName("OpenverseId");
// One song per provider result, also if two imports of the same track race.
// SQLite allows any number of rows without an id in a unique index.
song.HasIndex(s => s.ExternalId).IsUnique();
}
}
}