mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Standardizes the remaining boolean names across the stack
This commit is contained in:
1 parent
3e0c3b624f
commit
39f4a04459
40 files changed
+1062
-448
No files matched your search
@@ -2,8 +2,11 @@
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -38,8 +41,8 @@ internal sealed class PermissionRepository : IPermissionRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(PermissionEntity permission, CancellationToken cancellationToken)
|
||||
{
|
||||
bool permissionExists = await _luminaDbContext.Permissions.AnyAsync(repositoryPermission => repositoryPermission.Id == permission.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (permissionExists)
|
||||
bool doesPermissionExist = await _luminaDbContext.Permissions.AnyAsync(repositoryPermission => repositoryPermission.Id == permission.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (doesPermissionExist)
|
||||
return Errors.Authorization.PermissionAlreadyExists;
|
||||
|
||||
_luminaDbContext.Permissions.Add(permission);
|
||||
@@ -47,13 +50,54 @@ internal sealed class PermissionRepository : IPermissionRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all autorization permissions.
|
||||
/// Gets all the authorization permissions, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching permissions are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="PermissionEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<PermissionEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="PermissionEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<PermissionEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Permissions.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<PermissionEntity> query = _luminaDbContext.Permissions;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the permissions.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<PermissionEntity> allPermissions = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<PermissionEntity>
|
||||
{
|
||||
Data = allPermissions,
|
||||
CurrentPage = 1,
|
||||
PerPage = allPermissions.Count,
|
||||
Count = allPermissions.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(permission => permission.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<PermissionEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<PermissionEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -37,8 +41,8 @@ internal sealed class RoleRepository : IRoleRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(RoleEntity role, CancellationToken cancellationToken)
|
||||
{
|
||||
bool roleExists = await _luminaDbContext.Roles.AnyAsync(repositoryRole => repositoryRole.Id == role.Id || repositoryRole.RoleName == role.RoleName, cancellationToken).ConfigureAwait(false);
|
||||
if (roleExists)
|
||||
bool doesRoleExist = await _luminaDbContext.Roles.AnyAsync(repositoryRole => repositoryRole.Id == role.Id || repositoryRole.RoleName == role.RoleName, cancellationToken).ConfigureAwait(false);
|
||||
if (doesRoleExist)
|
||||
return Errors.Authorization.RoleAlreadyExists;
|
||||
|
||||
_luminaDbContext.Roles.Add(role);
|
||||
@@ -46,13 +50,57 @@ internal sealed class RoleRepository : IRoleRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all autorization roles.
|
||||
/// Gets all the authorization roles, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching roles are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="RoleEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<RoleEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="RoleEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<RoleEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Roles.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<RoleEntity> query = _luminaDbContext.Roles;
|
||||
// Apply no-tracking when the caller only reads, so the entities are not tracked by the context.
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the roles.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<RoleEntity> allRoles = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<RoleEntity>
|
||||
{
|
||||
Data = allRoles,
|
||||
CurrentPage = 1,
|
||||
PerPage = allRoles.Count,
|
||||
Count = allRoles.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
// Count the matching roles first, so the requested page can be clamped to the number of available pages.
|
||||
int count = await query.Select(role => role.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
// Fetch only the requested page of roles.
|
||||
IReadOnlyList<RoleEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<RoleEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -76,13 +124,19 @@ internal sealed class RoleRepository : IRoleRepository
|
||||
/// <param name="id">The id of the authorization role to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="RoleEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<RoleEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<RoleEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.Roles
|
||||
.Include(role => role.RolePermissions)
|
||||
.ThenInclude(rolePermission => rolePermission.Permission)
|
||||
.FirstOrDefaultAsync(role => role.Id == id, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
IQueryable<RoleEntity> query = _luminaDbContext.Roles;
|
||||
// Apply no-tracking when the caller only reads, so the entities are not tracked by the context.
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(role => role.RolePermissions)
|
||||
.ThenInclude(rolePermission => rolePermission.Permission);
|
||||
}
|
||||
return await query.FirstOrDefaultAsync(role => role.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -93,16 +147,18 @@ internal sealed class RoleRepository : IRoleRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Updated>> UpdateAsync(RoleEntity data, CancellationToken cancellationToken)
|
||||
{
|
||||
// check if a role with the requested Id exists, and retrieve it
|
||||
// Check if a role with the requested Id exists, and retrieve it.
|
||||
RoleEntity? foundRole = await _luminaDbContext.Roles
|
||||
.Include(role => role.RolePermissions)
|
||||
.ThenInclude(rolePermission => rolePermission.Permission)
|
||||
.FirstOrDefaultAsync(role => role.Id == data.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (foundRole is null)
|
||||
return Errors.Authorization.RoleNotFound;
|
||||
// update scalar properties
|
||||
|
||||
// Update the scalar properties.
|
||||
_luminaDbContext.Entry(foundRole).CurrentValues.SetValues(data);
|
||||
// update owned entities (their changes are not automatically tracked by EF)
|
||||
|
||||
// Update the owned entities, whose changes are not tracked by EF automatically.
|
||||
foundRole.RolePermissions.Clear();
|
||||
foreach (RolePermissionEntity rolePermission in data.RolePermissions)
|
||||
foundRole.RolePermissions.Add(rolePermission);
|
||||
@@ -117,12 +173,13 @@ internal sealed class RoleRepository : IRoleRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
// check if a role with the requested Id exists, and retrieve it
|
||||
// Check if a role with the requested Id exists, and retrieve it.
|
||||
RoleEntity? foundRole = await _luminaDbContext.Roles
|
||||
.FirstOrDefaultAsync(role => role.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
if (foundRole is null)
|
||||
return Errors.Authorization.RoleNotFound;
|
||||
// then, remove it
|
||||
|
||||
// Remove the role.
|
||||
_luminaDbContext.Remove(foundRole);
|
||||
return Result.Deleted;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Common.Persistence;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -38,8 +42,8 @@ internal sealed class LibraryRepository : ILibraryRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(LibraryEntity library, CancellationToken cancellationToken)
|
||||
{
|
||||
bool libraryExists = await _luminaDbContext.Libraries.AnyAsync(repositoryLibrary => repositoryLibrary.Id == library.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (libraryExists)
|
||||
bool doesLibraryExist = await _luminaDbContext.Libraries.AnyAsync(repositoryLibrary => repositoryLibrary.Id == library.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (doesLibraryExist)
|
||||
return Errors.Library.LibraryAlreadyExists;
|
||||
|
||||
_luminaDbContext.Libraries.Add(library);
|
||||
@@ -52,11 +56,17 @@ internal sealed class LibraryRepository : ILibraryRepository
|
||||
/// <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="Result{TValue}"/> containing either a <see cref="LibraryEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<LibraryEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<LibraryEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.Libraries
|
||||
.Include(library => library.ContentLocations)
|
||||
.FirstOrDefaultAsync(library => library.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<LibraryEntity> query = _luminaDbContext.Libraries;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(library => library.ContentLocations);
|
||||
}
|
||||
return await query.FirstOrDefaultAsync(library => library.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -86,21 +96,65 @@ internal sealed class LibraryRepository : ILibraryRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all media libraries.
|
||||
/// Gets all the media libraries, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching media libraries are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="LibraryEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<LibraryEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="LibraryEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<LibraryEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Libraries
|
||||
.Include(library => library.ContentLocations)
|
||||
IQueryable<LibraryEntity> query = _luminaDbContext.Libraries;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(library => library.ContentLocations);
|
||||
}
|
||||
|
||||
// If no pagination was requested, return all the media libraries.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<LibraryEntity> allLibraries = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<LibraryEntity>
|
||||
{
|
||||
Data = allLibraries,
|
||||
CurrentPage = 1,
|
||||
PerPage = allLibraries.Count,
|
||||
Count = allLibraries.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(library => library.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<LibraryEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<LibraryEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates a media library.
|
||||
/// Updates a media library, replacing only the editable data that actually changed, while preserving its identity, its audit columns, and the identity of its content locations.
|
||||
/// </summary>
|
||||
/// <param name="data">The media library to update.</param>
|
||||
/// <param name="data">The media library whose editable data is applied to the stored library.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Updated>> UpdateAsync(LibraryEntity data, CancellationToken cancellationToken)
|
||||
@@ -110,12 +164,20 @@ internal sealed class LibraryRepository : ILibraryRepository
|
||||
.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);
|
||||
|
||||
// The stored identity is never overwritten by an edit, and the audit columns are only ever written by the auditing interceptor.
|
||||
EditableValuesCopier.CopyEditableValues(_luminaDbContext, foundLibrary, data);
|
||||
|
||||
// A content location is matched by its path. Matched locations keep their identity and the locations that did not change are left exactly as they are stored,
|
||||
// so that editing a library never deletes and re-inserts all of its content locations.
|
||||
CollectionReconciler.Reconcile(
|
||||
foundLibrary.ContentLocations,
|
||||
data.ContentLocations,
|
||||
existingContentLocation => existingContentLocation.Path,
|
||||
incomingContentLocation => incomingContentLocation.Path,
|
||||
shouldReplace: (existingContentLocation, incomingContentLocation) => false,
|
||||
createNew: incomingContentLocation => incomingContentLocation);
|
||||
|
||||
return Result.Updated;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,9 +39,9 @@ internal sealed class LibraryScanRepository : ILibraryScanRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(LibraryScanEntity libraryScan, CancellationToken cancellationToken)
|
||||
{
|
||||
bool libraryScanExists = await _luminaDbContext.LibraryScans.AnyAsync(
|
||||
bool doesLibraryScanExist = await _luminaDbContext.LibraryScans.AnyAsync(
|
||||
repositoryScanLibrary => repositoryScanLibrary.Id == libraryScan.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (libraryScanExists)
|
||||
if (doesLibraryScanExist)
|
||||
return Errors.LibraryScanning.LibraryScanAlreadyExists;
|
||||
|
||||
_luminaDbContext.LibraryScans.Add(libraryScan);
|
||||
@@ -54,12 +54,18 @@ internal sealed class LibraryScanRepository : ILibraryScanRepository
|
||||
/// <param name="id">The id of the library scan to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="LibraryScanEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<LibraryScanEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<LibraryScanEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.LibraryScans
|
||||
.Include(libraryScan => libraryScan.Library)
|
||||
.Include(libraryScan => libraryScan.User)
|
||||
.FirstOrDefaultAsync(libraryScan => libraryScan.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<LibraryScanEntity> query = _luminaDbContext.LibraryScans;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(libraryScan => libraryScan.Library)
|
||||
.Include(libraryScan => libraryScan.User);
|
||||
}
|
||||
return await query.FirstOrDefaultAsync(libraryScan => libraryScan.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,7 +107,7 @@ internal sealed class LibraryScanRepository : ILibraryScanRepository
|
||||
.FirstOrDefaultAsync(libraryScan => libraryScan.Id == data.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (foundLibraryScan is null)
|
||||
return Errors.LibraryScanning.LibraryScanNotFound;
|
||||
// update scalar properties
|
||||
// Update scalar properties.
|
||||
_luminaDbContext.Entry(foundLibraryScan).CurrentValues.SetValues(data);
|
||||
return Result.Updated;
|
||||
}
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -35,21 +39,63 @@ internal sealed class PluginRepository : IPluginRepository
|
||||
/// <param name="id">The Id of the plugin to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="PluginEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PluginEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<PluginEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.Plugins
|
||||
.FirstOrDefaultAsync(plugin => plugin.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<PluginEntity> query = _luminaDbContext.Plugins;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
return await query.FirstOrDefaultAsync(plugin => plugin.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all the detected plugins.
|
||||
/// Gets all the detected plugins, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching plugins are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="PluginEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<PluginEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="PluginEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<PluginEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Plugins
|
||||
IQueryable<PluginEntity> query = _luminaDbContext.Plugins;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the plugins.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<PluginEntity> allPlugins = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<PluginEntity>
|
||||
{
|
||||
Data = allPlugins,
|
||||
CurrentPage = 1,
|
||||
PerPage = allPlugins.Count,
|
||||
Count = allPlugins.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(plugin => plugin.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<PluginEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<PluginEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -66,7 +112,7 @@ internal sealed class PluginRepository : IPluginRepository
|
||||
_luminaDbContext.Plugins.Add(plugin);
|
||||
else
|
||||
{
|
||||
// update only the detection fields, preserving the stored settings and creation date
|
||||
// Update only the detection fields, preserving the stored settings and creation date.
|
||||
existingPlugin.Name = plugin.Name;
|
||||
existingPlugin.Author = plugin.Author;
|
||||
existingPlugin.Version = plugin.Version;
|
||||
|
||||
+7
-4
@@ -36,11 +36,14 @@ internal sealed class ScheduledJobExecutionRepository : IScheduledJobExecutionRe
|
||||
/// <param name="id">The id of the execution to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="ScheduledJobExecutionEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<ScheduledJobExecutionEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<ScheduledJobExecutionEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// FindAsync returns an entity that was already loaded in the current unit of work from the change tracker, and only
|
||||
// reads it from the storage medium when it is not tracked, so an entity loaded earlier is not read again.
|
||||
return await _luminaDbContext.ScheduledJobExecutions.FindAsync([id], cancellationToken).ConfigureAwait(false);
|
||||
// The execution is read with a query instead of FindAsync so that it can be retrieved without tracking when requested.
|
||||
// When the entity is tracked, the change tracker still returns the already loaded instance instead of a new one.
|
||||
IQueryable<ScheduledJobExecutionEntity> query = _luminaDbContext.ScheduledJobExecutions;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
return await query.FirstOrDefaultAsync(execution => execution.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
@@ -37,11 +40,14 @@ internal sealed class ScheduledJobRepository : IScheduledJobRepository
|
||||
/// <param name="id">The id of the scheduled job to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="ScheduledJobEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<ScheduledJobEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<ScheduledJobEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// FindAsync returns an entity that was already loaded in the current unit of work from the change tracker, and only
|
||||
// reads it from the storage medium when it is not tracked, so an entity loaded earlier is not read again.
|
||||
return await _luminaDbContext.ScheduledJobs.FindAsync([id], cancellationToken).ConfigureAwait(false);
|
||||
// The scheduled job is read with a query instead of FindAsync so that it can be retrieved without tracking when requested.
|
||||
// When the entity is tracked, the change tracker still returns the already loaded instance instead of a new one.
|
||||
IQueryable<ScheduledJobEntity> query = _luminaDbContext.ScheduledJobs;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
return await query.FirstOrDefaultAsync(scheduledJob => scheduledJob.Id == id, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -59,14 +65,54 @@ internal sealed class ScheduledJobRepository : IScheduledJobRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all scheduled jobs from the storage medium.
|
||||
/// Gets all the scheduled jobs from the storage medium, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching scheduled jobs are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="ScheduledJobEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<ScheduledJobEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="ScheduledJobEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<ScheduledJobEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.ScheduledJobs
|
||||
IQueryable<ScheduledJobEntity> query = _luminaDbContext.ScheduledJobs;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the scheduled jobs.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<ScheduledJobEntity> allScheduledJobs = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<ScheduledJobEntity>
|
||||
{
|
||||
Data = allScheduledJobs,
|
||||
CurrentPage = 1,
|
||||
PerPage = allScheduledJobs.Count,
|
||||
Count = allScheduledJobs.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(scheduledJob => scheduledJob.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<ScheduledJobEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<ScheduledJobEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,8 +2,11 @@
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Themes;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -38,8 +41,8 @@ internal sealed class ThemeRepository : IThemeRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(ThemeEntity theme, CancellationToken cancellationToken)
|
||||
{
|
||||
bool themeExists = await _luminaDbContext.Themes.AnyAsync(repositoryTheme => repositoryTheme.Id == theme.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (themeExists)
|
||||
bool doesThemeExist = await _luminaDbContext.Themes.AnyAsync(repositoryTheme => repositoryTheme.Id == theme.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (doesThemeExist)
|
||||
return Errors.Themes.ThemeNotFound;
|
||||
|
||||
_luminaDbContext.Themes.Add(theme);
|
||||
@@ -64,13 +67,54 @@ internal sealed class ThemeRepository : IThemeRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all themes from the storage medium.
|
||||
/// Gets all the themes from the storage medium, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching themes are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="ThemeEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<ThemeEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="ThemeEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<ThemeEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Themes.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<ThemeEntity> query = _luminaDbContext.Themes;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the themes.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<ThemeEntity> allThemes = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<ThemeEntity>
|
||||
{
|
||||
Data = allThemes,
|
||||
CurrentPage = 1,
|
||||
PerPage = allThemes.Count,
|
||||
Count = allThemes.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(theme => theme.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<ThemeEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<ThemeEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -3,11 +3,15 @@ using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Users;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -38,8 +42,8 @@ internal sealed class UserRepository : IUserRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(UserEntity user, CancellationToken cancellationToken)
|
||||
{
|
||||
bool userExists = await _luminaDbContext.Users.AnyAsync(repositoryUser => repositoryUser.Id == user.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (userExists)
|
||||
bool doesUserExist = await _luminaDbContext.Users.AnyAsync(repositoryUser => repositoryUser.Id == user.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (doesUserExist)
|
||||
return Errors.Users.UserAlreadyExists;
|
||||
|
||||
_luminaDbContext.Users.Add(user);
|
||||
@@ -47,13 +51,54 @@ internal sealed class UserRepository : IUserRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all users.
|
||||
/// Gets all the users, or a page of them when the pagination data is provided.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFilter">The type of the filter carrying the criteria used to filter the results.</typeparam>
|
||||
/// <param name="paginationData">The pagination data that includes the current page and the number of items per page to retrieve. If <see langword="null"/>, all the matching users are returned.</param>
|
||||
/// <param name="sortBy">The name of the field by which to sort the results.</param>
|
||||
/// <param name="sortOrder">The direction in which to sort the results.</param>
|
||||
/// <param name="filterModel">The model containing the parameters used to filter the results.</param>
|
||||
/// <param name="shouldIncludeNavigationProperties">Whether the navigation properties of the entities should be loaded together with the entities themselves. Pass <see langword="false"/> to retrieve only the data stored directly on the entity rows.</param>
|
||||
/// <param name="shouldTrackEntities">Whether the retrieved entities should be tracked by the persistence medium, so that changes to them can be saved. Pass <see langword="false"/> for read-only scenarios, to avoid the tracking overhead.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="UserEntity"/>, or an error.</returns>
|
||||
public async Task<Result<IEnumerable<UserEntity>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a paginated result of <see cref="UserEntity"/>, or an error.</returns>
|
||||
public async Task<Result<PaginatedResultDto<UserEntity>>> GetAllAsync<TFilter>(PaginationDataDto? paginationData = null, string? sortBy = null, SortOrder? sortOrder = null, TFilter? filterModel = null, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default) where TFilter : BaseFilterDto
|
||||
{
|
||||
return await _luminaDbContext.Users.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
IQueryable<UserEntity> query = _luminaDbContext.Users;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
|
||||
// If no pagination was requested, return all the users.
|
||||
if (paginationData is null)
|
||||
{
|
||||
IReadOnlyList<UserEntity> allUsers = await query.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return new PaginatedResultDto<UserEntity>
|
||||
{
|
||||
Data = allUsers,
|
||||
CurrentPage = 1,
|
||||
PerPage = allUsers.Count,
|
||||
Count = allUsers.Count,
|
||||
NumberOfPages = 1
|
||||
};
|
||||
}
|
||||
|
||||
int count = await query.Select(user => user.Id).CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
int numberOfPages = (int)Math.Ceiling((double)count / paginationData.PerPage);
|
||||
int currentPage = Math.Min(paginationData.CurrentPage, Math.Max(1, numberOfPages)); // Make sure current page doesn't exceed maximum number of pages.
|
||||
|
||||
IReadOnlyList<UserEntity> paginatedResult = await query
|
||||
.Skip((currentPage - 1) * paginationData.PerPage)
|
||||
.Take(paginationData.PerPage)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new PaginatedResultDto<UserEntity>
|
||||
{
|
||||
Data = paginatedResult,
|
||||
CurrentPage = currentPage,
|
||||
PerPage = paginationData.PerPage,
|
||||
Count = count,
|
||||
NumberOfPages = numberOfPages
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,10 +140,10 @@ internal sealed class UserRepository : IUserRepository
|
||||
if (foundUser is null)
|
||||
return Errors.Users.UserDoesNotExist;
|
||||
|
||||
// update scalar properties
|
||||
// Update scalar properties.
|
||||
_luminaDbContext.Entry(foundUser).CurrentValues.SetValues(data);
|
||||
|
||||
// update user permissions
|
||||
// Update user permissions.
|
||||
List<UserPermissionEntity> existingPermissions = [.. foundUser.UserPermissions];
|
||||
foreach (UserPermissionEntity permission in existingPermissions)
|
||||
_luminaDbContext.UserPermissions.Remove(permission);
|
||||
@@ -114,7 +159,7 @@ internal sealed class UserRepository : IUserRepository
|
||||
});
|
||||
}
|
||||
|
||||
// update user role
|
||||
// Update user role.
|
||||
if (foundUser.UserRole != null)
|
||||
_luminaDbContext.UserRoles.Remove(foundUser.UserRole);
|
||||
if (data.UserRole is not null)
|
||||
@@ -136,16 +181,22 @@ internal sealed class UserRepository : IUserRepository
|
||||
/// <param name="id">The id of the user to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="UserEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<UserEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<UserEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.Users
|
||||
.Include(user => user.Libraries)
|
||||
.ThenInclude(library => library.ContentLocations)
|
||||
.Include(user => user.UserPermissions)
|
||||
.ThenInclude(userPermission => userPermission.Permission)
|
||||
.Include(user => user.UserRole!.Role.RolePermissions)
|
||||
.ThenInclude(rolePermission => rolePermission.Permission)
|
||||
.FirstOrDefaultAsync(user => user.Id == id, cancellationToken)
|
||||
IQueryable<UserEntity> query = _luminaDbContext.Users;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
if (shouldIncludeNavigationProperties)
|
||||
{
|
||||
query = query
|
||||
.Include(user => user.Libraries)
|
||||
.ThenInclude(library => library.ContentLocations)
|
||||
.Include(user => user.UserPermissions)
|
||||
.ThenInclude(userPermission => userPermission.Permission)
|
||||
.Include(user => user.UserRole!.Role.RolePermissions)
|
||||
.ThenInclude(rolePermission => rolePermission.Permission);
|
||||
}
|
||||
return await query.FirstOrDefaultAsync(user => user.Id == id, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -36,8 +37,8 @@ internal sealed class UserSettingsRepository : IUserSettingsRepository
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Created>> InsertAsync(UserSettingsEntity data, CancellationToken cancellationToken)
|
||||
{
|
||||
bool settingsExists = await _luminaDbContext.UserSettings.AnyAsync(settings => settings.UserId == data.UserId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (settingsExists)
|
||||
bool doesSettingsExist = await _luminaDbContext.UserSettings.AnyAsync(settings => settings.UserId == data.UserId, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (doesSettingsExist)
|
||||
return Errors.UserSettings.UserSettingsAlreadyExists;
|
||||
|
||||
_luminaDbContext.UserSettings.Add(data);
|
||||
@@ -50,10 +51,12 @@ internal sealed class UserSettingsRepository : IUserSettingsRepository
|
||||
/// <param name="id">The Id of the user settings to get.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the <see cref="UserSettingsEntity"/> identified by <paramref name="id"/>, or an error.</returns>
|
||||
public async Task<Result<UserSettingsEntity?>> GetByIdAsync(Guid id, CancellationToken cancellationToken)
|
||||
public async Task<Result<UserSettingsEntity?>> GetByIdAsync(Guid id, bool shouldIncludeNavigationProperties = true, bool shouldTrackEntities = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _luminaDbContext.UserSettings
|
||||
.FirstOrDefaultAsync(settings => settings.Id == id, cancellationToken)
|
||||
IQueryable<UserSettingsEntity> query = _luminaDbContext.UserSettings;
|
||||
if (!shouldTrackEntities)
|
||||
query = query.AsNoTracking();
|
||||
return await query.FirstOrDefaultAsync(settings => settings.Id == id, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user