This commit is contained in:
Robert McRackan 2021-09-02 15:19:55 -04:00
parent b4803c42a5
commit 802763a4fb
3 changed files with 23 additions and 23 deletions

View File

@ -7,13 +7,11 @@ namespace ApplicationServices
{
public static class DbContexts
{
//// idea for future command/query separation
// public static LibationContext GetCommandContext() { }
// public static LibationContext GetQueryContext() { }
/// <summary>Use for fully functional context, incl. SaveChanges(). For query-only, use the other method</summary>
public static LibationContext GetContext()
=> LibationContext.Create(SqliteStorage.ConnectionString);
/// <summary>Use for full library querying. No lazy loading</summary>
public static List<LibraryBook> GetLibrary_Flat_NoTracking()
{
using var context = GetContext();

View File

@ -155,13 +155,30 @@ namespace ApplicationServices
}
#endregion
#region Update book details
#region remove books
public static List<LibraryBook> RemoveBooks(List<string> idsToRemove)
{
using var context = DbContexts.GetContext();
var libBooks = context.GetLibrary_Flat_NoTracking();
var removeLibraryBooks = libBooks.Where(lb => idsToRemove.Contains(lb.Book.AudibleProductId)).ToList();
context.Library.RemoveRange(removeLibraryBooks);
var qtyChanges = context.SaveChanges();
if (qtyChanges > 0)
SearchEngineCommands.FullReIndex();
return removeLibraryBooks;
}
#endregion
/// <summary>
/// Occurs when <see cref="UserDefinedItem.Tags"/>, <see cref="UserDefinedItem.BookStatus"/>, or <see cref="UserDefinedItem.PdfStatus"/>
/// changed values are successfully persisted.
/// </summary>
public static event EventHandler<string> BookUserDefinedItemCommitted;
#region Update book details
public static int UpdateUserDefinedItem(Book book)
{
try
@ -187,21 +204,6 @@ namespace ApplicationServices
}
#endregion
#region remove books
public static List<LibraryBook> RemoveBooks(List<string> idsToRemove)
{
using var context = DbContexts.GetContext();
var libBooks = context.GetLibrary_Flat_NoTracking();
var removeLibraryBooks = libBooks.Where(lb => idsToRemove.Contains(lb.Book.AudibleProductId)).ToList();
context.Library.RemoveRange(removeLibraryBooks);
context.SaveChanges();
return removeLibraryBooks;
}
#endregion
public static LiberatedStatus Liberated_Status(Book book)
=> book.Audio_Exists ? LiberatedStatus.Liberated
: FileManager.AudibleFileStorage.AaxcExists(book.AudibleProductId) ? LiberatedStatus.PartialDownload

View File

@ -40,17 +40,17 @@ namespace ApplicationServices
}
}
private static T performSearchEngineFunc_safe<T>(Func<SearchEngine, T> action)
private static T performSearchEngineFunc_safe<T>(Func<SearchEngine, T> func)
{
var engine = new SearchEngine();
try
{
return action(engine);
return func(engine);
}
catch (FileNotFoundException)
{
FullReIndex(engine);
return action(engine);
return func(engine);
}
}
}