Developer 02 3a1aeb7ac3 Refactor namespaces and enhance application structure
This commit reorganizes namespaces from `DigitalData.Core.Abstractions` and `DigitalData.Core.DTO` to `DigitalData.Core.Application.Interfaces` and `DigitalData.Core.Application.DTO`, improving maintainability and clarity.

Updated using directives across multiple files to reflect the new structure, ensuring functionality remains intact.

Project references in `DigitalData.Core.API.csproj` have been consolidated to include the new Application project.

Introduced new classes and interfaces such as `BaseDTO`, `CookieConsentSettings`, `DataResult`, `Notice`, and `Result` to enhance data transfer and service result handling.

Updated `IRepository`, `ICRUDRepository`, and `IEntityMapper` interfaces to facilitate CRUD operations and entity mapping.

Added extension methods in `Extensions.cs` to improve repository usability.

New interfaces for HTTP client services have been added, enhancing external API call handling.

Overall, these changes reflect a significant restructuring aimed at improving organization and preparing for future development.
2025-05-16 11:24:58 +02:00

61 lines
3.1 KiB
C#

using AutoMapper;
using DigitalData.Core.Abstractions;
using Microsoft.Extensions.Logging;
using DigitalData.Core.Application.Interfaces.Repository;
using DigitalData.Core.Application.Interfaces;
using DigitalData.Core.Application.DTO;
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="TEntity">The entity type.</typeparam>
/// <typeparam name="TId">The type of the identifier for the entity.</typeparam>
public class CRUDService<TCRUDRepository, TCreateDto, TReadDto, TEntity, TId> : ReadService<TCRUDRepository, TReadDto, TEntity, TId>, ICRUDService<TCreateDto, TReadDto, TEntity, TId>
where TCRUDRepository : ICRUDRepository<TEntity, TId> where TCreateDto : class where TReadDto : class where TEntity : class, IUnique<TId>
{
/// <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) : base(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 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>(TUpdateDto updateDto) where TUpdateDto : IUnique<TId>
{
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();
}
}
}