Made changes discussed.

This commit is contained in:
Michael Bucari-Tovo 2021-08-22 13:27:39 -06:00
parent 3767c3574a
commit 6beb5cc74a
3 changed files with 49 additions and 66 deletions

View File

@ -156,72 +156,23 @@ namespace ApplicationServices
#region Update book details #region Update book details
public static int UpdateTags(Book libraryBook, string newTags) public static int UpdateUserDefinedItem(Book book)
{ {
try try
{ {
using var context = DbContexts.GetContext(); using var context = DbContexts.GetContext();
var udi = libraryBook.UserDefinedItem;
udi.Tags = newTags;
// Attach() NoTracking entities before SaveChanges() // Attach() NoTracking entities before SaveChanges()
context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.Attach(book.UserDefinedItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
var qtyChanges = context.SaveChanges(); var qtyChanges = context.SaveChanges();
if (qtyChanges > 0) if (qtyChanges > 0)
SearchEngineCommands.UpdateBookTags(libraryBook); SearchEngineCommands.UpdateLiberatedStatus(book);
return qtyChanges; return qtyChanges;
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Logger.Error(ex, "Error updating tags"); Log.Logger.Error(ex, $"Error updating {nameof(book.UserDefinedItem)}");
throw;
}
}
public static int UpdateBook(Book libraryBook, LiberatedStatus liberatedStatus)
{
try
{
using var context = DbContexts.GetContext();
var udi = libraryBook.UserDefinedItem;
udi.BookStatus = liberatedStatus;
// Attach() NoTracking entities before SaveChanges()
context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
var qtyChanges = context.SaveChanges();
if (qtyChanges > 0)
SearchEngineCommands.UpdateLiberatedStatus(libraryBook);
return qtyChanges;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Error updating audiobook status");
throw;
}
}
public static int UpdatePdf(Book libraryBook, LiberatedStatus? liberatedStatus)
{
try
{
using var context = DbContexts.GetContext();
var udi = libraryBook.UserDefinedItem;
udi.PdfStatus = liberatedStatus;
// Attach() NoTracking entities before SaveChanges()
context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
var qtyChanges = context.SaveChanges();
return qtyChanges;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Error updating pdf status");
throw; throw;
} }
} }

View File

@ -29,12 +29,12 @@ namespace LibationWinForms
private Book Book => LibraryBook.Book; private Book Book => LibraryBook.Book;
private Image _cover; private Image _cover;
private Action _refilter; private Action Refilter { get; }
public GridEntry(LibraryBook libraryBook, Action refilterOnChanged = null) public GridEntry(LibraryBook libraryBook, Action refilterOnChanged = null)
{ {
LibraryBook = libraryBook; LibraryBook = libraryBook;
_refilter = refilterOnChanged; Refilter = refilterOnChanged;
_memberValues = CreateMemberValueDictionary(); _memberValues = CreateMemberValueDictionary();
@ -76,7 +76,7 @@ namespace LibationWinForms
} }
} }
#region detect changes to the model and update the view #region detect changes to the model, update the view, and save to database.
/// <summary> /// <summary>
/// This event handler receives notifications from the model that it has changed. /// This event handler receives notifications from the model that it has changed.
@ -86,33 +86,62 @@ namespace LibationWinForms
{ {
var udi = sender as UserDefinedItem; var udi = sender as UserDefinedItem;
if (udi.Book.AudibleProductId != LibraryBook.Book.AudibleProductId) if (udi.Book.AudibleProductId != Book.AudibleProductId)
return; return;
switch (itemName) switch (itemName)
{ {
case nameof(udi.Tags): case nameof(udi.Tags):
{ {
LibraryCommands.UpdateTags(LibraryBook.Book, udi.Tags); Book.UserDefinedItem.Tags = udi.Tags;
NotifyPropertyChanged(nameof(DisplayTags)); NotifyPropertyChanged(nameof(DisplayTags));
} }
break; break;
case nameof(udi.BookStatus): case nameof(udi.BookStatus):
{ {
var status = udi.BookStatus == LiberatedStatus.PartialDownload ? LiberatedStatus.NotLiberated : udi.BookStatus; Book.UserDefinedItem.BookStatus = udi.BookStatus;
LibraryCommands.UpdateBook(LibraryBook.Book, status);
NotifyPropertyChanged(nameof(Liberate)); NotifyPropertyChanged(nameof(Liberate));
} }
break; break;
case nameof(udi.PdfStatus): case nameof(udi.PdfStatus):
{ {
LibraryCommands.UpdatePdf(LibraryBook.Book, udi.PdfStatus); Book.UserDefinedItem.PdfStatus = udi.PdfStatus;
NotifyPropertyChanged(nameof(Liberate)); NotifyPropertyChanged(nameof(Liberate));
} }
break; break;
} }
_refilter?.Invoke(); if (!suspendCommit)
Commit();
Refilter?.Invoke();
}
private bool suspendCommit = false;
/// <summary>
/// Begin editing the model, suspending commits until <see cref="EndEdit"/> is called.
/// </summary>
public void BeginEdit() => suspendCommit = true;
/// <summary>
/// Save all edits to the database.
/// </summary>
public void EndEdit()
{
Commit();
suspendCommit = false;
}
private void Commit()
{
//We don't want LiberatedStatus.PartialDownload to be a persistent status.
var bookStatus = Book.UserDefinedItem.BookStatus;
var saveStatus = bookStatus == LiberatedStatus.PartialDownload ? LiberatedStatus.NotLiberated : bookStatus;
Book.UserDefinedItem.BookStatus = saveStatus;
LibraryCommands.UpdateUserDefinedItem(Book);
Book.UserDefinedItem.BookStatus = bookStatus;
} }
#endregion #endregion
@ -144,7 +173,7 @@ namespace LibationWinForms
public string Description { get; } public string Description { get; }
public string DisplayTags public string DisplayTags
{ {
get=> Book.UserDefinedItem.Tags; get => string.Join("\r\n", Book.UserDefinedItem.TagsEnumerated);
set => Book.UserDefinedItem.Tags = value; set => Book.UserDefinedItem.Tags = value;
} }
public (LiberatedStatus BookStatus, LiberatedStatus? PdfStatus) Liberate public (LiberatedStatus BookStatus, LiberatedStatus? PdfStatus) Liberate
@ -267,5 +296,4 @@ namespace LibationWinForms
FileManager.PictureStorage.PictureCached -= PictureStorage_PictureCached; FileManager.PictureStorage.PictureCached -= PictureStorage_PictureCached;
} }
} }
} }

View File

@ -95,9 +95,13 @@ namespace LibationWinForms
if (bookDetailsForm.ShowDialog() != DialogResult.OK) if (bookDetailsForm.ShowDialog() != DialogResult.OK)
return; return;
liveGridEntry.BeginEdit();
liveGridEntry.DisplayTags = bookDetailsForm.NewTags; liveGridEntry.DisplayTags = bookDetailsForm.NewTags;
liveGridEntry.Liberate = (bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus); liveGridEntry.Liberate = (bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus);
liveGridEntry.EndEdit();
BackupCountsChanged?.Invoke(this, EventArgs.Empty); BackupCountsChanged?.Invoke(this, EventArgs.Empty);
} }