Developer 02 36d763d5e5 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.
2024-08-14 18:49:59 +02:00

51 lines
1.9 KiB
C#

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);
}
}
}