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 : CRUDService, IBaseService where TCRUDRepository : ICRUDRepository where TCreateDto : BaseCreateDto where TReadDto : class where TBaseEntity : BaseEntity { public BaseService(TCRUDRepository repository, IMapper mapper) : base(repository, mapper) { } private Lazy>? _lazyUserAsync = null; public Func> UserFactoryAsync { set => _lazyUserAsync = new Lazy>(value); } public async Task GetUserAsync() => _lazyUserAsync is null ? null : await _lazyUserAsync.Value; public override async Task> CreateAsync(TCreateDto createDto) { var user = await GetUserAsync(); if(user is not null) { createDto.AddedWho = user.Username; } return await base.CreateAsync(createDto); } // made without generic type public override async Task UpdateAsync(TUpdateDto updateDto) { var user = await GetUserAsync(); if (user is not null && updateDto is BaseUpdateDto baseUpdateDto) { baseUpdateDto.ChangedWho = user.Username; } return await base.UpdateAsync(updateDto); } } }