Style: C# 12 Primary constructor

This commit is contained in:
Kamigen
2024-03-30 20:59:17 -04:00
parent 786c65410d
commit 8f58311283
50 changed files with 267 additions and 449 deletions

View File

@@ -3,11 +3,10 @@ using Hutopy.Application.Common.Interfaces;
using Hutopy.Infrastructure.Data;
using Hutopy.Web.Services;
using Microsoft.AspNetCore.Mvc;
using NSwag;
using NSwag.Generation.Processors.Security;
namespace Microsoft.Extensions.DependencyInjection;
namespace Hutopy.Web;
public static class DependencyInjection
{

View File

@@ -9,8 +9,8 @@ public class JoinUs : EndpointGroupBase
app.MapGroup(this)
.MapPost(CreateFutureCreator);
}
public Task<int> CreateFutureCreator(ISender sender, CreateFutureCreatorCommand command)
private static Task<int> CreateFutureCreator(ISender sender, CreateFutureCreatorCommand command)
{
return sender.Send(command);
}

View File

@@ -10,7 +10,7 @@ public class Stripe : EndpointGroupBase
.MapPost(CreateSessionCheckout);
}
public Task<string> CreateSessionCheckout(ISender sender, CreateSessionCheckoutCommand command)
private static Task<string> CreateSessionCheckout(ISender sender, CreateSessionCheckoutCommand command)
{
return sender.Send(command);
}

View File

@@ -20,31 +20,31 @@ public class TodoItems : EndpointGroupBase
.MapDelete(DeleteTodoItem, "{id}");
}
public Task<PaginatedList<TodoItemBriefDto>> GetTodoItemsWithPagination(ISender sender, [AsParameters] GetTodoItemsWithPaginationQuery query)
private static Task<PaginatedList<TodoItemBriefDto>> GetTodoItemsWithPagination(ISender sender, [AsParameters] GetTodoItemsWithPaginationQuery query)
{
return sender.Send(query);
}
public Task<int> CreateTodoItem(ISender sender, CreateTodoItemCommand command)
private static Task<int> CreateTodoItem(ISender sender, CreateTodoItemCommand command)
{
return sender.Send(command);
}
public async Task<IResult> UpdateTodoItem(ISender sender, int id, UpdateTodoItemCommand command)
private static async Task<IResult> UpdateTodoItem(ISender sender, int id, UpdateTodoItemCommand command)
{
if (id != command.Id) return Results.BadRequest();
await sender.Send(command);
return Results.NoContent();
}
public async Task<IResult> UpdateTodoItemDetail(ISender sender, int id, UpdateTodoItemDetailCommand command)
private static async Task<IResult> UpdateTodoItemDetail(ISender sender, int id, UpdateTodoItemDetailCommand command)
{
if (id != command.Id) return Results.BadRequest();
await sender.Send(command);
return Results.NoContent();
}
public async Task<IResult> DeleteTodoItem(ISender sender, int id)
private static async Task<IResult> DeleteTodoItem(ISender sender, int id)
{
await sender.Send(new DeleteTodoItemCommand(id));
return Results.NoContent();

View File

@@ -17,24 +17,24 @@ public class TodoLists : EndpointGroupBase
.MapDelete(DeleteTodoList, "{id}");
}
public Task<TodosVm> GetTodoLists(ISender sender)
private static Task<TodosVm> GetTodoLists(ISender sender)
{
return sender.Send(new GetTodosQuery());
return sender.Send(new GetTodosQuery());
}
public Task<int> CreateTodoList(ISender sender, CreateTodoListCommand command)
private static Task<int> CreateTodoList(ISender sender, CreateTodoListCommand command)
{
return sender.Send(command);
}
public async Task<IResult> UpdateTodoList(ISender sender, int id, UpdateTodoListCommand command)
private static async Task<IResult> UpdateTodoList(ISender sender, int id, UpdateTodoListCommand command)
{
if (id != command.Id) return Results.BadRequest();
await sender.Send(command);
return Results.NoContent();
}
public async Task<IResult> DeleteTodoList(ISender sender, int id)
private static async Task<IResult> DeleteTodoList(ISender sender, int id)
{
await sender.Send(new DeleteTodoListCommand(id));
return Results.NoContent();

View File

@@ -11,7 +11,7 @@ public class WeatherForecasts : EndpointGroupBase
.MapGet(GetWeatherForecasts);
}
public async Task<IEnumerable<WeatherForecast>> GetWeatherForecasts(ISender sender)
private static async Task<IEnumerable<WeatherForecast>> GetWeatherForecasts(ISender sender)
{
return await sender.Send(new GetWeatherForecastsQuery());
}

View File

@@ -11,8 +11,8 @@ public class CustomExceptionHandler : IExceptionHandler
public CustomExceptionHandler()
{
// Register known exception types and handlers.
_exceptionHandlers = new()
{
_exceptionHandlers = new Dictionary<Type, Func<HttpContext, Exception, Task>>
{
{ typeof(ValidationException), HandleValidationException },
{ typeof(NotFoundException), HandleNotFoundException },
{ typeof(UnauthorizedAccessException), HandleUnauthorizedAccessException },
@@ -24,16 +24,14 @@ public class CustomExceptionHandler : IExceptionHandler
{
var exceptionType = exception.GetType();
if (_exceptionHandlers.ContainsKey(exceptionType))
{
await _exceptionHandlers[exceptionType].Invoke(httpContext, exception);
return true;
}
if (!_exceptionHandlers.TryGetValue(exceptionType, out Func<HttpContext, Exception, Task>? value)) return false;
await value.Invoke(httpContext, exception);
return true;
return false;
}
private async Task HandleValidationException(HttpContext httpContext, Exception ex)
private static async Task HandleValidationException(HttpContext httpContext, Exception ex)
{
var exception = (ValidationException)ex;
@@ -46,7 +44,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleNotFoundException(HttpContext httpContext, Exception ex)
private static async Task HandleNotFoundException(HttpContext httpContext, Exception ex)
{
var exception = (NotFoundException)ex;
@@ -61,7 +59,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleUnauthorizedAccessException(HttpContext httpContext, Exception ex)
private static async Task HandleUnauthorizedAccessException(HttpContext httpContext, Exception ex)
{
httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
@@ -73,7 +71,7 @@ public class CustomExceptionHandler : IExceptionHandler
});
}
private async Task HandleForbiddenAccessException(HttpContext httpContext, Exception ex)
private static async Task HandleForbiddenAccessException(HttpContext httpContext, Exception ex)
{
httpContext.Response.StatusCode = StatusCodes.Status403Forbidden;

View File

@@ -4,7 +4,7 @@ namespace Hutopy.Web.Infrastructure;
public static class MethodInfoExtensions
{
public static bool IsAnonymous(this MethodInfo method)
private static bool IsAnonymous(this MethodInfo method)
{
var invalidChars = new[] { '<', '>' };
return method.Name.Any(invalidChars.Contains);
@@ -15,4 +15,4 @@ public static class MethodInfoExtensions
if (input.Method.IsAnonymous())
throw new ArgumentException("The endpoint name must be specified when using anonymous handlers.");
}
}
}

View File

@@ -1,4 +1,7 @@
using Hutopy.Application;
using Hutopy.Infrastructure;
using Hutopy.Infrastructure.Data;
using Hutopy.Web;
var builder = WebApplication.CreateBuilder(args);
@@ -61,4 +64,4 @@ app.MapEndpoints();
app.Run();
public partial class Program { }
public abstract partial class Program { }

View File

@@ -4,14 +4,9 @@ using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Web.Services;
public class CurrentUser : IUser
public class CurrentUser(
IHttpContextAccessor httpContextAccessor)
: IUser
{
private readonly IHttpContextAccessor _httpContextAccessor;
public CurrentUser(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string? Id => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
public string? Id => httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
}