Files
PersonaCasa/back/Application/Services/GenericService.cs
Alejandro Sarmiento b672887299 Primera subida
2024-04-06 03:32:16 +02:00

55 lines
1.5 KiB
C#

using AutoMapper;
using back.Application.Contracts.Persistence;
using back.Application.Contracts.Services;
using back.Entities;
using back.Infra;
namespace back.Application.Services
{
public class GenericService<T, A, U> : IBaseService<T, A, U> where T : BaseEntity
{
private readonly IMapper mapper;
private readonly IAsyncRepository<T> repo;
public GenericService(IMapper mapper, IAsyncRepository<T> repo)
{
this.mapper = mapper;
this.repo = repo;
}
public virtual async Task<T> Add(A dto)
{
T entity = mapper.Map<T>(dto);
return await repo.AddAsync(entity);
}
public virtual async Task<bool> Delete(int id)
{
T entityExists = await repo.GetByIdAsync(id);
if(entityExists == null)
{
return false;
}
await repo.DeleteAsync(entityExists);
return true;
}
public virtual async Task<IEnumerable<T>> GetAll()
{
return await repo.GetAllAsync();
}
public virtual async Task<T> GetOne(int id)
{
return await repo.GetByIdAsync(id);
}
public virtual async Task<T> Update(U dto)
{
T entity = mapper.Map<T>(dto);
await repo.UpdateAsync(entity);
return entity;
}
}
}