Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AutoMapper" Version="14.0.0" />
<PackageVersion Include="BuildBundlerMinifier" Version="3.2.449" />
<PackageVersion Include="IdentityModel" Version="7.0.0" />
<PackageVersion Include="Mapster" Version="7.4.0" />
<PackageVersion Include="Mapster.DependencyInjection" Version="1.0.1" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.7" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="9.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.7" />
Expand All @@ -20,11 +21,11 @@
<PackageVersion Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="7.0.0" />
<PackageVersion Include="Serilog.Sinks.MSSqlServer" Version="8.2.2" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.12.1" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.13.0" />
<PackageVersion Include="System.Text.Json" Version="9.0.7" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="xunit.v3" Version="2.0.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.1" />
<PackageVersion Include="xunit.v3" Version="3.0.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="3.1.3" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public class DungeonOptionModel : EditModel
public int RoamingPercent { get; set; }
public int Width { get; set; } = 800;
public int Height { get; set; } = 800;
public IEnumerable<DungeonModel> Dungeons { get; } = new List<DungeonModel>();
public List<DungeonModel> Dungeons { get; set; } = [];
}
6 changes: 3 additions & 3 deletions src/RDMG.Core/ConfigureServices.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using RDMG.Core.Abstractions.Generator;
using RDMG.Core.Abstractions.Services;
using RDMG.Core.Generator;
using RDMG.Core.Services;
using RDMG.Core.Services.Automapper;

namespace RDMG.Core;

Expand All @@ -22,7 +22,7 @@ this IServiceCollection services
.AddScoped<IDungeonNoCorridor, DungeonNoCorridor>()
.AddScoped<IDungeonService, DungeonService>();

services.AddAutoMapper(cfg => { cfg.AllowNullCollections = true; }, typeof(DungeonProfile));
services.AddMapster();

return services;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RDMG.Core/Domain/DungeonOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public class DungeonOption : AuditableEntity

public int UserId { get; set; }
public User? User { get; set; }
public IEnumerable<Dungeon> Dungeons { get; } = new List<Dungeon>();
public List<Dungeon> Dungeons { get; } = [];
}
15 changes: 10 additions & 5 deletions src/RDMG.Core/Generator/DungeonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,20 @@ private List<Monster> GetMonsters(IEnumerable<Monster> monsters)
{
if (MonsterType.Equals("any", StringComparison.OrdinalIgnoreCase))
{
return monsters.Where(monster => Parse(monster.ChallengeRating) <= PartyLevel + 2 &&
return
[
.. monsters.Where(monster => Parse(monster.ChallengeRating) <= PartyLevel + 2 &&
Parse(monster.ChallengeRating) >= PartyLevel / 4.0)
.ToList();
];
}

return monsters.Where(monster => Parse(monster.ChallengeRating) <= PartyLevel + 2 &&
return
[
.. monsters.Where(monster => Parse(monster.ChallengeRating) <= PartyLevel + 2 &&
Parse(monster.ChallengeRating) >= PartyLevel / 4.0 &&
MonsterType.Contains(monster.Type))
.ToList();
MonsterType.Contains(monster.Type,
StringComparison.InvariantCultureIgnoreCase))
];
}

public string GetMonsterDescription()
Expand Down
3 changes: 2 additions & 1 deletion src/RDMG.Core/RDMG.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="AutoMapper" />
<PackageReference Include="Mapster" />
<PackageReference Include="Mapster.DependencyInjection" />
<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="System.Text.Json" />
<PackageReference Include="Serilog.AspNetCore" />
Expand Down
2 changes: 1 addition & 1 deletion src/RDMG.Core/Services/AuthService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using RDMG.Core.Abstractions.Repository;
using RDMG.Core.Abstractions.Services;
Expand Down
14 changes: 0 additions & 14 deletions src/RDMG.Core/Services/Automapper/DungeonProfile.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/RDMG.Core/Services/Automapper/OptionProfile.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/RDMG.Core/Services/Automapper/UserProfile.cs

This file was deleted.

13 changes: 8 additions & 5 deletions src/RDMG.Core/Services/DungeonService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using RDMG.Core.Abstractions.Generator;
using RDMG.Core.Abstractions.Repository;
Expand Down Expand Up @@ -208,8 +208,9 @@ public async Task<IEnumerable<DungeonOptionModel>> GetAllDungeonOptionsForUserAs
{
try
{
return _mapper.Map<DungeonOptionModel>(
await _dungeonOptionRepository.GetDungeonOptionByNameAsync(dungeonName, userId, cancellationToken));
var model = await _dungeonOptionRepository.GetDungeonOptionByNameAsync(dungeonName, userId,
cancellationToken);
return model is not null ? _mapper.Map<DungeonOptionModel>(model) : null;
}
catch (Exception ex)
{
Expand Down Expand Up @@ -278,7 +279,8 @@ public async Task<DungeonModel> GetDungeonAsync(int id, CancellationToken cancel
{
try
{
return _mapper.Map<DungeonModel>(await _dungeonRepository.GetDungeonAsync(id, cancellationToken));
return _mapper.Map<DungeonModel>(await _dungeonRepository.GetDungeonAsync(id, cancellationToken) ??
throw new ServiceException(Error.NotFound));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -310,7 +312,8 @@ public async Task<DungeonOptionModel> GetDungeonOptionAsync(int id, Cancellation
try
{
return _mapper.Map<DungeonOptionModel>(
await _dungeonOptionRepository.GetDungeonOptionAsync(id, cancellationToken));
await _dungeonOptionRepository.GetDungeonOptionAsync(id, cancellationToken) ??
throw new ServiceException(Error.NotFound));
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion src/RDMG.Core/Services/OptionService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using RDMG.Core.Abstractions.Repository;
Expand Down
9 changes: 5 additions & 4 deletions src/RDMG.Core/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.Extensions.Logging;
using RDMG.Core.Abstractions.Repository;
using RDMG.Core.Abstractions.Services;
Expand All @@ -18,7 +18,7 @@ public class UserService(IMapper mapper, IUserRepository userRepository, ILogger
public async Task<int> CreateAsync(UserModel model)
{
ValidateModel(model);
await CheckUserExist(model);
await CheckUserExistAsync(model);
try
{
model.Password = PasswordHelper.EncryptPassword(model.Password);
Expand Down Expand Up @@ -53,7 +53,7 @@ private static void ValidateModel(UserModel model)
throw new ServiceAggregateException(errors);
}

private async Task CheckUserExist(UserModel model)
private async Task CheckUserExistAsync(UserModel model)
{
var user = await _userRepository.GetByUsernameAsync(model.Username, null);
if (user is not null)
Expand All @@ -77,7 +77,8 @@ public async Task<UserModel> GetAsync(int id)
{
try
{
var user = await _userRepository.GetAsync(id);
var user = await _userRepository.GetAsync(id) ??
throw new ServiceException(Resources.Error.NotFound);

return _mapper.Map<UserModel>(user);
}
Expand Down
5 changes: 3 additions & 2 deletions src/RDMG.Infrastructure/ConfigureServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using RDMG.Infrastructure.Interceptors;
using RDMG.Infrastructure.Repository;
using System.Reflection;
using RDMG.Infrastructure.Repository.Automapper;
using Mapster;

namespace RDMG.Infrastructure;

Expand Down Expand Up @@ -77,7 +77,8 @@ private static void Configure(IServiceCollection services, IConfiguration config
.AddScoped<IDungeonOptionRepository, DungeonOptionRepository>()
.AddScoped<IOptionRepository, OptionRepository>()
.AddScoped<IUserRepository, UserRepository>();
services.AddAutoMapper(cfg => { cfg.AllowNullCollections = true; }, typeof(DungeonProfile));

services.AddMapster();
}

public static IServiceCollection AddTestInfrastructureServices(this IServiceCollection services,
Expand Down
19 changes: 11 additions & 8 deletions src/RDMG.Infrastructure/Data/AppDbContextInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
using RDMG.Core.Helpers;

namespace RDMG.Infrastructure.Data;

public class AppDbContextInitializer(IAppDbContext context, IDungeonService dungeonService, IOptions<AppConfig> config)
{
private readonly IAppDbContext _context = context;
private readonly IDungeonService _dungeonService = dungeonService;
private const string UtDungeonName1 = "Test 1";
public const string UtDungeonName2 = "Test 2";

public async Task UpdateAsync(CancellationToken cancellationToken)
{
Expand All @@ -24,13 +27,14 @@ public async Task UpdateAsync(CancellationToken cancellationToken)
await _context.Database.EnsureCreatedAsync(cancellationToken);
}
}

public async Task SeedDataAsync(CancellationToken cancellationToken)
{
if (!_context.Users.Any())
{
await SeedUsersAsync(cancellationToken);
await SeedOptionsAsync(cancellationToken);
await SeedDungeonsAsync(cancellationToken, 1);
await SeedDungeonsAsync(1, cancellationToken);
}
}

Expand All @@ -40,8 +44,8 @@ public async Task SeedTestBaseAsync(CancellationToken cancellationToken)
{
await SeedUsersAsync(cancellationToken);
await SeedOptionsAsync(cancellationToken);
await SeedDungeonsAsync(cancellationToken, 1);
await SeedDungeonsAsync(cancellationToken, 2);
await SeedDungeonsAsync(1, cancellationToken);
await SeedDungeonsAsync(2, cancellationToken);
}
}

Expand Down Expand Up @@ -345,7 +349,7 @@ private async Task SeedTreasureValueAsync(CancellationToken token)
new()
{
Key = OptionKey.TreasureValue,
Name = Resources.Common.Standard,
Name = Resources.Common.Standard,
Value = "1"
},
new()
Expand Down Expand Up @@ -419,13 +423,12 @@ private async Task SeedSizeAsync(CancellationToken token)
await _context.SaveChangesAsync(token);
}

private async Task SeedDungeonsAsync(CancellationToken token,
int userId)
private async Task SeedDungeonsAsync(int userId, CancellationToken token)
{
var dungeonOption = new DungeonOption
{
UserId = userId,
DungeonName = "Test 1",
DungeonName = UtDungeonName1,
Created = DateTime.UtcNow,
ItemsRarity = 1,
DeadEnd = true,
Expand Down Expand Up @@ -473,7 +476,7 @@ private async Task SeedDungeonsAsync(CancellationToken token,
dungeonOption = new DungeonOption
{
UserId = userId,
DungeonName = "Test 2",
DungeonName = UtDungeonName2,
Created = DateTime.UtcNow,
ItemsRarity = 1,
DeadEnd = true,
Expand Down
18 changes: 0 additions & 18 deletions src/RDMG.Infrastructure/Repository/Automapper/DungeonProfile.cs

This file was deleted.

13 changes: 0 additions & 13 deletions src/RDMG.Infrastructure/Repository/Automapper/UserProfile.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/RDMG.Infrastructure/Repository/DungeonOptionRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using AutoMapper;
using MapsterMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using RDMG.Core.Abstractions.Data;
Expand Down Expand Up @@ -43,7 +43,7 @@ public async Task<DungeonOption> AddDungeonOptionAsync(DungeonOption dungeonOpti

public async Task<DungeonOption?> GetDungeonOptionByNameAsync(string dungeonName, int userId, CancellationToken cancellationToken)
{
return await _context.DungeonOptions
return await _context.DungeonOptions
.Include(d => d.Dungeons)
.AsNoTracking()
.Where(d => d.DungeonName == dungeonName && d.UserId == userId)
Expand Down
Loading