Die Implementierung der HasEntity-Methode wurde ersetzt, um die CountAsync-Methode des Repositories zu verwenden. Dies sorgt für eine effizientere Überprüfung der Existenz von Entitäten.
113 lines
5.4 KiB
C#
113 lines
5.4 KiB
C#
using DigitalData.Core.Abstractions.Application;
|
|
using DigitalData.Core.Abstractions.Infrastructure;
|
|
using AutoMapper;
|
|
using DigitalData.Core.DTO;
|
|
using DigitalData.Core.Abstractions;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace DigitalData.Core.Application
|
|
{
|
|
/// <summary>
|
|
/// Provides generic CRUD (Create, Read, Update, Delete) operations for a specified type of entity.
|
|
/// </summary>
|
|
/// <typeparam name="TCreateDto">The DTO type for create operations.</typeparam>
|
|
/// <typeparam name="TReadDto">The DTO type for read operations.</typeparam>
|
|
/// <typeparam name="TUpdateDto">The DTO type for update operations.</typeparam>
|
|
/// <typeparam name="TEntity">The entity type.</typeparam>
|
|
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
|
|
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TUpdateDto, TEntity, TId> : ICRUDService<TCreateDto, TReadDto, TUpdateDto, TEntity, TId>
|
|
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TUpdateDto : IUnique<TId> where TEntity : class, IUnique<TId>
|
|
{
|
|
protected readonly TCRUDRepository _repository;
|
|
protected readonly IMapper _mapper;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper.
|
|
/// </summary>
|
|
/// <param name="repository">The CRUD repository for accessing the database.</param>
|
|
/// <param name="mapper">The AutoMapper instance for mapping between DTOs and entity objects.</param>
|
|
public CRUDService(TCRUDRepository repository, IMapper mapper)
|
|
{
|
|
_repository = repository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously creates an entity based on the provided create DTO.
|
|
/// </summary>
|
|
/// <param name="createDto">The DTO to create an entity from.</param>
|
|
/// <returns>A service result indicating success or failure, including the entity DTO.</returns>
|
|
public virtual async Task<DataResult<TId>> CreateAsync(TCreateDto createDto)
|
|
{
|
|
var entity = _mapper.Map<TEntity>(createDto);
|
|
var createdEntity = await _repository.CreateAsync(entity);
|
|
return createdEntity is null ? Result.Fail<TId>() : Result.Success(createdEntity.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously reads an entity by its identifier and maps it to a read DTO.
|
|
/// </summary>
|
|
/// <param name="id">The identifier of the entity to read.</param>
|
|
/// <returns>A service result indicating success or failure, including the read DTO if successful.</returns>
|
|
public virtual async Task<DataResult<TReadDto>> ReadByIdAsync(TId id)
|
|
{
|
|
var entity = await _repository.ReadByIdAsync(id);
|
|
return entity is null
|
|
? Result.Fail<TReadDto>()
|
|
: Result.Success(_mapper.Map<TReadDto>(entity));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously reads all entities and maps them to read DTOs.
|
|
/// </summary>
|
|
/// <returns>A service result including a collection of read DTOs.</returns>
|
|
public virtual async Task<DataResult<IEnumerable<TReadDto>>> ReadAllAsync()
|
|
{
|
|
var entities = await _repository.ReadAllAsync();
|
|
var readDto = _mapper.Map<IEnumerable<TReadDto>>(entities);
|
|
return Result.Success(readDto);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously updates an entity based on the provided update DTO.
|
|
/// </summary>
|
|
/// <param name="updateDto">The DTO to update an entity from.</param>
|
|
/// <returns>A service message indicating success or failure.</returns>
|
|
public virtual async Task<Result> UpdateAsync(TUpdateDto updateDto)
|
|
{
|
|
var currentEntitiy = await _repository.ReadByIdAsync(updateDto.Id);
|
|
|
|
if (currentEntitiy is null)
|
|
return Result.Fail().Notice(LogLevel.Warning, Flag.NotFound, $"{updateDto.Id} is not found in update process of {GetType()} entity.");
|
|
|
|
var entity = _mapper.Map(updateDto, currentEntitiy);
|
|
|
|
return await _repository.UpdateAsync(entity)
|
|
? Result.Success()
|
|
: Result.Fail();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously deletes an entity by its identifier.
|
|
/// </summary>
|
|
/// <param name="id">The identifier of the entity to delete.</param>
|
|
/// <returns>A service message indicating success or failure.</returns>
|
|
public virtual async Task<Result> DeleteAsyncById(TId id)
|
|
{
|
|
TEntity? entity = await _repository.ReadByIdAsync(id);
|
|
|
|
if (entity is null)
|
|
return Result.Fail();
|
|
|
|
bool isDeleted = await _repository.DeleteAsync(entity);
|
|
return isDeleted ? Result.Success() : Result.Fail();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Asynchronously checks if an entity with the specified identifier exists.
|
|
/// </summary>
|
|
/// <param name="id">The identifier of the entity to check.</param>
|
|
/// <returns>A Task that represents the asynchronous operation. The task result contains a boolean value indicating whether the entity exists.</returns>
|
|
public virtual async Task<bool> HasEntity(TId id) => await _repository.CountAsync(id) > 0;
|
|
}
|
|
} |