Implements library management update library

This commit is contained in:
BinaryAlley committed 2024-11-17 21:02:10 +02:00
1 parent d0d53fd471
commit 291d1fa329
68 files changed
+1003 -110

No files matched your search

@@ -1,10 +1,11 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -42,4 +43,39 @@ internal sealed class LibraryRepository : ILibraryRepository
_luminaDbContext.Libraries.Add(library);
return Result.Created;
}
/// <summary>
/// Gets a <see cref="LibraryEntity"/> identified by <paramref name="id"/> from the storage medium.
/// </summary>
/// <param name="id">The id of the library to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> containing either a <see cref="LibraryEntity"/> identified by <paramref name="id"/>, or an error.</returns>
public async Task<ErrorOr<LibraryEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
{
return await _luminaDbContext.Libraries
.Include(library => library.ContentLocations)
.FirstOrDefaultAsync(library => library.Id == id, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Updates a media library.
/// </summary>
/// <param name="data">Ther media library to update.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
public async Task<ErrorOr<Updated>> UpdateAsync(LibraryEntity data, CancellationToken cancellationToken)
{
LibraryEntity? foundLibrary = await _luminaDbContext.Libraries
.Include(library => library.ContentLocations)
.FirstOrDefaultAsync(library => library.Id == data.Id, cancellationToken).ConfigureAwait(false);
if (foundLibrary is null)
return Errors.Library.LibraryNotFound;
// update scalar properties
_luminaDbContext.Entry(foundLibrary).CurrentValues.SetValues(data);
// update owned entities (their changes are not automatically tracked by EF)
foundLibrary.ContentLocations.Clear();
foreach (LibraryContentLocationEntity contentLocation in data.ContentLocations)
foundLibrary.ContentLocations.Add(contentLocation);
return Result.Updated;
}
}