Skip to content

Commit

Permalink
Sealed everything that could be sealed
Browse files Browse the repository at this point in the history
- Also made some classes use primary constructors
  • Loading branch information
Atulin committed Sep 14, 2024
1 parent bf09479 commit 39c9d50
Show file tree
Hide file tree
Showing 281 changed files with 433 additions and 501 deletions.
10 changes: 8 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ tab_width = 4
dotnet_diagnostic.rcs1260.severity = warning
roslynator_trailing_comma_style = omit_when_single_line
# Roslyn.Analyzers rules
dotnet_diagnostic.ASYNC0004.severity = silent
dotnet_diagnostic.ASYNC0004.severity = silent # use ConfigureAwait()
dotnet_diagnostic_ASYNC0001.severity = silent # suffix async classes with *Async

[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
dotnet_diagnostic.IAPI0006.severity = silent
dotnet_diagnostic.CA1852.severity = warning
dotnet_diagnostic.CA1852.severity = warning

[**/Migrations/**.cs]
generated_code = true


Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ogma3.Tests.Infrastructure.CustomValidators;

public class HashtagCountValidatorTest
public sealed class HashtagCountValidatorTest
{
private readonly HashtagCountValidator<int> _validator = new(3);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Ogma3.Tests.Infrastructure.CustomValidators;

public class HashtagLengthValidatorTest
public sealed class HashtagLengthValidatorTest
{
private readonly HashtagLengthValidator<int> _validator = new(10);

Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/ClubJoin/JoinClub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ CancellationToken cancellationToken
return TypedResults.Ok(true);
}

private record MemberOrBanned(bool IsMember, bool IsBanned);
private sealed record MemberOrBanned(bool IsMember, bool IsBanned);
}
12 changes: 5 additions & 7 deletions Ogma3/Api/V1/Clubs/ClubsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,29 @@ namespace Ogma3.Api.V1.Clubs;

[Route("api/[controller]", Name = nameof(ClubsController))]
[ApiController]
public class ClubsController : ControllerBase
public sealed class ClubsController(IMediator mediator) : ControllerBase
{
private readonly IMediator _mediator;
public ClubsController(IMediator mediator) => _mediator = mediator;

// GET: /api/clubs/user
[HttpGet("user")]
[Authorize]
public async Task<ActionResult<List<GetJoinedClubs.Response>>> GetUserClubs()
=> await _mediator.Send(new GetJoinedClubs.Query());
=> await mediator.Send(new GetJoinedClubs.Query());

// GET: /api/clubs/story/3
[HttpGet("story/{id:long}")]
public async Task<ActionResult<List<GetClubsWithStory.Result>>> GetClubsWithStory(long id)
=> await _mediator.Send(new GetClubsWithStory.Query(id));
=> await mediator.Send(new GetClubsWithStory.Query(id));

[HttpPost("user/ban")]
[IgnoreAntiforgeryToken]
public async Task<ActionResult<bool>> BanUser(BanUser.Command command)
=> await _mediator.Send(command);
=> await mediator.Send(command);

[HttpDelete("user/ban")]
[IgnoreAntiforgeryToken]
public async Task<ActionResult<bool>> UnbanUser(UnbanUser.Command command)
=> await _mediator.Send(command);
=> await mediator.Send(command);

// Don't delete or this whole controller will break
[HttpGet, OpenApiIgnore]
Expand Down
4 changes: 2 additions & 2 deletions Ogma3/Api/V1/Clubs/Commands/BanUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public async ValueTask<ActionResult<bool>> Handle(Command request, CancellationT
return Ok(true);
}

private record BannedUser(string UserName, EClubMemberRoles Role);
private sealed record BannedUser(string UserName, EClubMemberRoles Role);

private record Issuer(EClubMemberRoles Role, string UserName);
private sealed record Issuer(EClubMemberRoles Role, string UserName);
}
}
6 changes: 3 additions & 3 deletions Ogma3/Api/V1/Clubs/Commands/UnbanUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class UnbanUser
{
public sealed record Command(long UserId, long ClubId) : IRequest<ActionResult<bool>>;

public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
public sealed class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
{
public async ValueTask<ActionResult<bool>> Handle(Command request, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -63,8 +63,8 @@ public async ValueTask<ActionResult<bool>> Handle(Command request, CancellationT
return Ok(false);
}

private record BannedUser(string UserName, EClubMemberRoles Role);
private sealed record BannedUser(string UserName, EClubMemberRoles Role);

private record Issuer(EClubMemberRoles Role, string UserName);
private sealed record Issuer(EClubMemberRoles Role, string UserName);
}
}
10 changes: 2 additions & 8 deletions Ogma3/Api/V1/Clubs/Queries/GetClubsWithStory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ public static class GetClubsWithStory
{
public sealed record Query(long StoryId) : IRequest<ActionResult<List<Result>>>;

public class Handler : BaseHandler, IRequestHandler<Query, ActionResult<List<Result>>>
public sealed class Handler(ApplicationDbContext context) : BaseHandler, IRequestHandler<Query, ActionResult<List<Result>>>
{
private readonly ApplicationDbContext _context;

public Handler(ApplicationDbContext context)
{
_context = context;
}

public async ValueTask<ActionResult<List<Result>>> Handle(Query request, CancellationToken cancellationToken)
{
var clubs = await _context.Clubs
var clubs = await context.Clubs
.Where(c => c.Folders
.Any(f => f.Stories
.Any(s => s.Id == request.StoryId)))
Expand Down
14 changes: 4 additions & 10 deletions Ogma3/Api/V1/Clubs/Queries/GetJoinedClubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ public static class GetJoinedClubs
{
public sealed record Query : IRequest<ActionResult<List<Response>>>;

public class Handler : BaseHandler, IRequestHandler<Query, ActionResult<List<Response>>>
public sealed class Handler
(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Query, ActionResult<List<Response>>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IUserService userService)
{
_context = context;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<List<Response>>> Handle(Query request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

var clubs = await _context.Clubs
var clubs = await context.Clubs
.Where(c => c.ClubMembers.Any(cm => cm.MemberId == (long)_uid))
.OrderBy(c => c.Name)
.Select(c => new Response(c.Id, c.Name, c.Icon))
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/ErrorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Ogma3.Api.V1;

[Route("api/[controller]", Name = nameof(ErrorController))]
[ApiController]
public class ErrorController : ControllerBase
public sealed class ErrorController : ControllerBase
{
[HttpGet]
public ActionResult<Result> OnGet([FromQuery] int? code)
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Faqs/CreateFaq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static partial class CreateFaq
{
public sealed record Command(string Question, string Answer);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Faqs/UpdateFaq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static partial class UpdateFaq
public sealed record Command(long Id, string Question, string Answer);

[UsedImplicitly]
public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Folders/Commands/AddStoryToFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed record Response(long FolderId, long StoryId, DateTime Added, long
public static Response FromFolderStory(FolderStory fs) => new Response(fs.FolderId, fs.StoryId, fs.Added, fs.AddedById);
}

public class Handler(ApplicationDbContext context, IUserService userService)
public sealed class Handler(ApplicationDbContext context, IUserService userService)
: BaseHandler, IRequestHandler<Command, ActionResult<Response>>
{
private readonly long? _uid = userService.User?.GetNumericId();
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Folders/FoldersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ogma3.Api.V1.Folders;

[Route("api/[controller]", Name = nameof(FoldersController))]
[ApiController]
public class FoldersController(IMediator mediator) : ControllerBase
public sealed class FoldersController(IMediator mediator) : ControllerBase
{
// GET api/folders/5
[HttpGet("{id:long}")]
Expand Down
14 changes: 4 additions & 10 deletions Ogma3/Api/V1/Folders/Queries/GetFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ public static class GetFolder
{
public sealed record Query(long Id) : IRequest<ActionResult<List<Result>>>;

public class Handler : BaseHandler, IRequestHandler<Query, ActionResult<List<Result>>>
public sealed class Handler
(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Query, ActionResult<List<Result>>>
{
private readonly ApplicationDbContext _context;
private readonly long? _uid;

public Handler(ApplicationDbContext context, IUserService userService)
{
_context = context;
_uid = userService.User?.GetNumericId();
}
private readonly long? _uid = userService.User?.GetNumericId();

public async ValueTask<ActionResult<List<Result>>> Handle(Query request, CancellationToken cancellationToken)
{
if (_uid is null) return Unauthorized();

var folder = await _context.Folders
var folder = await context.Folders
.Where(f => f.ClubId == request.Id)
.Select(f => new Result(
f.Id,
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Quotes/CreateQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal static void CustomizeEndpoint(IEndpointConventionBuilder endpoint) => e

public sealed record Command(string Body, string Author);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Ratings/CreateRating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static partial class CreateRating
{
public sealed record Command(string Name, string Description, bool BlacklistedByDefault, byte Order, IFormFile Icon);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Ratings/UpdateRating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static partial class UpdateRating
{
public sealed record Command(long Id, string Name, string Description, bool BlacklistedByDefault, byte Order, IFormFile Icon);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Roles/CreateRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static partial class CreateRole
{
public sealed record Command(string Name, bool IsStaff, string Color, byte Order);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator() => RuleFor(r => r.Name).NotEmpty();
}
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Roles/UpdateRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static partial class UpdateRole
{
public sealed record Command(long Id, string Name, bool IsStaff, string Color, byte Order);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator() => RuleFor(r => r.Name).NotEmpty();
}
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Shelves/CreateShelf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed record Command
long Icon
);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Shelves/UpdateShelf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed record Command
long Icon
);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class SubscribeCommentsThread
{
public sealed record Command(long ThreadId) : IRequest<ActionResult<bool>>;

public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
public sealed class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
{
private readonly long? _uid = userService.User?.GetNumericId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class UnsubscribeCommentsThread
{
public sealed record Command(long ThreadId) : IRequest<ActionResult<bool>>;

public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
public sealed class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Command, ActionResult<bool>>
{
private readonly long? _uid = userService.User?.GetNumericId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class GetSubscriptionStatus
{
public sealed record Query(long ThreadId) : IRequest<ActionResult<bool>>;

public class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Query, ActionResult<bool>>
public sealed class Handler(ApplicationDbContext context, IUserService userService) : BaseHandler, IRequestHandler<Query, ActionResult<bool>>
{
private readonly long? _uid = userService.User?.GetNumericId();

Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Subscriptions/SubscriptionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Ogma3.Api.V1.Subscriptions;

[Route("api/[controller]", Name = nameof(SubscriptionsController))]
[ApiController]
public class SubscriptionsController(IMediator mediator) : ControllerBase
public sealed class SubscriptionsController(IMediator mediator) : ControllerBase
{

[HttpGet("thread")]
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Tags/CreateTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class CreateTag
{
public sealed record Command(string Name, string? Description, ETagNamespace? Namespace);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Api/V1/Tags/UpdateTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class UpdateTag
{
public sealed record Command(long Id, string? Name, string? Description, ETagNamespace? Namespace);

public class CommandValidator : AbstractValidator<Command>
public sealed class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
Expand Down
1 change: 1 addition & 0 deletions Ogma3/Areas/Admin/Api/V1/Cache/GetCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static partial class GetCache
[UsedImplicitly]
public sealed record Query;

// ReSharper disable once UnusedParameter.Local
private static async ValueTask<ReturnType> HandleAsync(Query _, IMemoryCache cache, CancellationToken __)
{
await Task.Yield();
Expand Down
1 change: 1 addition & 0 deletions Ogma3/Areas/Admin/Api/V1/Cache/PurgeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static partial class PurgeCache
{
public sealed record Query;

// ReSharper disable once UnusedParameter.Local
private static async ValueTask<ReturnType> HandleAsync(Query _, IMemoryCache cache, ILogger<Query> logger, CancellationToken __)
{
await Task.Yield();
Expand Down
2 changes: 1 addition & 1 deletion Ogma3/Areas/Admin/Pages/ContentBlock.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace Ogma3.Areas.Admin.Pages;

[Authorize(Roles = $"{RoleNames.Admin},{RoleNames.Moderator}")]
public class ContentBlock : PageModel
public sealed class ContentBlock : PageModel
{
private readonly ApplicationDbContext _context;
public ContentBlock(ApplicationDbContext context) => _context = context;
Expand Down
Loading

0 comments on commit 39c9d50

Please sign in to comment.