refactor (readonly for static objects in tests and file-scoped namespaces

This commit is contained in:
2024-09-22 17:34:29 +03:00
parent 1625764e0a
commit a80076e2e6
50 changed files with 1196 additions and 1246 deletions

View File

@@ -1,4 +1,3 @@
namespace BlazorWebAssemblyVisaApiClient.Common.Exceptions
{
public class BlazorClientException(string message) : Exception(message);
}
namespace BlazorWebAssemblyVisaApiClient.Common.Exceptions;
public class BlazorClientException(string message) : Exception(message);

View File

@@ -1,4 +1,3 @@
namespace BlazorWebAssemblyVisaApiClient.Common.Exceptions
{
public class NotLoggedInException() : BlazorClientException("User is not logged in.");
}
namespace BlazorWebAssemblyVisaApiClient.Common.Exceptions;
public class NotLoggedInException() : BlazorClientException("User is not logged in.");

View File

@@ -1,20 +1,19 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Components;
namespace BlazorWebAssemblyVisaApiClient
namespace BlazorWebAssemblyVisaApiClient;
public static class Constants
{
public static class Constants
{
public readonly static Regex EnglishWordRegex = new("^[a-zA-Z]*$");
public readonly static Regex EnglishWordRegex = new("^[a-zA-Z]*$");
public readonly static Regex EnglishPhraseRegex = new(@"^[a-z A-Z№0-9?><;,{}[\]\-_+=!@#$%\^&*|']*$");
public readonly static Regex EnglishPhraseRegex = new(@"^[a-z A-Z№0-9?><;,{}[\]\-_+=!@#$%\^&*|']*$");
public readonly static Regex PhoneNumRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$");
public readonly static Regex PhoneNumRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$");
public readonly static MarkupString RequiredFieldMarkup = (MarkupString)"<span style=\"color: red;\">*</span>";
public readonly static MarkupString RequiredFieldMarkup = (MarkupString)"<span style=\"color: red;\">*</span>";
public const string ApplicantRole = "Applicant";
public const string ApprovingAuthorityRole = "ApprovingAuthority";
public const string AdminRole = "Admin";
}
}
public const string ApplicantRole = "Applicant";
public const string ApprovingAuthorityRole = "ApprovingAuthority";
public const string AdminRole = "Admin";
}

View File

@@ -3,17 +3,16 @@ using BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models;
using VisaApiClient;
using PlaceOfWorkModel = BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models.PlaceOfWorkModel;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.AutoMapper.Profiles
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.AutoMapper.Profiles;
public class RegisterApplicantRequestProfile : Profile
{
public class RegisterApplicantRequestProfile : Profile
public RegisterApplicantRequestProfile()
{
public RegisterApplicantRequestProfile()
{
CreateMap<RegisterApplicantRequestModel, RegisterApplicantRequest>(MemberList.Destination);
CreateMap<RegisterRequestModel, RegisterRequest>(MemberList.Destination);
CreateMap<PlaceOfWorkModel, VisaApiClient.PlaceOfWorkModel>(MemberList.Destination);
}
}
}
}

View File

@@ -2,13 +2,12 @@
using BlazorWebAssemblyVisaApiClient.Validation.VisaApplications.Models;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.AutoMapper.Profiles
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.AutoMapper.Profiles;
public class VisaApplicationCreateRequestProfile : Profile
{
public class VisaApplicationCreateRequestProfile : Profile
public VisaApplicationCreateRequestProfile()
{
public VisaApplicationCreateRequestProfile()
{
CreateMap<VisaApplicationCreateRequestModel, VisaApplicationCreateRequest>(MemberList.Destination);
}
}
}
}

View File

@@ -1,11 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers;
public static class EnumExtensions
{
public static class EnumExtensions
public static string GetDisplayName(this Enum value)
{
public static string GetDisplayName(this Enum value)
{
var enumMembers = value.GetType().GetMembers();
var member = enumMembers.First(info => info.Name == value.ToString());
var displayAttribute = (DisplayAttribute?)member
@@ -14,5 +14,4 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
var displayName = displayAttribute?.Name ?? value.ToString();
return displayName;
}
}
}
}

View File

@@ -1,15 +1,15 @@
using System.Text;
using FluentValidation.Results;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
{
public static class ValidationResultExtensions
{
public static string ToErrorsString(this ValidationResult validationResult)
=> ErrorsToString(validationResult.Errors.Select(e => e.ErrorMessage));
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers;
private static string ErrorsToString(IEnumerable<string> errors)
{
public static class ValidationResultExtensions
{
public static string ToErrorsString(this ValidationResult validationResult)
=> ErrorsToString(validationResult.Errors.Select(e => e.ErrorMessage));
private static string ErrorsToString(IEnumerable<string> errors)
{
var stringBuilder = new StringBuilder();
foreach (var error in errors)
{
@@ -18,5 +18,4 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Helpers
return stringBuilder.ToString();
}
}
}
}

View File

@@ -1,9 +1,8 @@
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
{
public class DateTimeProvider : IDateTimeProvider
{
public DateTime Now() => DateTime.Now;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider;
public string FormattedNow() => Now().ToString("yyyy-MM-dd");
}
}
public class DateTimeProvider : IDateTimeProvider
{
public DateTime Now() => DateTime.Now;
public string FormattedNow() => Now().ToString("yyyy-MM-dd");
}

View File

@@ -1,9 +1,8 @@
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider
{
public interface IDateTimeProvider
{
DateTime Now();
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.DateTimeProvider;
string FormattedNow();
}
}
public interface IDateTimeProvider
{
DateTime Now();
string FormattedNow();
}

View File

@@ -1,6 +1,5 @@
using BlazorWebAssemblyVisaApiClient.Common.Exceptions;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider.Exceptions
{
public class UnknownRoleException() : BlazorClientException("Unknown user role");
}
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider.Exceptions;
public class UnknownRoleException() : BlazorClientException("Unknown user role");

View File

@@ -1,15 +1,14 @@
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider;
public interface IUserDataProvider
{
public interface IUserDataProvider
{
public string? CurrentRole { get; }
public string? CurrentRole { get; }
public Action? OnRoleChanged { get; set; }
public Action? OnRoleChanged { get; set; }
public Task<ApplicantModel> GetApplicant();
public Task<ApplicantModel> GetApplicant();
public void UpdateCurrentRole();
}
}
public void UpdateCurrentRole();
}

View File

@@ -3,23 +3,23 @@ using System.Security.Claims;
using BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider.Exceptions;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider
namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvider;
public class UserDataProvider(Client client) : IUserDataProvider
{
public class UserDataProvider(Client client) : IUserDataProvider
private readonly static JwtSecurityTokenHandler tokenHandler = new();
public string? CurrentRole { get; private set; }
public Action? OnRoleChanged { get; set; }
public async Task<ApplicantModel> GetApplicant()
{
private readonly static JwtSecurityTokenHandler tokenHandler = new();
public string? CurrentRole { get; private set; }
public Action? OnRoleChanged { get; set; }
public async Task<ApplicantModel> GetApplicant()
{
return await client.GetApplicantAsync();
}
public void UpdateCurrentRole()
{
public void UpdateCurrentRole()
{
var role = CurrentRole;
if (client.AuthToken is null)
@@ -49,5 +49,4 @@ namespace BlazorWebAssemblyVisaApiClient.Infrastructure.Services.UserDataProvide
OnRoleChanged?.Invoke();
}
}
}
}
}

View File

@@ -1,21 +1,20 @@
using System.ComponentModel.DataAnnotations;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models;
/// Model of place of work with attributes required for validation to work
public class PlaceOfWorkModel
{
/// Model of place of work with attributes required for validation to work
public class PlaceOfWorkModel
{
[Required]
[StringLength(ConfigurationConstraints.PlaceOfWorkNameLength, MinimumLength = 1)]
public string Name { get; set; } = default!;
[Required]
[StringLength(ConfigurationConstraints.PlaceOfWorkNameLength, MinimumLength = 1)]
public string Name { get; set; } = default!;
[Required]
[ValidateComplexType]
public AddressModel Address { get; set; } = new AddressModel();
[Required]
[ValidateComplexType]
public AddressModel Address { get; set; } = new AddressModel();
[Required]
[StringLength(ConfigurationConstraints.PhoneNumberLength, MinimumLength = ConfigurationConstraints.PhoneNumberMinLength)]
public string PhoneNum { get; set; } = default!;
}
}
[Required]
[StringLength(ConfigurationConstraints.PhoneNumberLength, MinimumLength = ConfigurationConstraints.PhoneNumberMinLength)]
public string PhoneNum { get; set; } = default!;
}

View File

@@ -3,66 +3,65 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models;
/// Model of request with attributes required for validation to work
public class RegisterApplicantRequestModel
{
/// Model of request with attributes required for validation to work
public class RegisterApplicantRequestModel
{
[Required]
[ValidateComplexType]
public RegisterRequestModel RegisterRequest { get; set; } = new();
[Required]
[ValidateComplexType]
public RegisterRequestModel RegisterRequest { get; set; } = new();
[Required]
[ValidateComplexType]
public NameModel ApplicantName { get; set; } = new();
[Required]
[ValidateComplexType]
public NameModel ApplicantName { get; set; } = new();
[Required]
[ValidateComplexType]
public PassportModel Passport { get; set; } = new();
[Required]
[ValidateComplexType]
public PassportModel Passport { get; set; } = new();
[Required(AllowEmptyStrings = true)]
public DateTimeOffset BirthDate { get; set; }
[Required(AllowEmptyStrings = true)]
public DateTimeOffset BirthDate { get; set; }
[Required]
[StringLength(70, MinimumLength = 1)]
public string CityOfBirth { get; set; } = default!;
[Required]
[StringLength(70, MinimumLength = 1)]
public string CityOfBirth { get; set; } = default!;
[Required]
[StringLength(70, MinimumLength = 1)]
public string CountryOfBirth { get; set; } = default!;
[Required]
[StringLength(70, MinimumLength = 1)]
public string CountryOfBirth { get; set; } = default!;
[Required]
[StringLength(30, MinimumLength = 1)]
public string Citizenship { get; set; } = default!;
[Required]
[StringLength(30, MinimumLength = 1)]
public string Citizenship { get; set; } = default!;
[Required]
[StringLength(30, MinimumLength = 1)]
public string CitizenshipByBirth { get; set; } = default!;
[Required]
[StringLength(30, MinimumLength = 1)]
public string CitizenshipByBirth { get; set; } = default!;
[Required(AllowEmptyStrings = true)]
[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }
[Required(AllowEmptyStrings = true)]
[JsonConverter(typeof(StringEnumConverter))]
public Gender Gender { get; set; }
[Required(AllowEmptyStrings = true)]
[JsonConverter(typeof(StringEnumConverter))]
public MaritalStatus MaritalStatus { get; set; }
[Required(AllowEmptyStrings = true)]
[JsonConverter(typeof(StringEnumConverter))]
public MaritalStatus MaritalStatus { get; set; }
[Required]
[ValidateComplexType]
public NameModel FatherName { get; set; } = new();
[Required]
[ValidateComplexType]
public NameModel FatherName { get; set; } = new();
[Required]
[ValidateComplexType]
public NameModel MotherName { get; set; } = new();
[Required]
[ValidateComplexType]
public NameModel MotherName { get; set; } = new();
[Required]
[StringLength(50, MinimumLength = 1)]
public string JobTitle { get; set; } = default!;
[Required]
[StringLength(50, MinimumLength = 1)]
public string JobTitle { get; set; } = default!;
[Required]
[ValidateComplexType]
public PlaceOfWorkModel PlaceOfWork { get; set; } = new();
[Required]
[ValidateComplexType]
public PlaceOfWorkModel PlaceOfWork { get; set; } = new();
public bool IsNonResident { get; set; }
}
}
public bool IsNonResident { get; set; }
}

View File

@@ -1,13 +1,12 @@
using System.ComponentModel.DataAnnotations;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models
namespace BlazorWebAssemblyVisaApiClient.Validation.Applicants.Models;
/// Model of request with attributes required for validation to work
public class RegisterRequestModel
{
/// Model of request with attributes required for validation to work
public class RegisterRequestModel
{
[Required]
[ValidateComplexType]
public AuthData AuthData { get; set; } = new AuthData();
}
}
[Required]
[ValidateComplexType]
public AuthData AuthData { get; set; } = new AuthData();
}

View File

@@ -1,12 +1,12 @@
using FluentValidation;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Validation.Auth
namespace BlazorWebAssemblyVisaApiClient.Validation.Auth;
public class ChangeUserAuthDataRequestValidator : AbstractValidator<ChangeUserAuthDataRequest>
{
public class ChangeUserAuthDataRequestValidator : AbstractValidator<ChangeUserAuthDataRequest>
public ChangeUserAuthDataRequestValidator()
{
public ChangeUserAuthDataRequestValidator()
{
RuleFor(r => r.NewAuthData)
.NotEmpty();
@@ -17,5 +17,4 @@ namespace BlazorWebAssemblyVisaApiClient.Validation.Auth
.MaximumLength(ConfigurationConstraints.EmailLength)
.WithMessage($"Email address length must be less than {ConfigurationConstraints.EmailLength}");
}
}
}
}

View File

@@ -1,38 +1,37 @@
using System.ComponentModel.DataAnnotations;
using VisaApiClient;
namespace BlazorWebAssemblyVisaApiClient.Validation.VisaApplications.Models
namespace BlazorWebAssemblyVisaApiClient.Validation.VisaApplications.Models;
/// Model for request for data annotations validation to work
public class VisaApplicationCreateRequestModel
{
/// Model for request for data annotations validation to work
public class VisaApplicationCreateRequestModel
{
[ValidateComplexType]
public ReentryPermitModel? ReentryPermit { get; set; } = default!;
[ValidateComplexType]
public ReentryPermitModel? ReentryPermit { get; set; } = default!;
[Required]
[MaxLength(ConfigurationConstraints.CountryNameLength)]
public string DestinationCountry { get; set; } = default!;
[Required]
[MaxLength(ConfigurationConstraints.CountryNameLength)]
public string DestinationCountry { get; set; } = default!;
[Required]
public VisaCategory VisaCategory { get; set; }
[Required]
public VisaCategory VisaCategory { get; set; }
[Required]
public bool IsForGroup { get; set; }
[Required]
public bool IsForGroup { get; set; }
[Required]
public RequestedNumberOfEntries RequestedNumberOfEntries { get; set; }
[Required]
public RequestedNumberOfEntries RequestedNumberOfEntries { get; set; }
[Required]
[Range(0, ConfigurationConstraints.MaxValidDays)]
public int ValidDaysRequested { get; set; }
[Required]
[Range(0, ConfigurationConstraints.MaxValidDays)]
public int ValidDaysRequested { get; set; }
[ValidateComplexType]
public List<PastVisaModel> PastVisas { get; set; } = [];
[ValidateComplexType]
public List<PastVisaModel> PastVisas { get; set; } = [];
[ValidateComplexType]
public PermissionToDestCountryModel? PermissionToDestCountry { get; set; } = default!;
[ValidateComplexType]
public PermissionToDestCountryModel? PermissionToDestCountry { get; set; } = default!;
[ValidateComplexType]
public List<PastVisitModel> PastVisits { get; set; } = [];
}
}
[ValidateComplexType]
public List<PastVisitModel> PastVisits { get; set; } = [];
}