Adds music play history persistence and reporting endpoints

This commit is contained in:
BinaryAlley committed 2026-10-19 22:16:09 +03:00
1 parent 9919965a73
commit 009a0c8156
55 files changed
+6426 -3

No files matched your search

+50 -2
View File
@@ -64,6 +64,7 @@ public class UnitOfWork : IUnitOfWork, IDisposable
private IThemeRepository? _themeRepository;
private IUserRepository? _userRepository;
private IUserSettingsRepository? _userSettingsRepository;
private IMusicPlayRecordRepository? _musicPlayRecordRepository;
/// <summary>
/// Gets the permission repository.
@@ -374,6 +375,18 @@ public class UnitOfWork : IUnitOfWork, IDisposable
}
}
/// <summary>
/// Gets the music play record repository.
/// </summary>
public IMusicPlayRecordRepository MusicPlayRecordRepository
{
get
{
_musicPlayRecordRepository ??= new MusicPlayRecordRepository(_luminaDbContext);
return _musicPlayRecordRepository;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UnitOfWork"/> class.
/// </summary>
@@ -395,13 +408,28 @@ public class UnitOfWork : IUnitOfWork, IDisposable
await _luminaDbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return Result.Success;
}
// A concurrency conflict means that another request changed the same row after this request read it. It is an expected persistence
// conflict, so it is reported as a failure result instead of bubbling up as an unhandled exception.
catch (DbUpdateConcurrencyException)
{
return Errors.Persistence.ConcurrencyConflict;
}
// A unique constraint violation is an expected persistence conflict, so it is reported as a failure result instead of bubbling up
// as an unhandled exception and being turned into a generic internal server error. Any other exception is unexpected and is left
// to propagate to the centralized exception handling middleware.
// as an unhandled exception and being turned into a generic internal server error.
catch (DbUpdateException exception) when (IsUniqueConstraintViolation(exception))
{
return Errors.Persistence.UniqueConstraintViolation;
}
// SQLite reports a busy or locked database when another writer holds the lock. It is a concurrent write conflict, not an unexpected
// server failure, so it is reported the same way as an optimistic concurrency conflict.
catch (DbUpdateException exception) when (IsSqliteConcurrencyConflict(exception))
{
return Errors.Persistence.ConcurrencyConflict;
}
catch (SqliteException exception) when (IsSqliteBusyOrLocked(exception))
{
return Errors.Persistence.ConcurrencyConflict;
}
}
/// <summary>
@@ -416,6 +444,26 @@ public class UnitOfWork : IUnitOfWork, IDisposable
&& sqliteException.SqliteExtendedErrorCode == 2067; // SQLITE_CONSTRAINT_UNIQUE
}
/// <summary>
/// Determines whether the provided <paramref name="exception"/> was caused by a SQLite write conflict.
/// </summary>
/// <param name="exception">The exception to inspect.</param>
/// <returns><see langword="true"/> if the exception was caused by a busy or locked database, <see langword="false"/> otherwise.</returns>
private static bool IsSqliteConcurrencyConflict(DbUpdateException exception)
{
return exception.InnerException is SqliteException sqliteException && IsSqliteBusyOrLocked(sqliteException);
}
/// <summary>
/// Determines whether the provided <paramref name="exception"/> reports a busy or locked SQLite database.
/// </summary>
/// <param name="exception">The exception to inspect.</param>
/// <returns><see langword="true"/> if the database is busy or locked, <see langword="false"/> otherwise.</returns>
private static bool IsSqliteBusyOrLocked(SqliteException exception)
{
return exception.SqliteErrorCode is 5 or 6; // SQLITE_BUSY, SQLITE_LOCKED
}
/// <summary>
/// Detaches all the entities tracked by the unit of work, freeing the memory they occupy.
/// </summary>