Skip to content

Commit

Permalink
Conver to array initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Socolin committed Apr 1, 2024
1 parent 96d6798 commit e881efc
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Naheulbook.Requests.Validators;

public class CharacterLevelUpRequestValidator : AbstractValidator<CharacterLevelUpRequest>
{
private static readonly List<string> ValidEvEa = new List<string> {"EV", "EA"};
private static readonly List<string> ValidStats = new List<string> {"AD", "FO", "COU", "INT", "PRD", "AT"};
private static readonly List<string> ValidEvEa = ["EV", "EA"];
private static readonly List<string> ValidStats = ["AD", "FO", "COU", "INT", "PRD", "AT"];

public CharacterLevelUpRequestValidator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Naheulbook.Requests.Validators;

public class CreateCharacterRequestValidator: AbstractValidator<CreateCharacterRequest>
{
private static readonly List<string> ValidSex = new List<string> {"Homme", "Femme"};
private static readonly List<string> ValidSex = ["Homme", "Femme"];
public CreateCharacterRequestValidator()
{
RuleFor(e => e.Name).NotNull().Length(1, 255);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Naheulbook.Requests.Validators;

public class ItemTemplateRequestValidator : AbstractValidator<ItemTemplateRequest>
{
private static readonly List<string> ValidSources = new List<string> {"official", "private", "community"};
private static readonly List<string> ValidSources = ["official", "private", "community"];

public ItemTemplateRequestValidator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Naheulbook.Requests.Validators;

public class LapCountDecrementValidator : AbstractValidator<LapCountDecrement>
{
private static readonly List<string> ValidWhenValues = new List<string> {"BEFORE", "AFTER"};
private static readonly List<string> ValidWhenValues = ["BEFORE", "AFTER"];

public LapCountDecrementValidator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Naheulbook.Requests.Validators;

public class StatModifierValidator : AbstractValidator<StatModifierRequest>
{
private static readonly List<string> ValidTypeValues = new List<string> {"ADD", "MUL", "DIV", "SET", "PERCENTAGE"};
private static readonly List<string> ValidTypeValues = ["ADD", "MUL", "DIV", "SET", "PERCENTAGE"];

public StatModifierValidator()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Naheulbook.Requests.Validators;
public abstract class BaseStatsModifierValidator<T> : AbstractValidator<T>
where T : StatsModifier
{
private static List<string> ValidDurationTypes => new List<string> {"combat", "custom", "time", "lap", "forever"};
private static List<string> ValidDurationTypes => ["combat", "custom", "time", "lap", "forever"];

protected BaseStatsModifierValidator()
{
Expand Down
2 changes: 1 addition & 1 deletion backend/src/Naheulbook.Shared/Clients/Oauth1/Oauth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task<IDictionary<string, string>> DoRequest()
if (response.IsSuccessStatusCode)
{
return result.Split('&')
.Select(s => s.Split(new[] {'='}, 2))
.Select(s => s.Split(['='], 2))
.ToDictionary(s => s[0], s => s[1]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

namespace Naheulbook.Web.Middlewares;

public class DevExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IConfiguration configuration)
public class DevExceptionMiddleware(
RequestDelegate next,
ILoggerFactory loggerFactory,
IConfiguration configuration
)
{
private static readonly string[] ExcludedExceptionFields = {"TargetSite", "StackTrace", "Message", "Data", "InnerException", "HelpLink", "Source", "HResult"};
private static readonly string[] ExcludedExceptionFields = ["TargetSite", "StackTrace", "Message", "Data", "InnerException", "HelpLink", "Source", "HResult"];
private readonly ILogger _logger = loggerFactory.CreateLogger(nameof(DevExceptionMiddleware));
private readonly bool _displayExceptionFields = configuration.GetValue<bool>("DisplayExceptionFields");

Expand Down
25 changes: 16 additions & 9 deletions backend/src/Naheulbook.Web/Middlewares/HttpExceptionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

namespace Naheulbook.Web.Middlewares;

public class HttpExceptionMiddleware(RequestDelegate next, ILoggerFactory loggerFactory, IConfiguration configuration)
public class HttpExceptionMiddleware(
RequestDelegate next,
ILoggerFactory loggerFactory,
IConfiguration configuration
)
{
private static readonly string[] ExcludedExceptionFields = {"TargetSite", "StackTrace", "Message", "Data", "InnerException", "HelpLink", "Source", "HResult"};
private static readonly string[] ExcludedExceptionFields = ["TargetSite", "StackTrace", "Message", "Data", "InnerException", "HelpLink", "Source", "HResult"];
private readonly ILogger _logger = loggerFactory.CreateLogger(nameof(HttpExceptionMiddleware));
private readonly bool _displayExceptionFields = configuration.GetValue<bool>("DisplayExceptionFields");

Expand All @@ -28,9 +32,11 @@ public async Task InvokeAsync(HttpContext context)
context.Response.StatusCode = ex.StatusCode;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonConvert.SerializeObject(new
{
ex.Message,
}));
{
ex.Message,
}
)
);
}
catch (Exception ex)
{
Expand All @@ -46,10 +52,11 @@ await context.Response.WriteAsync(JsonConvert.SerializeObject(new
}

await context.Response.WriteAsync(JsonConvert.SerializeObject(new
{
Message = $"An unexpected error occured, and was logged with reference id: {context.TraceIdentifier}",
}));

{
Message = $"An unexpected error occured, and was logged with reference id: {context.TraceIdentifier}",
}
)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

namespace Naheulbook.Web.Middlewares;

public class JwtAuthenticationMiddleware(RequestDelegate next, IJwtService jwtService, ITimeService timeService, IUserAccessTokenService userAccessTokenService)
public class JwtAuthenticationMiddleware(
RequestDelegate next,
IJwtService jwtService,
ITimeService timeService,
IUserAccessTokenService userAccessTokenService
)
{
private const string UserAccessTokenPrefix = "userAccessToken:";

Expand Down Expand Up @@ -46,9 +51,10 @@ public async Task InvokeAsync(HttpContext context)
}

context.SetExecutionContext(new NaheulbookExecutionContext
{
UserId = token.UserId,
});
{
UserId = token.UserId,
}
);
}
else
{
Expand All @@ -59,6 +65,7 @@ public async Task InvokeAsync(HttpContext context)
await context.Response.WriteAsync(JsonConvert.SerializeObject(new {Message = "Invalid JWT"}));
return;
}

if (token.Exp < timeService.UtcNow.ToUnixTimeSeconds())
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
Expand All @@ -67,9 +74,10 @@ public async Task InvokeAsync(HttpContext context)
}

context.SetExecutionContext(new NaheulbookExecutionContext
{
UserId = token.Sub,
});
{
UserId = token.Sub,
}
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,27 +163,30 @@ public async Task CreateEffect_AddANewEffectInDatabase()
Dice = (short?)2,
LapCount = 3,
TimeDuration = 4,
Modifiers = new List<StatModifierRequest>
{
Modifiers =
[
new()
{
Stat = "some-stat",
Type = "some-type",
Value = 5,
},

new()
{
Stat = "some-stat",
Type = "some-type",
Value = 6,
},

new()
{
Stat = "some-stat",
Type = "some-type",
Value = 7,
},
},

],
};
var executionContext = new NaheulbookExecutionContext();

Expand All @@ -201,7 +204,7 @@ public async Task CreateEffect_AddANewEffectInDatabase()
public async Task CreateEffect_EnsureThatUserIsAnAdmin_BeforeAddingInDatabase()
{
var executionContext = new NaheulbookExecutionContext();
var createEffectRequest = new CreateEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = new List<StatModifierRequest>()};
var createEffectRequest = new CreateEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = []};

await _effectService.CreateEffectAsync(executionContext, 2, createEffectRequest);

Expand All @@ -228,27 +231,30 @@ public async Task EditEffect_UpdateEffectInDatabase()
Dice = (short?)3,
LapCount = 4,
TimeDuration = 5,
Modifiers = new List<StatModifierRequest>
{
Modifiers =
[
new()
{
Stat = "some-stat",
Type = "some-type",
Value = 6,
},

new()
{
Stat = "some-stat",
Type = "some-type",
Value = 7,
},

new()
{
Stat = "some-stat",
Type = "some-type",
Value = 8,
},
},

],
SubCategoryId = 1,
};

Expand All @@ -269,7 +275,7 @@ public async Task EditEffect_EnsureThatUserIsAnAdmin_BeforeAddingInDatabase()
{
var executionContext = new NaheulbookExecutionContext();
var previousEffect = AutoFill<EffectEntity>.One(AutoFillFlags.RandomizeString | AutoFillFlags.RandomInt);
var editEffectRequest = new EditEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = new List<StatModifierRequest>()};
var editEffectRequest = new EditEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = []};
previousEffect.Id = 42;

_effectRepository.GetWithModifiersAsync(42)
Expand All @@ -288,7 +294,7 @@ public async Task EditEffect_EnsureThatUserIsAnAdmin_BeforeAddingInDatabase()
public async Task EditEffect_WhenEffectDoesNotExists_Throw()
{
var executionContext = new NaheulbookExecutionContext();
var editEffectRequest = new EditEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = new List<StatModifierRequest>()};
var editEffectRequest = new EditEffectRequest {Name = string.Empty, DurationType = string.Empty, Modifiers = []};

_effectRepository.GetWithModifiersAsync(Arg.Any<int>())
.Returns((EffectEntity)null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public async Task CreateItemsAsync_ShouldThrowIfItemTemplateIsNotFound()
};

_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetByIdsAsync(Arg.Any<IEnumerable<Guid>>())
.Returns(new List<ItemTemplateEntity>());
.Returns([]);

Func<Task> act = () => _service.CreateItemsAsync(createItemRequests);

Expand All @@ -406,7 +406,7 @@ public async Task CreateItemsAsync_ShouldCreateNewItems_ForEachElementOfTheReque
};

_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetByIdsAsync(Arg.Is<IEnumerable<Guid>>(ids => ids.SequenceEqual(new[] {itemTemplateId1, itemTemplateId2})))
.Returns(new List<ItemTemplateEntity> {itemTemplate1, itemTemplate2});
.Returns([itemTemplate1, itemTemplate2]);
_itemFactory.CreateItem(itemTemplate1, itemData1)
.Returns(item1);
_itemFactory.CreateItem(itemTemplate2, itemData2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public async Task CreateItemTemplateSection_AddANewItemTemplateSectionInDatabase
Name = "some-name",
Icon = "some-icon",
Note = "some-note",
Specials = new List<string>
{
Specials =
[
"some-specials0",
"some-specials1",
"some-specials2",
},
],
};

var itemTemplateSection = await _service.CreateItemTemplateSectionAsync(new NaheulbookExecutionContext(), createItemTemplateSectionRequest);
Expand All @@ -65,7 +65,7 @@ public async Task CreateItemTemplateSection_AddANewItemTemplateSectionInDatabase
[Test]
public async Task CreateItemTemplateSection_EnsureThatUserIsAnAdmin_BeforeAddingInDatabase()
{
var request = new CreateItemTemplateSectionRequest {Name = string.Empty, Icon = string.Empty, Specials = new List<string>()};
var request = new CreateItemTemplateSectionRequest {Name = string.Empty, Icon = string.Empty, Specials = []};
var executionContext = new NaheulbookExecutionContext();

await _service.CreateItemTemplateSectionAsync(executionContext, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ public async Task SearchItemTemplateAsync_ShouldTryToFindItemsInDatabaseWithMore
_stringCleanupUtil.RemoveSeparators(cleanFilter)
.Returns(cleanFilterWithoutSeparator);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByCleanNameWithAllDataAsync(cleanFilter, Arg.Any<int>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {item1});
.Returns([item1]);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithAllDataAsync(cleanFilter, Arg.Any<int>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {item2});
.Returns([item2]);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithoutSeparatorWithAllDataAsync(cleanFilterWithoutSeparator, Arg.Any<int>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {item3});
.Returns([item3]);

var actualItemTemplates = await _service.SearchItemTemplateAsync(filter, 40, null);

Expand All @@ -267,11 +267,11 @@ public async Task SearchItemTemplateAsync_ShouldLimitResultToGivenMaximumCount()
_stringCleanupUtil.RemoveSeparators(Arg.Any<string>())
.Returns("some-clean-name-with-no-separator");
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByCleanNameWithAllDataAsync(Arg.Any<string>(), Arg.Any<int>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {new ItemTemplateEntity(), new ItemTemplateEntity()});
.Returns([new ItemTemplateEntity(), new ItemTemplateEntity()]);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithAllDataAsync(Arg.Any<string>(), Arg.Any<int>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {new ItemTemplateEntity(), new ItemTemplateEntity(), new ItemTemplateEntity()});
.Returns([new ItemTemplateEntity(), new ItemTemplateEntity(), new ItemTemplateEntity()]);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetItemByPartialCleanNameWithoutSeparatorWithAllDataAsync(Arg.Any<string>(), Arg.Any<int>(), Arg.Any<IEnumerable<Guid>>(), Arg.Any<int?>(), Arg.Any<bool>())
.Returns(new List<ItemTemplateEntity> {new ItemTemplateEntity()});
.Returns([new ItemTemplateEntity()]);

await _service.SearchItemTemplateAsync("some-filter", 10, null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public async Task CreateMonsterTemplate_InsertNewEntityInDatabase()
_unitOfWorkFactory.GetUnitOfWork().MonsterSubCategories.GetAsync(subCategoryId)
.Returns(monsterSubCategory);
_unitOfWorkFactory.GetUnitOfWork().ItemTemplates.GetByIdsAsync(Arg.Is<IEnumerable<Guid>>(x => x.SequenceEqual(new[] {itemTemplateId})))
.Returns(new List<ItemTemplateEntity> {itemTemplate});
.Returns([itemTemplate]);

var monsterTemplate = await _service.CreateMonsterTemplateAsync(executionContext, request);

Expand Down
Loading

0 comments on commit e881efc

Please sign in to comment.