refactor(Application): made IStringLocalizer<Resource>

This commit is contained in:
Developer 02 2025-01-20 14:10:31 +01:00
parent bd7d521c1e
commit 7fc71f427b
4 changed files with 12 additions and 11 deletions

View File

@ -29,6 +29,7 @@
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="DigitalData.Core.Abstractions" Version="3.1.0" />
<PackageReference Include="DigitalData.Core.Application" Version="3.0.1" />
<PackageReference Include="DigitalData.Core.DTO" Version="2.0.1" />
<PackageReference Include="DigitalData.EmailProfilerDispatcher.Abstraction" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.16" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />

View File

@ -10,8 +10,8 @@ namespace DigitalData.UserManager.Application.Services
{
public class GroupService : BaseService<IGroupRepository, GroupCreateDto, GroupReadDto, Group>, IGroupService
{
private readonly IStringLocalizer<Resource> _localizer;
public GroupService(IGroupRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
private readonly IStringLocalizer<Resource>? _localizer;
public GroupService(IGroupRepository repository, IStringLocalizer<Resource>? localizer, IMapper mapper) : base(repository, mapper)
{
_localizer = localizer;
}
@ -25,7 +25,7 @@ namespace DigitalData.UserManager.Application.Services
group.AddedWho = user?.AddedWho ?? "UNAUTHORIZED";
if (await HasEntity(group.Id))
return Result.Fail<int>().Message(_localizer[Key.GroupAlreadyExists.ToString()]);
return Result.Fail<int>().Message(_localizer?[Key.GroupAlreadyExists].Value);
var createdGroup = await _repository.CreateAsync(group);
if (createdGroup is null)

View File

@ -11,8 +11,8 @@ namespace DigitalData.UserManager.Application.Services
{
public class UserRepService : BaseService<IUserRepRepository, UserRepCreateDto, UserRepReadDto, UserRep>, IUserRepService
{
private readonly IStringLocalizer<Resource> _localizer;
public UserRepService(IUserRepRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
private readonly IStringLocalizer<Resource>? _localizer;
public UserRepService(IUserRepRepository repository, IStringLocalizer<Resource>? localizer, IMapper mapper) : base(repository, mapper)
{
_localizer = localizer;
}
@ -27,10 +27,10 @@ namespace DigitalData.UserManager.Application.Services
public override async Task<DataResult<int>> CreateAsync(UserRepCreateDto createDto)
// XOR control
=> (createDto.ValidFrom is null && createDto.ValidTo is not null) || (createDto.ValidFrom is not null && createDto.ValidTo is null)
? Result.Fail<int>().Notice(LogLevel.None, Flag.DataIntegrityIssue, _localizer[Key.DateRangeNotXNOR])
? Result.Fail<int>().Notice(LogLevel.None, Flag.DataIntegrityIssue, _localizer?[Key.DateRangeNotXNOR].Value)
//date range control
: (createDto.ValidFrom > createDto.ValidTo)
? Result.Fail<int>().Notice(LogLevel.None, Flag.DataIntegrityIssue, _localizer[Key.InvalidDateRange])
? Result.Fail<int>().Notice(LogLevel.None, Flag.DataIntegrityIssue, _localizer?[Key.InvalidDateRange].Value)
: await base.CreateAsync(createDto);
}
}

View File

@ -10,8 +10,8 @@ namespace DigitalData.UserManager.Application.Services
{
public class UserService : BaseService<IUserRepository, UserCreateDto, UserReadDto, User>, IUserService
{
private readonly IStringLocalizer<Resource> _localizer;
public UserService(IUserRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
private readonly IStringLocalizer<Resource>? _localizer;
public UserService(IUserRepository repository, IStringLocalizer<Resource>? localizer, IMapper mapper) : base(repository, mapper)
{
_localizer = localizer;
}
@ -49,7 +49,7 @@ namespace DigitalData.UserManager.Application.Services
var user = _mapper.Map<User>(upDto);
if (await HasEntity(user.Id))
return Result.Fail<int>().Message(_localizer[Key.UserAlreadyExists]);
return Result.Fail<int>().Message(_localizer?[Key.UserAlreadyExists].Value);
//set the user
var current_user = await GetUserAsync();
@ -66,7 +66,7 @@ namespace DigitalData.UserManager.Application.Services
{
var user = await _repository.ReadByUsernameAsync(username);
if (user is null)
return Result.Fail<UserReadDto>().Message(_localizer[Key.UserNotFoundInLocalDB]);
return Result.Fail<UserReadDto>().Message(_localizer?[Key.UserNotFoundInLocalDB].Value);
var userDto = _mapper.Map<UserReadDto>(user);
return Result.Success(userDto);