mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-29 18:21:08 +00:00
feat: Code Cleanup
This commit is contained in:
parent
b54d4f4de5
commit
6d63df1eda
@ -38,7 +38,7 @@ if (builder.Environment.IsProduction())
|
||||
.AddSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")!)
|
||||
.AddRedis(builder.Configuration["RedisHostName"]!, "Redis")
|
||||
.AddRabbitMQ(
|
||||
rabbitConnectionString: $"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}",
|
||||
$"amqp://{rabbitUser}:{rabbitPass}@{rabbitHost}",
|
||||
name: "RabbitMQ");
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@ public static class QueryableExtensions
|
||||
|
||||
var sorted = GetFirstOrderLevelQuery(query, sort.Parameters.First(), fieldExpressions);
|
||||
|
||||
for (int i = 1; i < sort.Parameters.Count; i++)
|
||||
for (var i = 1; i < sort.Parameters.Count; i++)
|
||||
{
|
||||
sorted = GetMultiLevelOrderedQuery(sorted, sort.Parameters[i], fieldExpressions);
|
||||
}
|
||||
@ -38,7 +38,7 @@ public static class QueryableExtensions
|
||||
|
||||
private static IOrderedQueryable<TEntity> GetFirstOrderLevelQuery<TEntity>(
|
||||
IQueryable<TEntity> query,
|
||||
SortParameter @param,
|
||||
SortParameter param,
|
||||
Dictionary<string, Expression<Func<TEntity, object>>> fieldExpressions)
|
||||
{
|
||||
if (!fieldExpressions.TryGetValue(param.ParameterName, out var fieldExpression))
|
||||
@ -56,7 +56,7 @@ public static class QueryableExtensions
|
||||
|
||||
private static IOrderedQueryable<TEntity> GetMultiLevelOrderedQuery<TEntity>(
|
||||
IOrderedQueryable<TEntity> query,
|
||||
SortParameter @param,
|
||||
SortParameter param,
|
||||
Dictionary<string, Expression<Func<TEntity, object>>> fieldExpressions)
|
||||
{
|
||||
if (!fieldExpressions.TryGetValue(param.ParameterName, out var fieldExpression))
|
||||
|
@ -43,7 +43,7 @@ public static class ServiceCollectionExtensions
|
||||
{
|
||||
services.AddScoped<ISortingExpressionProvider<TenantViewModel, Tenant>, TenantViewModelSortProvider>();
|
||||
services.AddScoped<ISortingExpressionProvider<UserViewModel, User>, UserViewModelSortProvider>();
|
||||
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CleanArchitecture.Application.SortProviders;
|
||||
using CleanArchitecture.Application.ViewModels;
|
||||
using CleanArchitecture.Application.ViewModels.Sorting;
|
||||
using CleanArchitecture.Application.ViewModels.Tenants;
|
||||
|
@ -10,11 +10,13 @@ public interface IUserService
|
||||
{
|
||||
public Task<UserViewModel?> GetUserByUserIdAsync(Guid userId);
|
||||
public Task<UserViewModel?> GetCurrentUserAsync();
|
||||
|
||||
public Task<PagedResult<UserViewModel>> GetAllUsersAsync(
|
||||
PageQuery query,
|
||||
bool includeDeleted,
|
||||
string searchTerm = "",
|
||||
SortQuery? sortQuery = null);
|
||||
|
||||
public Task<Guid> CreateUserAsync(CreateUserViewModel user);
|
||||
public Task UpdateUserAsync(UpdateUserViewModel user);
|
||||
public Task DeleteUserAsync(Guid userId);
|
||||
|
@ -15,8 +15,8 @@ namespace CleanArchitecture.Application.Queries.Tenants.GetAll;
|
||||
public sealed class GetAllTenantsQueryHandler :
|
||||
IRequestHandler<GetAllTenantsQuery, PagedResult<TenantViewModel>>
|
||||
{
|
||||
private readonly ITenantRepository _tenantRepository;
|
||||
private readonly ISortingExpressionProvider<TenantViewModel, Tenant> _sortingExpressionProvider;
|
||||
private readonly ITenantRepository _tenantRepository;
|
||||
|
||||
public GetAllTenantsQueryHandler(
|
||||
ITenantRepository tenantRepository,
|
||||
|
@ -15,8 +15,8 @@ namespace CleanArchitecture.Application.Queries.Users.GetAll;
|
||||
public sealed class GetAllUsersQueryHandler :
|
||||
IRequestHandler<GetAllUsersQuery, PagedResult<UserViewModel>>
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly ISortingExpressionProvider<UserViewModel, User> _sortingExpressionProvider;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public GetAllUsersQueryHandler(
|
||||
IUserRepository userRepository,
|
||||
|
@ -3,7 +3,6 @@ using System.Threading.Tasks;
|
||||
using CleanArchitecture.Application.Interfaces;
|
||||
using CleanArchitecture.Application.Queries.Tenants.GetAll;
|
||||
using CleanArchitecture.Application.Queries.Tenants.GetTenantById;
|
||||
using CleanArchitecture.Application.SortProviders;
|
||||
using CleanArchitecture.Application.ViewModels;
|
||||
using CleanArchitecture.Application.ViewModels.Sorting;
|
||||
using CleanArchitecture.Application.ViewModels.Tenants;
|
||||
|
@ -12,9 +12,9 @@ public sealed class TenantViewModelSortProvider : ISortingExpressionProvider<Ten
|
||||
private static readonly Dictionary<string, Expression<Func<Tenant, object>>> s_expressions = new()
|
||||
{
|
||||
{ "id", tenant => tenant.Id },
|
||||
{ "name", tenant => tenant.Name },
|
||||
{ "name", tenant => tenant.Name }
|
||||
};
|
||||
|
||||
|
||||
public Dictionary<string, Expression<Func<Tenant, object>>> GetSortingExpressions()
|
||||
{
|
||||
return s_expressions;
|
||||
|
@ -19,7 +19,7 @@ public sealed class UserViewModelSortProvider : ISortingExpressionProvider<UserV
|
||||
{ "role", user => user.Role },
|
||||
{ "status", user => user.Status }
|
||||
};
|
||||
|
||||
|
||||
public Dictionary<string, Expression<Func<User, object>>> GetSortingExpressions()
|
||||
{
|
||||
return s_expressions;
|
||||
|
@ -6,34 +6,9 @@ namespace CleanArchitecture.Application.ViewModels.Sorting;
|
||||
|
||||
public sealed class SortQuery
|
||||
{
|
||||
private readonly struct QueryInfo
|
||||
{
|
||||
public readonly short PlusSignIndex;
|
||||
public readonly short MinusSignIndex;
|
||||
public readonly short FirstSpaceIndex;
|
||||
public readonly short OpeningBracketIndex;
|
||||
public readonly short ClosingBracketIndex;
|
||||
|
||||
public QueryInfo(
|
||||
short plusSignIndex,
|
||||
short minusSignIndex,
|
||||
short firstSpaceIndex,
|
||||
short openingBracketIndex,
|
||||
short closingBracketIndex)
|
||||
{
|
||||
PlusSignIndex = plusSignIndex;
|
||||
MinusSignIndex = minusSignIndex;
|
||||
FirstSpaceIndex = firstSpaceIndex;
|
||||
OpeningBracketIndex = openingBracketIndex;
|
||||
ClosingBracketIndex = closingBracketIndex;
|
||||
}
|
||||
}
|
||||
|
||||
private string? _query = string.Empty;
|
||||
|
||||
private ReadOnlyCollection<SortParameter> _parameters = new(Array.Empty<SortParameter>());
|
||||
|
||||
public ReadOnlyCollection<SortParameter> Parameters => _parameters;
|
||||
public ReadOnlyCollection<SortParameter> Parameters { get; private set; } = new(Array.Empty<SortParameter>());
|
||||
|
||||
[FromQuery(Name = "order_by")]
|
||||
public string? Query
|
||||
@ -42,7 +17,7 @@ public sealed class SortQuery
|
||||
set
|
||||
{
|
||||
_query = value;
|
||||
_parameters = ParseQuery(_query);
|
||||
Parameters = ParseQuery(_query);
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,7 +43,7 @@ public sealed class SortQuery
|
||||
var @params = value.Split(',');
|
||||
var parsedParams = new SortParameter[@params.Length];
|
||||
|
||||
for (int i = 0; i < @params.Length; i++)
|
||||
for (var i = 0; i < @params.Length; i++)
|
||||
{
|
||||
parsedParams[i] = GetParam(@params[i]);
|
||||
}
|
||||
@ -114,7 +89,8 @@ public sealed class SortQuery
|
||||
{
|
||||
return new SortParameter(paramName, SortOrder.Ascending);
|
||||
}
|
||||
else if (orderName == "desc" || orderName == "descending")
|
||||
|
||||
if (orderName == "desc" || orderName == "descending")
|
||||
{
|
||||
return new SortParameter(paramName, SortOrder.Descending);
|
||||
}
|
||||
@ -137,10 +113,8 @@ public sealed class SortQuery
|
||||
{
|
||||
return new SortParameter(value[1..], order);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new SortParameter(value[..indicatorIndex], order);
|
||||
}
|
||||
|
||||
return new SortParameter(value[..indicatorIndex], order);
|
||||
}
|
||||
|
||||
private static SortParameter GetSortParamFromFunctionalStyle(string value, QueryInfo info)
|
||||
@ -158,7 +132,8 @@ public sealed class SortQuery
|
||||
{
|
||||
return new SortParameter(param, SortOrder.Ascending);
|
||||
}
|
||||
else if (value.StartsWith("desc(") || value.StartsWith("descending("))
|
||||
|
||||
if (value.StartsWith("desc(") || value.StartsWith("descending("))
|
||||
{
|
||||
return new SortParameter(param, SortOrder.Descending);
|
||||
}
|
||||
@ -288,7 +263,7 @@ public sealed class SortQuery
|
||||
|
||||
private static int FindNextNonWhitespaceCharacter(string value, int startIndex)
|
||||
{
|
||||
for (int i = startIndex; i < value.Length; i++)
|
||||
for (var i = startIndex; i < value.Length; i++)
|
||||
{
|
||||
if (!char.IsWhiteSpace(value[i]))
|
||||
{
|
||||
@ -298,4 +273,27 @@ public sealed class SortQuery
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private readonly struct QueryInfo
|
||||
{
|
||||
public readonly short PlusSignIndex;
|
||||
public readonly short MinusSignIndex;
|
||||
public readonly short FirstSpaceIndex;
|
||||
public readonly short OpeningBracketIndex;
|
||||
public readonly short ClosingBracketIndex;
|
||||
|
||||
public QueryInfo(
|
||||
short plusSignIndex,
|
||||
short minusSignIndex,
|
||||
short firstSpaceIndex,
|
||||
short openingBracketIndex,
|
||||
short closingBracketIndex)
|
||||
{
|
||||
PlusSignIndex = plusSignIndex;
|
||||
MinusSignIndex = minusSignIndex;
|
||||
FirstSpaceIndex = firstSpaceIndex;
|
||||
OpeningBracketIndex = openingBracketIndex;
|
||||
ClosingBracketIndex = closingBracketIndex;
|
||||
}
|
||||
}
|
||||
}
|
@ -52,7 +52,7 @@ public sealed class RabbitMqHandler : BackgroundService
|
||||
{
|
||||
if (!_configuration.Enabled)
|
||||
{
|
||||
_logger.LogInformation($"RabbitMQ is disabled. Skipping the creation of exchange {exchangeName}.");
|
||||
_logger.LogInformation("RabbitMQ is disabled. Skipping the creation of exchange {exchangeName}.", exchangeName);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -159,7 +159,7 @@ public sealed class RabbitMqHandler : BackgroundService
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Error while handling event in queue {ea.RoutingKey}");
|
||||
_logger.LogError(ex, "Error while handling event in queue {RoutingKey}", ea.RoutingKey);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user