Skip to content

Commit

Permalink
Merge pull request #94 from VPKSoft/fixAlbumCreate
Browse files Browse the repository at this point in the history
Fix album saving.
  • Loading branch information
Petteri Kautonen authored Aug 18, 2022
2 parents 87a09b4 + c3ba8ac commit 94b0fb7
Show file tree
Hide file tree
Showing 28 changed files with 592 additions and 127 deletions.
60 changes: 36 additions & 24 deletions amp.Database/ExtensionClasses/DbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*/
#endregion

using System.Reflection;
using amp.Shared.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace amp.Database.ExtensionClasses;

Expand All @@ -35,36 +36,47 @@ namespace amp.Database.ExtensionClasses;
public static class DbContextExtensions
{
/// <summary>
/// Updates the specified range of entities, saves the possible changes and stops tracking the <paramref name="entities"/> if the <paramref name="clearTracking"/> is set to <c>true</c>.
/// Upserts the specified entity range into the database and saves the changes.
/// </summary>
/// <typeparam name="TEntity">The type of entity being operated on by this set.</typeparam>
/// <param name="context">The database context the entities belong to.</param>
/// <param name="entities">The entities to update.</param>
/// <param name="clearTracking">if set to <c>true</c> the <see cref="ChangeTracker.Clear()"/> is called.</param>
/// <returns>A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.</returns>
public static async Task<int> UpdateRangeAndSave<TEntity>(this DbContext context, IEnumerable<TEntity> entities, bool clearTracking = true) where TEntity : class
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="context">The database context.</param>
/// <param name="entities">The entities to upsert.</param>
public static async Task UpsertRange<TEntity>(this DbContext context, params TEntity[] entities)
where TEntity : class, IEntity
{
return await context.UpdateRangeAndSave(entities.ToArray(), clearTracking);
var toInsert = entities.Where(f => f.Id == 0).ToList();
var toUpdate = entities.Where(f => f.Id != 0).ToList();

foreach (var entity in toUpdate)
{
var update = await context.Set<TEntity>().FirstOrDefaultAsync(f => f.Id == entity.Id);
if (update != null)
{
UpdateEntity(update, entity);
await context.SaveChangesAsync();
}
}

if (toInsert.Count > 0)
{
context.Set<TEntity>().AddRange(toInsert);
await context.SaveChangesAsync();
}
}

/// <summary>
/// Updates the specified range of entities, saves the possible changes and stops tracking the <paramref name="entities"/> if the <paramref name="clearTracking"/> is set to <c>true</c>.
/// </summary>
/// <typeparam name="TEntity">The type of entity being operated on by this set.</typeparam>
/// <param name="context">The database context the entities belong to.</param>
/// <param name="entities">The entities to update.</param>
/// <param name="clearTracking">if set to <c>true</c> the <see cref="ChangeTracker.Clear()"/> is called.</param>
/// <returns>A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.</returns>
public static async Task<int> UpdateRangeAndSave<TEntity>(this DbContext context, TEntity[] entities, bool clearTracking = true) where TEntity : class
private static void UpdateEntity(IEntity destination, IEntity source)
{
context.Set<TEntity>().UpdateRange(entities);
var result = await context.SaveChangesAsync();
var propertyInfos = destination.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);

if (clearTracking)
foreach (var propertyInfo in propertyInfos)
{
context.ChangeTracker.Clear();
}
if (destination.Id != 0 && propertyInfo.Name == nameof(IEntity.Id) || propertyInfo.Name == nameof(IRowVersionEntity.RowVersion))
{
continue;
}

return result;
var sourceValue = propertyInfo.GetValue(source);
propertyInfo.SetValue(destination, sourceValue);
}
}
}
2 changes: 1 addition & 1 deletion amp.EtoForms/Classes/AlbumTrackMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System.Collections.ObjectModel;
using amp.Database;
using amp.EtoForms.Models;
using amp.EtoForms.DtoClasses;
using Eto.Forms;
using EtoForms.Controls.Custom.Utilities;
using Microsoft.EntityFrameworkCore;
Expand Down
30 changes: 24 additions & 6 deletions amp.EtoForms/Dialogs/DialogModifySavedQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

using System.Collections.ObjectModel;
using amp.Database;
using amp.Database.DataModel;
using amp.Database.ExtensionClasses;
using amp.EtoForms.DtoClasses;
using amp.EtoForms.Utilities;
using amp.Shared.Localization;
using Eto.Drawing;
Expand Down Expand Up @@ -103,14 +102,14 @@ public DialogModifySavedQueue(AmpContext context, long queueId)
new GridColumn
{
DataCell = new TextBoxCell(nameof(QueueTrack.QueueIndex)),
HeaderText = UI.QueueCreated,
HeaderText = UI.QueueIndex,
},
new GridColumn
{
DataCell = new TextBoxCell
{
Binding = Binding
.Property((QueueTrack qs) => TrackDisplayNameGenerate.GetAudioTrackName(qs.AudioTrack!))
.Property((QueueTrack qs) => TrackDisplayNameGenerate.GetAudioTrackName(qs.AudioTrack))
.Convert(s => s)
.Cast<string?>(),
}, Expand = true,
Expand Down Expand Up @@ -211,7 +210,7 @@ private async Task ListQueue()
{
queueTracks.Clear();
foreach (var queueTrack in await context.QueueTracks.Include(f => f.AudioTrack).Where(f => f.QueueSnapshotId == queueId)
.OrderBy(f => f.QueueIndex).AsNoTracking().ToListAsync())
.OrderBy(f => f.QueueIndex).Select(f => Globals.AutoMapper.Map<QueueTrack>(f)).ToListAsync())
{
queueTracks.Add(queueTrack);
}
Expand All @@ -225,6 +224,7 @@ private async void DialogModifySavedQueue_Shown(object? sender, EventArgs e)

private async void BtnSaveAndClose_Click(object? sender, EventArgs e)
{
context.ChangeTracker.Clear();
await using var transaction = await context.Database.BeginTransactionAsync();

await Globals.LoggerSafeInvokeAsync(async () =>
Expand All @@ -233,12 +233,30 @@ await Globals.LoggerSafeInvokeAsync(async () =>
context.QueueTracks.RemoveRange(itemsToDelete);
await context.SaveChangesAsync();

await context.UpdateRangeAndSave(queueTracks);
foreach (var queueTrack in queueTracks)
{
await UpdateTrack(context, queueTrack);
}

await context.SaveChangesAsync();
await transaction.CommitAsync();
}, async (_) => await transaction.RollbackAsync());

Close(true);
}

private static async Task UpdateTrack(AmpContext context, QueueTrack queueTrack)
{
var track = await context.QueueTracks.FirstAsync(f => f.Id == queueTrack.Id);
track.AudioTrackId = queueTrack.AudioTrackId;
track.CreatedAtUtc = queueTrack.CreatedAtUtc;
track.ModifiedAtUtc = queueTrack.ModifiedAtUtc;
track.QueueSnapshotId = queueTrack.QueueSnapshotId;
track.QueueIndex = queueTrack.QueueIndex;

await context.SaveChangesAsync();
}

private void BtnCancel_Click(object? sender, EventArgs e)
{
Close(false);
Expand Down
2 changes: 1 addition & 1 deletion amp.EtoForms/Dialogs/DialogUpdateTagData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#endregion

using amp.Database;
using amp.EtoForms.Models;
using amp.EtoForms.DtoClasses;
using amp.Shared.Classes;
using amp.Shared.Localization;
using Eto.Drawing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Runtime.CompilerServices;
using amp.Shared.Interfaces;

namespace amp.EtoForms.Models;
namespace amp.EtoForms.DtoClasses;

/// <summary>
/// A class for album.
Expand Down Expand Up @@ -56,7 +56,7 @@ public DateTime? ModifiedAtUtc
if (modifiedAtUtc != value)
{
modifiedAtUtc = value;
OnPropertyChanged(nameof(ModifiedAtUtc));
OnPropertyChanged();
}
}
}
Expand All @@ -71,7 +71,7 @@ public DateTime CreatedAtUtc
if (createdAtUtc != value)
{
createdAtUtc = value;
OnPropertyChanged(nameof(CreatedAtUtc));
OnPropertyChanged();
}
}
}
Expand All @@ -86,7 +86,7 @@ public string AlbumName
if (albumName != value)
{
albumName = value;
OnPropertyChanged(nameof(AlbumName));
OnPropertyChanged();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using amp.EtoForms.Utilities;
using amp.Shared.Interfaces;

namespace amp.EtoForms.Models;
namespace amp.EtoForms.DtoClasses;

/// <summary>
/// A class for album tracks.
Expand Down Expand Up @@ -78,7 +78,7 @@ public DateTime? ModifiedAtUtc
if (modifiedAtUtc != value)
{
modifiedAtUtc = value;
OnPropertyChanged(nameof(ModifiedAtUtc));
OnPropertyChanged();
}
}
}
Expand All @@ -93,7 +93,7 @@ public DateTime CreatedAtUtc
if (createdAtUtc != value)
{
createdAtUtc = value;
OnPropertyChanged(nameof(CreatedAtUtc));
OnPropertyChanged();
}
}
}
Expand All @@ -108,7 +108,7 @@ public long AlbumId
if (albumId != value)
{
albumId = value;
OnPropertyChanged(nameof(AlbumId));
OnPropertyChanged();
}
}

Expand All @@ -124,7 +124,7 @@ public long AudioTrackId
if (trackId != value)
{
trackId = value;
OnPropertyChanged(nameof(AudioTrackId));
OnPropertyChanged();
}
}
}
Expand All @@ -139,7 +139,7 @@ public int QueueIndex
if (queueIndex != value)
{
queueIndex = value;
OnPropertyChanged(nameof(QueueIndex));
OnPropertyChanged();
}
}

Expand All @@ -155,7 +155,7 @@ public int QueueIndexAlternate
if (queueIndexAlternate != value)
{
queueIndexAlternate = value;
OnPropertyChanged(nameof(QueueIndexAlternate));
OnPropertyChanged();
}
}

Expand All @@ -171,7 +171,7 @@ public AudioTrack? AudioTrack
if (track != value)
{
track = value;
OnPropertyChanged(nameof(AudioTrack));
OnPropertyChanged();
}
}
}
Expand All @@ -186,7 +186,7 @@ public Album? Album
if (album != value)
{
album = value;
OnPropertyChanged(nameof(Album));
OnPropertyChanged();
}
}
}
Expand Down
Loading

0 comments on commit 94b0fb7

Please sign in to comment.