feat: Basis-DTOs, Service und Controller für automatische Metadatenverwaltung hinzugefügt

- Basis-DTOs für Lese-, Erstellungs- und Aktualisierungsvorgänge erstellt, um die Felder "hinzugefügt von", "hinzugefügt am", "geändert von" und "geändert am" automatisch über Middleware zu ergänzen.
- Diese Basiskomponenten in die Gruppenstruktur integriert.
This commit is contained in:
Developer 02
2024-08-14 18:49:59 +02:00
parent 4746d63aea
commit 36d763d5e5
16 changed files with 240 additions and 50 deletions

View File

@@ -0,0 +1,19 @@
using DigitalData.UserManager.Domain.Entities;
using DigitalData.Core.Abstractions.Application;
using DigitalData.UserManager.Application.DTOs.User;
using DigitalData.UserManager.Application.DTOs.Base;
namespace DigitalData.UserManager.Application.Contracts
{
public interface IBaseService<TCreateDto, TReadDto, TUpdateDto, TBaseEntity> : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TBaseEntity, int>
where TCreateDto : BaseCreateDto
where TReadDto : class
where TUpdateDto : BaseUpdateDto
where TBaseEntity : BaseEntity
{
public Func<Task<UserReadDto?>> UserFactoryAsync { set; }
public Task<UserReadDto?> GetUserAsync();
}
}

View File

@@ -1,11 +1,10 @@
using DigitalData.Core.Abstractions.Application;
using DigitalData.UserManager.Application.DTOs.Group;
using DigitalData.UserManager.Application.DTOs.Group;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.Core.DTO;
namespace DigitalData.UserManager.Application.Contracts
{
public interface IGroupService : ICRUDService<GroupCreateDto, GroupReadDto, GroupUpdateDto, Group, int>
public interface IGroupService : IBaseService<GroupCreateDto, GroupReadDto, GroupUpdateDto, Group>
{
Task<DataResult<int>> CreateAsync(DirectoryGroupDto dirGroup);
}

View File

@@ -0,0 +1,7 @@
namespace DigitalData.UserManager.Application.DTOs.Base
{
public record BaseCreateDto()
{
public string AddedWho { get; set; } = "UNAUTHORIZED";
}
}

View File

@@ -0,0 +1,6 @@
using DigitalData.Core.DTO;
namespace DigitalData.UserManager.Application.DTOs.Base
{
public record BaseReadDto(int Id, string? AddedWho, DateTime? AddedWhen, string? ChangedWho, DateTime? ChangedWhen) : BaseDTO<int>(Id);
}

View File

@@ -0,0 +1,7 @@
namespace DigitalData.UserManager.Application.DTOs.Base
{
public record BaseUpdateDto()
{
public string ChangedWho { get; set; } = "UNAUTHORIZED";
}
}

View File

@@ -1,4 +1,6 @@
namespace DigitalData.UserManager.Application.DTOs.Group
using DigitalData.UserManager.Application.DTOs.Base;
namespace DigitalData.UserManager.Application.DTOs.Group
{
public record GroupCreateDto
(
@@ -7,8 +9,6 @@
bool? Internal,
bool? Active,
string? Comment,
string? AddedWho,
string? ChangedWho,
int EcmFkId
);
) : BaseCreateDto();
}

View File

@@ -1,4 +1,6 @@
namespace DigitalData.UserManager.Application.DTOs.Group
using DigitalData.UserManager.Application.DTOs.Base;
namespace DigitalData.UserManager.Application.DTOs.Group
{
public record GroupReadDto
(
@@ -9,6 +11,8 @@
bool? Active,
string? Comment,
string? AddedWho,
string? ChangedWho
);
DateTime? AddedWhen,
string? ChangedWho,
DateTime? ChangedWhen
) : BaseReadDto(Id, AddedWho, AddedWhen, ChangedWho, ChangedWhen);
}

View File

@@ -1,4 +1,6 @@
namespace DigitalData.UserManager.Application.DTOs.Group
using DigitalData.UserManager.Application.DTOs.Base;
namespace DigitalData.UserManager.Application.DTOs.Group
{
public record GroupUpdateDto
(
@@ -9,5 +11,5 @@
bool? Active,
string? Comment,
string? ChangedWho
);
) : BaseUpdateDto();
}

View File

@@ -0,0 +1,51 @@
using AutoMapper;
using DigitalData.Core.Abstractions.Infrastructure;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.Base;
using DigitalData.UserManager.Application.DTOs.User;
using DigitalData.UserManager.Domain.Entities;
namespace DigitalData.UserManager.Application.Services
{
public class BaseService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TBaseEntity> : CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TBaseEntity, int>, IBaseService<TCreateDto, TReadDto, TUpdateDto, TBaseEntity>
where TCRUDRepository : ICRUDRepository<TBaseEntity, int>
where TCreateDto : BaseCreateDto
where TReadDto : class
where TUpdateDto : BaseUpdateDto
where TBaseEntity : BaseEntity
{
public BaseService(TCRUDRepository repository, IMapper mapper) : base(repository, mapper)
{
}
private Lazy<Task<UserReadDto?>>? _lazyUserAsync = null;
public Func<Task<UserReadDto?>> UserFactoryAsync { set => _lazyUserAsync = new Lazy<Task<UserReadDto?>>(value); }
public async Task<UserReadDto?> GetUserAsync() => _lazyUserAsync is null ? null : await _lazyUserAsync.Value;
public override async Task<DataResult<int>> CreateAsync(TCreateDto createDto)
{
var user = await GetUserAsync();
if(user is not null)
{
createDto.AddedWho = user.Username;
}
return await base.CreateAsync(createDto);
}
public override async Task<Result> UpdateAsync(TUpdateDto updateDto)
{
var user = await GetUserAsync();
if (user is not null)
{
updateDto.ChangedWho = user.Username;
}
return await base.UpdateAsync(updateDto);
}
}
}

View File

@@ -1,5 +1,4 @@
using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.Group;
@@ -9,7 +8,7 @@ using Microsoft.Extensions.Localization;
namespace DigitalData.UserManager.Application.Services
{
public class GroupService : CRUDService<IGroupRepository, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group, int>, IGroupService
public class GroupService : BaseService<IGroupRepository, GroupCreateDto, GroupReadDto, GroupUpdateDto, Group>, IGroupService
{
private readonly IStringLocalizer<Resource> _localizer;
public GroupService(IGroupRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
@@ -17,15 +16,14 @@ namespace DigitalData.UserManager.Application.Services
_localizer = localizer;
}
public override Task<DataResult<int>> CreateAsync(GroupCreateDto createDto)
{
return base.CreateAsync(createDto);
}
public async Task<DataResult<int>> CreateAsync(DirectoryGroupDto adGroup)
{
var group = _mapper.MapOrThrow<Group>(adGroup);
//set the user
var user = await GetUserAsync();
group.AddedWho = user?.AddedWho ?? "UNAUTHORIZED";
if (await HasEntity(group.Id))
return Result.Fail<int>().Message(_localizer[Key.GroupAlreadyExists.ToString()]);