Skip to content

Commit

Permalink
Fix editorconfig warnings (#358)
Browse files Browse the repository at this point in the history
* Fix editorconfig errors

* Fix build warnings

* Remove "this." from codebase

* Fixing build editor suggestions

* Enable nullable references in Blazor project

* Fix more editor config issues

* WIP: Use ModelName -> Request/Response namespaces everywhere

* Use ModelName -> Request/Response namespaces everywhere

* Remove unused using statements

* Fix null reference issues

* 11 warnings to go

* Add more XML comments and fix all editorconfig warnings

* Fix unit test
  • Loading branch information
Viincenttt authored May 14, 2024
1 parent ef73b55 commit 104e560
Show file tree
Hide file tree
Showing 235 changed files with 2,121 additions and 1,944 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Reflection;

namespace Mollie.WebApplication.Blazor.Framework;
namespace Mollie.WebApplication.Blazor.Framework;

public static class StaticStringListBuilder {
public static IEnumerable<string> GetStaticStringList(Type type) {
foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.Static | BindingFlags.Public)) {
string value = fieldInfo.GetValue(null).ToString();
yield return value;
string? value = fieldInfo.GetValue(null)?.ToString();
if (value != null) {
yield return value;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Mollie.WebApplication.Blazor.Framework.Validators;
namespace Mollie.WebApplication.Blazor.Framework.Validators;

public class DecimalPlacesAttribute : ValidationAttribute {
public int DecimalPlaces { get; }
private int _decimalPlaces { get; }

public DecimalPlacesAttribute(int decimalPlaces) {
DecimalPlaces = decimalPlaces;
_decimalPlaces = decimalPlaces;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) {
if (value == null) {
return new ValidationResult("Value is null");
}

decimal amount = (decimal)value;
string text = amount.ToString(CultureInfo.InvariantCulture);
int dotIndex = text.IndexOf('.');
var decimals = text.Length - dotIndex - 1;
var places = DecimalPlaces switch
var places = _decimalPlaces switch
{
0 => "without decimal places",
1 => "with one decimal place",
_ => $"with {DecimalPlaces} decimal places"
_ => $"with {_decimalPlaces} decimal places"
};
return dotIndex < 0 || dotIndex != text.LastIndexOf('.') || decimals != DecimalPlaces
return dotIndex < 0 || dotIndex != text.LastIndexOf('.') || decimals != _decimalPlaces
? new ValidationResult(ErrorMessage ?? $"Please enter an amount {places}")
: ValidationResult.Success;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Mollie.WebApplication.Blazor.Framework.Validators;
namespace Mollie.WebApplication.Blazor.Framework.Validators;

public class StaticStringListAttribute : ValidationAttribute {
private readonly Type _staticClass;

public StaticStringListAttribute(Type staticClass) {
this._staticClass = staticClass;
_staticClass = staticClass;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
IEnumerable<string> validValues = this._staticClass
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) {
IEnumerable<string?> validValues = _staticClass
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(x => x.GetValue(null).ToString());
.Select(x => x.GetValue(null)?.ToString());

if (validValues.Contains(value)) {
return ValidationResult.Success;
}

return new ValidationResult($"The value \"{value}\" is invalid");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Mollie.WebApplication.Blazor.Models.Customer;

public class CreateCustomerModel {
[Required]
public string Name { get; set; }
public required string Name { get; set; }

[EmailAddress]
public string Email { get; set; }
public required string Email { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.ComponentModel.DataAnnotations;

namespace Mollie.WebApplication.Blazor.Models.Mandate;
namespace Mollie.WebApplication.Blazor.Models.Mandate;

public class CreateMandateModel {
[Required]
public string ConsumerName { get; set; }
public required string ConsumerName { get; set; }

[Required]
public string ConsumerAccount { get; set; }
}
public required string ConsumerAccount { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
using System.ComponentModel.DataAnnotations;

namespace Mollie.WebApplication.Blazor.Models.Order;
namespace Mollie.WebApplication.Blazor.Models.Order;

public class CreateOrderBillingAddressModel {
[Required]
public string GivenName { get; set; }
public required string GivenName { get; set; }

[Required]
public string FamilyName { get; set; }
public required string FamilyName { get; set; }

[Required]
[EmailAddress]
public string Email { get; set; }
public required string Email { get; set; }

[Required]
public string StreetAndNumber { get; set; }
public required string StreetAndNumber { get; set; }

[Required]
public string City { get; set; }
public required string City { get; set; }

[Required]
[MaxLength(2)]
public string Country { get; set; }
public required string Country { get; set; }

[Required]
public string PostalCode { get; set; }
}
public required string PostalCode { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using System.ComponentModel.DataAnnotations;
using Mollie.WebApplication.Blazor.Framework.Validators;

namespace Mollie.WebApplication.Blazor.Models.Order;
namespace Mollie.WebApplication.Blazor.Models.Order;

public class CreateOrderLineModel {
[Required]
public string Name { get; set; }
public string Name { get; set; } = string.Empty;

[Required]
[Range(1, 100, ErrorMessage = "Please enter a quantity between 0.01 and 1000")]
public int Quantity { get; set; }

[Required]
[Range(0.01, 10000, ErrorMessage = "Please enter a unit price between 0.01 and 10000")]
[DecimalPlaces(2)]
public decimal UnitPrice { get; set; }

public decimal TotalAmount { get; set; }

[Range(0.01, 100, ErrorMessage = "Please enter a vat rate between 0.01 and 100")]
[DecimalPlaces(2)]
public decimal VatRate { get; set; }
public decimal VatAmount { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
using Mollie.Api.Models;
using Mollie.WebApplication.Blazor.Framework.Validators;

namespace Mollie.WebApplication.Blazor.Models.Order;
namespace Mollie.WebApplication.Blazor.Models.Order;

public class CreateOrderModel {
[Required]
public string OrderNumber { get; set; }

public string? OrderNumber { get; set; }

[Required]
public string? Locale { get; set; }

[Required]
public string Locale { get; set; }
public decimal Amount { get; set; }
public decimal? Amount { get; set; }

[Required]
[StaticStringList(typeof(Currency))]
public string Currency { get; set; }
public required string Currency { get; set; }

[Required]
[Url]
public string RedirectUrl { get; set; }
public List<CreateOrderLineModel> Lines { get; set; }
public CreateOrderBillingAddressModel BillingAddress { get; set; }
}
public required string RedirectUrl { get; set; }

public List<CreateOrderLineModel>? Lines { get; set; } = new();

public required CreateOrderBillingAddressModel BillingAddress { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
using Mollie.Api.Models;
using Mollie.WebApplication.Blazor.Framework.Validators;

namespace Mollie.WebApplication.Blazor.Models.Payment;
namespace Mollie.WebApplication.Blazor.Models.Payment;

public class CreatePaymentModel {
[Required]
[Range(0.01, 1000, ErrorMessage = "Please enter an amount between 0.01 and 1000")]
[DecimalPlaces(2)]
public decimal Amount { get; set; }
public required decimal Amount { get; set; }

[Required]
[StaticStringList(typeof(Currency))]
public string Currency { get; set; }
public required string Currency { get; set; }

[Required]
[Url]
public string RedirectUrl { get; set; }
public required string RedirectUrl { get; set; }

[Required]
public string Description { get; set; }
}
public required string Description { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using Mollie.Api.Models;
using Mollie.WebApplication.Blazor.Framework.Validators;

namespace Mollie.WebApplication.Blazor.Models.Subscription;

namespace Mollie.WebApplication.Blazor.Models.Subscription;

public class CreateSubscriptionModel {
[Required]
[Range(0.01, 1000, ErrorMessage = "Please enter an amount between 0.01 and 1000")]
[DecimalPlaces(2)]
public decimal Amount { get; set; } = default!;
public required decimal Amount { get; set; }

[Required]
[StaticStringList(typeof(Currency))]
public string Currency { get; set; }
public required string Currency { get; set; }

[Range(1, 10)]
public int? Times { get; set; }
Expand All @@ -25,8 +24,8 @@ public class CreateSubscriptionModel {

[Required]
[Display(Name = "Interval period")]
public IntervalPeriod IntervalPeriod { get; set; }
public required IntervalPeriod IntervalPeriod { get; set; }

[Required]
public string Description { get; set; }
}
public required string Description { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>51758f49-d7ec-4044-8cd9-95cf49d0f3cf</UserSecretsId>
</PropertyGroup>
Expand Down
11 changes: 6 additions & 5 deletions samples/Mollie.WebApplication.Blazor/Pages/Customer/Create.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

@using Mollie.WebApplication.Blazor.Models.Customer
@using Mollie.Api.Client
@using Mollie.Api.Models.Customer.Request

@inject ICustomerClient CustomerClient
@inject NavigationManager NavigationManager
Expand Down Expand Up @@ -36,17 +37,17 @@
</EditForm>

@code {
private MollieApiException _apiException = null;
private MollieApiException? _apiException;

private CreateCustomerModel _customer = new() {
Name = "Customer name",
Email = "[email protected]"
};

private async Task OnSave() {
try {
_apiException = null;

await CustomerClient.CreateCustomerAsync(new CustomerRequest() {
Name = _customer.Name,
Email = _customer.Email
Expand All @@ -58,4 +59,4 @@
_apiException = e;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
@page "/customer/overview"
@using Mollie.Api.Models.Customer.Response
@using Mollie.Api.Models.List.Response

@inject ICustomerClient CustomerClient

Expand All @@ -11,7 +13,7 @@ else {
<div class="clearfix">
<a href="/customer/create" class="btn btn-primary float-right">Create new customer</a>
</div>

<table class="table table-striped">
<thead>
<tr>
Expand Down Expand Up @@ -39,7 +41,7 @@ else {
}
</tbody>
</table>

<OverviewNavigation
Previous="_customers.Links.Previous"
Next="_customers.Links.Next">
Expand All @@ -49,9 +51,9 @@ else {
@code {
[Parameter]
[SupplyParameterFromQuery]
public string Url { get; set; }
private ListResponse<CustomerResponse> _customers = null;
public string? Url { get; set; }

private ListResponse<CustomerResponse>? _customers;

protected override async Task OnParametersSetAsync() {
await LoadData();
Expand All @@ -68,4 +70,4 @@ else {
});
}
}
}
}
Loading

0 comments on commit 104e560

Please sign in to comment.