public static async Task <(int totalCount, int newCount)> ImportAccountAsync(Func <Account, ILoginCallback> loginCallbackFactoryFunc, params Account[] accounts) { if (accounts is null || accounts.Length == 0) { return(0, 0); } try { var importItems = await scanAccountsAsync(loginCallbackFactoryFunc, accounts); var totalCount = importItems.Count; Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); var newCount = await importIntoDbAsync(importItems); Log.Logger.Information($"Import: New count {newCount}"); await Task.Run(() => SearchEngineCommands.FullReIndex()); Log.Logger.Information("FullReIndex: success"); return(totalCount, newCount); } catch (AudibleApi.Authentication.LoginFailedException lfEx) { lfEx.SaveFiles(FileManager.Configuration.Instance.LibationFiles); // nuget Serilog.Exceptions would automatically log custom properties // However, it comes with a scary warning when used with EntityFrameworkCore which I'm not yet ready to implement: // https://github.com/RehanSaeed/Serilog.Exceptions // work-around: use 3rd param. don't just put exception object in 3rd param -- info overload: stack trace, etc Log.Logger.Error(lfEx, "Error importing library. Login failed. {@DebugInfo}", new { lfEx.RequestUrl, ResponseStatusCodeNumber = (int)lfEx.ResponseStatusCode, ResponseStatusCodeDesc = lfEx.ResponseStatusCode, lfEx.ResponseInputFields, lfEx.ResponseBodyFilePaths }); throw; } catch (Exception ex) { Log.Logger.Error(ex, "Error importing library"); throw; } }
public static int UpdateTags(this LibationContext context, Book book, string newTags) { try { book.UserDefinedItem.Tags = newTags; var qtyChanges = context.SaveChanges(); if (qtyChanges > 0) { SearchEngineCommands.UpdateBookTags(book); } return(qtyChanges); } catch (Exception ex) { Log.Logger.Error(ex, "Error updating tags"); throw; } }
public static int UpdateUserDefinedItem(Book book) { try { using var context = DbContexts.GetContext(); // Attach() NoTracking entities before SaveChanges() context.Attach(book.UserDefinedItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified; var qtyChanges = context.SaveChanges(); if (qtyChanges > 0) { SearchEngineCommands.UpdateLiberatedStatus(book); SearchEngineCommands.UpdateBookTags(book); BookUserDefinedItemCommitted?.Invoke(null, book.AudibleProductId); } return(qtyChanges); } catch (Exception ex) { Log.Logger.Error(ex, $"Error updating {nameof(book.UserDefinedItem)}"); throw; } }
// call this whenever books are added or removed from library private static void finalizeLibrarySizeChange() { SearchEngineCommands.FullReIndex(); LibrarySizeChanged?.Invoke(null, null); }