Primera subida

This commit is contained in:
Alejandro Sarmiento
2024-04-06 03:32:16 +02:00
parent c97b4a8179
commit b672887299
44 changed files with 44426 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
using AutoMapper;
using back.Entities.DTOs.Request;
using back.Entities;
using back.Infra;
using back.Application.Contracts.Persistence;
namespace back.Application.Services
{
public class CasaService : GenericService<Casa, CasaRequestDto, CasaUpdateRequestDto>
{
private readonly IMapper _mapper;
private readonly DireccionService dService;
private readonly IAsyncRepository<Casa> repo;
public CasaService(IMapper mapper, IAsyncRepository<Casa> _repo, DireccionService dService) : base(mapper, _repo)
{
_mapper = mapper;
this.dService = dService;
repo = _repo;
}
public async override Task<Casa> Add(CasaRequestDto dto)
{
var direccion = _mapper.Map<Direccion>(dto.Direction);
var d = await dService.AddOne(direccion);
var casa = _mapper.Map<Casa>(dto);
casa.Direccion = d;
return await repo.AddAsync(casa);
}
}
}

View File

@@ -0,0 +1,23 @@
using AutoMapper;
using back.Entities.DTOs.Request;
using back.Entities;
using back.Infra;
using back.Application.Contracts.Persistence;
namespace back.Application.Services
{
public class DireccionService : GenericService<Direccion, DireccionRequestDto, DireccionUpdateRequestDto>
{
private readonly IAsyncRepository<Direccion> repo;
public DireccionService(IMapper mapper, IAsyncRepository<Direccion> _repo) : base(mapper, _repo)
{
repo = _repo;
}
public async Task<Direccion> AddOne(Direccion direccion)
{
await repo.AddAsync(direccion);
return direccion;
}
}
}

View File

@@ -0,0 +1,54 @@
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;
}
}
}

View File

@@ -0,0 +1,22 @@
using AutoMapper;
using back.Application.Contracts.Persistence;
using back.Application.Contracts.Services;
using back.Entities;
using back.Entities.DTOs.Request;
using back.Entities.DTOs.Response;
using back.Infra;
using System.Runtime.CompilerServices;
namespace back.Application.Services
{
public class PersonaService : GenericService<Persona, PersonaRequestDto, PersonaUpdateRequestDto>
{
public PersonaService(IMapper mapper, IAsyncRepository<Persona> repo) : base(mapper, repo)
{
}
}
}

View File

@@ -0,0 +1,57 @@
using back.Entities.DTOs.Response;
using back.Entities;
using back.Application.Contracts.Services;
using back.Application.Contracts.Persistence;
namespace back.Application.Services
{
public class WantItAllService : IWantItAllService
{
private readonly IAsyncRepository<Persona> personaRepo;
private readonly IAsyncRepository<Casa> casaRepo;
private readonly IAsyncRepository<Direccion> direccionRepo;
public WantItAllService(IAsyncRepository<Persona> personaRepo, IAsyncRepository<Casa> casaRepo, IAsyncRepository<Direccion> direccionRepo)
{
this.personaRepo = personaRepo;
this.casaRepo = casaRepo;
this.direccionRepo = direccionRepo;
}
public async Task<IWantItAll> GetWithAsyncAwait()
{
var personas = await personaRepo.GetAllAsync();
var casas = await casaRepo.GetAllAsync();
var direcciones = await direccionRepo.GetAllAsync();
var toReturn = new IWantItAll
{
Personas = personas.ToList(),
Casas = casas.ToList(),
Direcciones = direcciones.ToList(),
};
return toReturn;
}
public async Task<IWantItAll> GetWithTasks()
{
var personas = personaRepo.GetAllAsync();
var casas = casaRepo.GetAllAsync();
var direcciones = direccionRepo.GetAllAsync();
await Task.WhenAll(personas, casas, direcciones);
IReadOnlyList<Persona> personas1 = await personas;
IReadOnlyList<Casa> casas1 = await casas;
IReadOnlyList<Direccion> direccions = await direcciones;
var toReturn = new IWantItAll
{
Personas = personas1.ToList(),
Casas = casas1.ToList(),
Direcciones = direccions.ToList(),
};
return toReturn;
}
}
}