Change editor rule
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
using ErrorOr;
|
||||
|
||||
namespace Musify.Application.Shared;
|
||||
|
||||
public static class AppErrors
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
public static Error NotFound(string entity, object id) =>
|
||||
Error.NotFound($"{entity}.NotFound", $"{entity} {id} was not found.");
|
||||
public static class AppErrors
|
||||
{
|
||||
public static Error NotFound(string entity, object id) =>
|
||||
Error.NotFound($"{entity}.NotFound", $"{entity} {id} was not found.");
|
||||
|
||||
public static Error Forbidden(string entity, object id) =>
|
||||
Error.Forbidden($"{entity}.Forbidden", $"You do not have permission to modify {entity} {id}.");
|
||||
public static Error Forbidden(string entity, object id) =>
|
||||
Error.Forbidden($"{entity}.Forbidden", $"You do not have permission to modify {entity} {id}.");
|
||||
|
||||
public static Error Conflict(string code, string description) =>
|
||||
Error.Conflict(code, description);
|
||||
public static Error Conflict(string code, string description) =>
|
||||
Error.Conflict(code, description);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
namespace Musify.Application.Shared;
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
public record ImageSize(
|
||||
string SavePath,
|
||||
int Width,
|
||||
int Height);
|
||||
|
||||
public record ImageSize(
|
||||
string SavePath,
|
||||
int Width,
|
||||
int Height);
|
||||
|
||||
public record ImageSizes(
|
||||
ImageSize Small,
|
||||
ImageSize Medium,
|
||||
ImageSize Large);
|
||||
public record ImageSizes(
|
||||
ImageSize Small,
|
||||
ImageSize Medium,
|
||||
ImageSize Large);
|
||||
}
|
||||
|
||||
@@ -3,26 +3,27 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Musify.Domain.Abstractions;
|
||||
using Musify.Domain.ValueObjects;
|
||||
|
||||
namespace Musify.Application.Shared;
|
||||
|
||||
public static class OwnershipExtensions
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
/// <summary>
|
||||
/// Loads an active, tracked entity owned by <paramref name="userId"/>: NotFound when it is missing
|
||||
/// or not active, Forbidden when someone else owns it.
|
||||
/// </summary>
|
||||
public static async Task<ErrorOr<T>> FindOwnedAsync<T>(
|
||||
this DbSet<T> set, Guid id, long userId, CancellationToken cancellationToken)
|
||||
where T : class, IOwnedEntity, IHasLifeCycle
|
||||
public static class OwnershipExtensions
|
||||
{
|
||||
var entity = await set.SingleOrDefaultAsync(
|
||||
e => e.Id == id && e.LifeCycleStatus == LifeCycleStatus.Active, cancellationToken);
|
||||
/// <summary>
|
||||
/// Loads an active, tracked entity owned by <paramref name="userId"/>: NotFound when it is missing
|
||||
/// or not active, Forbidden when someone else owns it.
|
||||
/// </summary>
|
||||
public static async Task<ErrorOr<T>> FindOwnedAsync<T>(
|
||||
this DbSet<T> set, Guid id, long userId, CancellationToken cancellationToken)
|
||||
where T : class, IOwnedEntity, IHasLifeCycle
|
||||
{
|
||||
var entity = await set.SingleOrDefaultAsync(
|
||||
e => e.Id == id && e.LifeCycleStatus == LifeCycleStatus.Active, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
return AppErrors.NotFound(typeof(T).Name, id);
|
||||
if (entity == null)
|
||||
return AppErrors.NotFound(typeof(T).Name, id);
|
||||
|
||||
return entity.OwnerUserId == userId
|
||||
? entity
|
||||
: AppErrors.Forbidden(typeof(T).Name, id);
|
||||
return entity.OwnerUserId == userId
|
||||
? entity
|
||||
: AppErrors.Forbidden(typeof(T).Name, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,46 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Musify.Application.Shared;
|
||||
|
||||
public record PageRequest(int PageNumber = 1, int PageSize = 20)
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
public const int MaxPageSize = 100;
|
||||
}
|
||||
|
||||
public record PaginatedResponse<T>(
|
||||
IReadOnlyList<T> Items,
|
||||
int PageNumber,
|
||||
int PageSize,
|
||||
int PageCount,
|
||||
int TotalItemCount,
|
||||
bool HasNextPage,
|
||||
bool HasPreviousPage);
|
||||
|
||||
public static class PaginationExtensions
|
||||
{
|
||||
/// <summary>Counts and pages the same (already filtered, ordered and projected) query.</summary>
|
||||
public static async Task<PaginatedResponse<T>> ToPaginatedAsync<T>(
|
||||
this IQueryable<T> query, PageRequest page, CancellationToken cancellationToken)
|
||||
public record PageRequest(int PageNumber = 1, int PageSize = 20)
|
||||
{
|
||||
var pageNumber = Math.Max(page.PageNumber, 1);
|
||||
var pageSize = Math.Clamp(page.PageSize, 1, PageRequest.MaxPageSize);
|
||||
public const int MaxPageSize = 100;
|
||||
}
|
||||
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
var items = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
public record PaginatedResponse<T>(
|
||||
IReadOnlyList<T> Items,
|
||||
int PageNumber,
|
||||
int PageSize,
|
||||
int PageCount,
|
||||
int TotalItemCount,
|
||||
bool HasNextPage,
|
||||
bool HasPreviousPage);
|
||||
|
||||
var pageCount = (int)Math.Ceiling(totalCount / (double)pageSize);
|
||||
public static class PaginationExtensions
|
||||
{
|
||||
/// <summary>Counts and pages the same (already filtered, ordered and projected) query.</summary>
|
||||
public static async Task<PaginatedResponse<T>> ToPaginatedAsync<T>(
|
||||
this IQueryable<T> query, PageRequest page, CancellationToken cancellationToken)
|
||||
{
|
||||
var pageNumber = Math.Max(page.PageNumber, 1);
|
||||
var pageSize = Math.Clamp(page.PageSize, 1, PageRequest.MaxPageSize);
|
||||
|
||||
return new PaginatedResponse<T>(
|
||||
items,
|
||||
pageNumber,
|
||||
pageSize,
|
||||
pageCount,
|
||||
totalCount,
|
||||
HasNextPage: pageNumber < pageCount,
|
||||
HasPreviousPage: pageNumber > 1 && pageCount > 0);
|
||||
var totalCount = await query.CountAsync(cancellationToken);
|
||||
var items = await query
|
||||
.Skip((pageNumber - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var pageCount = (int)Math.Ceiling(totalCount / (double)pageSize);
|
||||
|
||||
return new PaginatedResponse<T>(
|
||||
items,
|
||||
pageNumber,
|
||||
pageSize,
|
||||
pageCount,
|
||||
totalCount,
|
||||
HasNextPage: pageNumber < pageCount,
|
||||
HasPreviousPage: pageNumber > 1 && pageCount > 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
namespace Musify.Application.Shared;
|
||||
|
||||
public static class StorageKey
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
public static string Combine(params string[] segments)
|
||||
public static class StorageKey
|
||||
{
|
||||
return string.Join('/', segments
|
||||
.Where(static s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(static s => s.Trim().Trim('/', '\\')));
|
||||
public static string Combine(params string[] segments)
|
||||
{
|
||||
return string.Join('/', segments
|
||||
.Where(static s => !string.IsNullOrWhiteSpace(s))
|
||||
.Select(static s => s.Trim().Trim('/', '\\')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
namespace Musify.Application.Shared;
|
||||
|
||||
public static class TextNormalizer
|
||||
namespace Musify.Application.Shared
|
||||
{
|
||||
public static string Normalize(string value) => value.Trim().ToUpperInvariant();
|
||||
public static class TextNormalizer
|
||||
{
|
||||
public static string Normalize(string value) => value.Trim().ToUpperInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user