Skip to content

Commit

Permalink
Fix analysis (#146)
Browse files Browse the repository at this point in the history
* Fix analysis errors

* fix warnings
  • Loading branch information
patmoreau authored Dec 17, 2023
1 parent 674a534 commit c2a059f
Show file tree
Hide file tree
Showing 27 changed files with 53 additions and 103 deletions.
6 changes: 3 additions & 3 deletions src/Api/Holefeeder.Api/ErrorHandling/CustomErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private static async Task WriteResponse(HttpContext httpContext, bool includeDet
}

private static ProblemDetails CreateProblemDetails(int statusCode, string title, string? details) =>
new ProblemDetails { Status = statusCode, Title = title, Detail = details };
new() { Status = statusCode, Title = title, Detail = details };

private static ProblemDetails CreateValidationProblemDetails(IDictionary<string, string[]> errors) =>
new ValidationProblemDetails(errors) { Status = StatusCodes.Status422UnprocessableEntity };
private static ValidationProblemDetails CreateValidationProblemDetails(IDictionary<string, string[]> errors) =>
new(errors) { Status = StatusCodes.Status422UnprocessableEntity };
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IH
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "oauth2"}
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "oauth2" }
},
new List<string>()
}
Expand All @@ -88,13 +88,16 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IH
return services;
}

static readonly string[] serviceTags = new[] { "holefeeder", "api", "service" };
static readonly string[] databaseTags = new[] { "holefeeder", "api", "mysql" };

public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
{
services
.AddHealthChecks()
.AddCheck("api", () => HealthCheckResult.Healthy(), new[] { "holefeeder", "api", "service" })
.AddCheck("api", () => HealthCheckResult.Healthy(), serviceTags)
.AddMySql(configuration.GetConnectionString(BudgetingConnectionStringBuilder.BUDGETING_CONNECTION_STRING)!,
name: "budgeting-db-check", tags: new[] { "holefeeder", "api", "mysql" });
name: "budgeting-db-check", tags: databaseTags);

return services;
}
Expand Down
11 changes: 2 additions & 9 deletions src/Api/Holefeeder.Api/Swagger/QueryRequestOperationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,8 @@ public class QueryRequestOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation == null)
{
throw new ArgumentNullException(nameof(operation));
}

if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(operation);
ArgumentNullException.ThrowIfNull(context);

operation.Parameters ??= new List<OpenApiParameter>();

Expand Down
5 changes: 1 addition & 4 deletions src/Api/Holefeeder.Application/Context/BudgetingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ public async Task RollbackWorkAsync(CancellationToken cancellationToken)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException(nameof(modelBuilder));
}
ArgumentNullException.ThrowIfNull(modelBuilder);

new CategoryEntityTypeConfiguration().Configure(modelBuilder.Entity<Category>());
new AccountEntityTypeConfiguration().Configure(modelBuilder.Entity<Account>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static AccountViewModel MapToAccountViewModel(Account entity) =>
entity.Transactions.Count,
entity.OpenBalance +
entity.Transactions.Sum(x => x.Amount * x.Category?.Type.Multiplier * entity.Type.Multiplier ?? 0),
entity.Transactions.Any() ? entity.Transactions.Max(x => x.Date) : entity.OpenDate,
entity.Transactions.Count > 0 ? entity.Transactions.Max(x => x.Date) : entity.OpenDate,
entity.Description,
entity.Favorite,
entity.Inactive);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void UpdateProgress(Guid requestId, ImportDataStatusDto response) =>

private async Task ImportAccountsAsync(InternalRequest request, CancellationToken cancellationToken)
{
if (!request.Data.Accounts.Any())
if (request.Data.Accounts.Length == 0)
{
return;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ private async Task ImportAccountsAsync(InternalRequest request, CancellationToke

private async Task ImportCategoriesAsync(InternalRequest request, CancellationToken cancellationToken)
{
if (!request.Data.Categories.Any())
if (request.Data.Categories.Length == 0)
{
return;
}
Expand Down Expand Up @@ -241,7 +241,7 @@ private async Task ImportCategoriesAsync(InternalRequest request, CancellationTo

private async Task ImportCashflowsAsync(InternalRequest request, CancellationToken cancellationToken)
{
if (!request.Data.Cashflows.Any())
if (request.Data.Cashflows.Length == 0)
{
return;
}
Expand Down Expand Up @@ -319,7 +319,7 @@ private async Task ImportCashflowsAsync(InternalRequest request, CancellationTok

private async Task ImportTransactionsAsync(InternalRequest request, CancellationToken cancellationToken)
{
if (!request.Data.Transactions.Any())
if (request.Data.Transactions.Length == 0)
{
return;
}
Expand Down Expand Up @@ -399,10 +399,10 @@ public Validator() =>
RuleFor(command => command.Data)
.NotNull()
.Must(data =>
data.Accounts.Any() ||
data.Categories.Any() ||
data.Cashflows.Any() ||
data.Transactions.Any())
data.Accounts.Length > 0 ||
data.Categories.Length > 0 ||
data.Cashflows.Length > 0 ||
data.Transactions.Length > 0)
.WithMessage("must contain at least 1 array of accounts|categories|cashflows|transactions");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<IEnumerable<TagDto>> Handle(Request request, CancellationToken
.ToListAsync(cancellationToken: cancellationToken);

var results = transactions.SelectMany(transaction => transaction.Tags,
(transaction, tag) => new { Tag = tag })
(_, tag) => new { Tag = tag })
.GroupBy(x => new { x.Tag })
.Select(group => new TagDto(group.Key.Tag, group.Count()))
.OrderByDescending(x => x.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace Holefeeder.Application.Features.Tags.Queries;

public partial class GetTagsWithCount
{
internal partial record Request() : IRequest<IEnumerable<TagDto>>;
internal record Request : IRequest<IEnumerable<TagDto>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public Handler(IUserContext userContext, BudgetingContext context)

await _context.Transactions.AddAsync(transactionTo, cancellationToken);

if (errors.Any())
if (errors.Count > 0)
{
throw new ValidationException("Transfer error",
errors.Select(x => new ValidationFailure(x.Item1, x.Item2)));
}

return errors.Any() ? (Guid.Empty, Guid.Empty) : (transactionFrom.Id, transactionTo.Id);
return errors.Count > 0 ? (Guid.Empty, Guid.Empty) : (transactionFrom.Id, transactionTo.Id);
}

private async Task<Category> FirstCategoryAsync(string categoryName, CancellationToken cancellationToken) =>
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Holefeeder.Domain/Features/Accounts/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Account Close()
throw new AccountDomainException("Account already closed");
}

if (Cashflows.Any())
if (Cashflows.Count > 0)
{
throw new AccountDomainException("Account has active cashflows");
}
Expand All @@ -109,5 +109,5 @@ public Account Close()
public decimal CalculateBalance() =>
OpenBalance + Transactions.Sum(t => t.Amount * t.Category!.Type.Multiplier * Type.Multiplier);

public DateOnly CalculateLastTransactionDate() => Transactions.Any() ? Transactions.Max(t => t.Date) : OpenDate;
public DateOnly CalculateLastTransactionDate() => Transactions.Count > 0 ? Transactions.Max(t => t.Date) : OpenDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public required Guid CategoryId

public IReadOnlyCollection<Transaction> Transactions { get; init; } = new List<Transaction>();

public DateOnly? LastPaidDate => Transactions.Any() ? Transactions.Max(x => x.Date) : null;
public DateOnly? LastPaidDate => Transactions.Count > 0 ? Transactions.Max(x => x.Date) : null;

public DateOnly? LastCashflowDate => Transactions.Max(x => x.CashflowDate);

Expand Down
8 changes: 1 addition & 7 deletions src/Api/Holefeeder.FunctionalTests/Features/BaseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ public class BaseFeature : FeatureFixture
protected BaseFeature(ApiApplicationDriver apiApplicationDriver, ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
if (apiApplicationDriver == null)
{
throw new ArgumentNullException(nameof(apiApplicationDriver));
}

// Scope = apiApplicationDriver.Services.CreateScope();

ArgumentNullException.ThrowIfNull(apiApplicationDriver);
HttpClientDriver = apiApplicationDriver.CreateHttpClientDriver(testOutputHelper);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Holefeeder.FunctionalTests.Drivers;
using Holefeeder.FunctionalTests.StepDefinitions;
using CreateRequest = Holefeeder.Application.Features.StoreItems.Commands.CreateStoreItem.Request;
using ModifyRequest = Holefeeder.Application.Features.StoreItems.Commands.ModifyStoreItem.Request;

namespace Holefeeder.FunctionalTests.Features.StoreItems;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ public class ScenarioDeleteTransaction : HolefeederScenario
public ScenarioDeleteTransaction(ApiApplicationDriver applicationDriver, ITestOutputHelper testOutputHelper)
: base(applicationDriver, testOutputHelper)
{
if (applicationDriver == null)
{
throw new ArgumentNullException(nameof(applicationDriver));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public sealed class ScenarioMakePurchase : HolefeederScenario
public ScenarioMakePurchase(ApiApplicationDriver applicationDriver, ITestOutputHelper testOutputHelper)
: base(applicationDriver, testOutputHelper)
{
if (applicationDriver == null)
{
throw new ArgumentNullException(nameof(applicationDriver));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public class ScenarioModifyCashflow : HolefeederScenario
public ScenarioModifyCashflow(ApiApplicationDriver applicationDriver, ITestOutputHelper testOutputHelper)
: base(applicationDriver, testOutputHelper)
{
if (applicationDriver == null)
{
throw new ArgumentNullException(nameof(applicationDriver));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public class ScenarioModifyTransaction : HolefeederScenario
public ScenarioModifyTransaction(ApiApplicationDriver applicationDriver, ITestOutputHelper testOutputHelper)
: base(applicationDriver, testOutputHelper)
{
if (applicationDriver == null)
{
throw new ArgumentNullException(nameof(applicationDriver));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ public sealed class ScenarioPayCashflow : HolefeederScenario
public ScenarioPayCashflow(ApiApplicationDriver applicationDriver, ITestOutputHelper testOutputHelper)
: base(applicationDriver, testOutputHelper)
{
if (applicationDriver == null)
{
throw new ArgumentNullException(nameof(applicationDriver));
}
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ public TransactionStepDefinition(IHttpClientDriver httpClientDriver) : base(http

internal async Task MakesPurchase(MakePurchase.Request request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
ArgumentNullException.ThrowIfNull(request);

string json = JsonSerializer.Serialize(request);
await HttpClientDriver.SendPostRequestAsync(ApiResources.MakePurchase, json);
}

internal async Task PayACashflow(PayCashflow.Request request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
ArgumentNullException.ThrowIfNull(request);

string json = JsonSerializer.Serialize(request);
await HttpClientDriver.SendPostRequestAsync(ApiResources.PayCashflow, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddInfrastructure(this IServiceCollection services,
IConfiguration configuration)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}
ArgumentNullException.ThrowIfNull(configuration);

services.AddSingleton<BudgetingConnectionStringBuilder>(_ => new BudgetingConnectionStringBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static class WebApplicationExtensions

public static IApplicationBuilder MigrateDb(this IApplicationBuilder app)
{
if (app is null)
{
throw new ArgumentNullException(nameof(app));
}
ArgumentNullException.ThrowIfNull(app);

using IServiceScope scope = app.ApplicationServices.CreateScope();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class GetAccountsTests

public GetAccountsTests() =>
_faker = new Faker<Request>()
.CustomInstantiator(faker => new Request(faker.Random.Number(), faker.Random.Int(1), Array.Empty<string>(), Array.Empty<string>()))
.CustomInstantiator(faker => new Request(faker.Random.Number(), faker.Random.Int(1), Array.Empty<string>(),
Array.Empty<string>()))
.RuleFor(fake => fake.Offset, fake => fake.Random.Number())
.RuleFor(fake => fake.Limit, fake => fake.Random.Int(1))
.RuleFor(fake => fake.Sort, Array.Empty<string>())
Expand All @@ -30,7 +31,9 @@ public async Task GivenRequest_WhenBindingFromHttpContext_ThenReturnRequest()
Request? result = await Request.BindAsync(httpContext, null!);

// assert
result.Should().BeEquivalentTo(new Request(10, 100, new[] { "data" }, new[] { "code:eq:settings" }));
string[] sort = new[] { "data" };
string[] filter = new[] { "code:eq:settings" };
result.Should().BeEquivalentTo(new Request(10, 100, sort, filter));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public async Task GivenRequest_WhenBindingFromHttpContext_ThenReturnRequest()
Request? result = await Request.BindAsync(httpContext, null!);

// assert
result.Should().BeEquivalentTo(new Request(10, 100, new[] { "data" }, new[] { "code:eq:settings" }));
string[] sort = new[] { "data" };
string[] filter = new[] { "code:eq:settings" };
result.Should().BeEquivalentTo(new Request(10, 100, sort, filter));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public async Task GivenRequest_WhenBindingFromHttpContext_ThenReturnRequest()
Request? result = await Request.BindAsync(httpContext, null!);

// assert
result.Should().BeEquivalentTo(new Request(10, 100, new[] { "data" }, new[] { "code:eq:settings" }));
string[] sort = new[] { "data" };
string[] filter = new[] { "code:eq:settings" };
result.Should().BeEquivalentTo(new Request(10, 100, sort, filter));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public async Task GivenRequest_WhenBindingFromHttpContext_ThenReturnRequest()
Request? result = await Request.BindAsync(httpContext, null!);

// assert
result.Should().BeEquivalentTo(new Request(10, 100, new[] { "data" }, new[] { "code:eq:settings" }));
string[] sort = new[] { "data" };
string[] filter = new[] { "code:eq:settings" };
result.Should().BeEquivalentTo(new Request(10, 100, sort, filter));
}

[Fact]
Expand Down
Loading

0 comments on commit c2a059f

Please sign in to comment.