Primera subida
This commit is contained in:
26
back/Application/Contracts/Persistence/IAsyncRepository.cs
Normal file
26
back/Application/Contracts/Persistence/IAsyncRepository.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using back.Entities;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace back.Application.Contracts.Persistence
|
||||
{
|
||||
public interface IAsyncRepository<T> where T : BaseEntity
|
||||
{
|
||||
Task<IReadOnlyList<T>> GetAllAsync();
|
||||
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>>? predicate);
|
||||
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>>? predicate = null,
|
||||
Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null,
|
||||
string? includeString = null,
|
||||
bool disableTracking = true);
|
||||
Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>>? predicate = null,
|
||||
Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null,
|
||||
List<Expression<Func<T, object>>>? includes = null,
|
||||
bool disableTracking = true);
|
||||
Task<T> GetByIdAsync(int id);
|
||||
|
||||
Task<T> AddAsync(T entity);
|
||||
Task<T> UpdateAsync(T entity);
|
||||
Task DeleteAsync(T entity);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
14
back/Application/Contracts/Services/IBaseService.cs
Normal file
14
back/Application/Contracts/Services/IBaseService.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using back.Entities.DTOs.Request;
|
||||
using back.Entities;
|
||||
|
||||
namespace back.Application.Contracts.Services
|
||||
{
|
||||
public interface IBaseService<T, A, U> where T : class
|
||||
{
|
||||
Task<IEnumerable<T>> GetAll();
|
||||
Task<T> GetOne(int id);
|
||||
Task<T> Add(A dto);
|
||||
Task<bool> Delete(int id);
|
||||
Task<T> Update(U dto);
|
||||
}
|
||||
}
|
||||
10
back/Application/Contracts/Services/ICasaService.cs
Normal file
10
back/Application/Contracts/Services/ICasaService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using back.Entities;
|
||||
using back.Entities.DTOs.Request;
|
||||
using back.Infra;
|
||||
|
||||
namespace back.Application.Contracts.Services
|
||||
{
|
||||
public interface ICasaService : IBaseService<Casa, CasaRequestDto, CasaUpdateRequestDto>
|
||||
{
|
||||
}
|
||||
}
|
||||
9
back/Application/Contracts/Services/IDireccionService.cs
Normal file
9
back/Application/Contracts/Services/IDireccionService.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using back.Entities;
|
||||
using back.Entities.DTOs.Request;
|
||||
|
||||
namespace back.Application.Contracts.Services
|
||||
{
|
||||
public interface IDireccionService:IBaseService<Direccion, DireccionRequestDto, DireccionUpdateRequestDto>
|
||||
{
|
||||
}
|
||||
}
|
||||
11
back/Application/Contracts/Services/IPersonaService.cs
Normal file
11
back/Application/Contracts/Services/IPersonaService.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using back.Entities;
|
||||
using back.Entities.DTOs.Request;
|
||||
using back.Entities.DTOs.Response;
|
||||
|
||||
namespace back.Application.Contracts.Services
|
||||
{
|
||||
public interface IPersonaService : IBaseService<Persona, PersonaRequestDto, PersonaUpdateRequestDto>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
10
back/Application/Contracts/Services/IWantItAllService.cs
Normal file
10
back/Application/Contracts/Services/IWantItAllService.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using back.Entities.DTOs.Response;
|
||||
|
||||
namespace back.Application.Contracts.Services
|
||||
{
|
||||
public interface IWantItAllService
|
||||
{
|
||||
Task<IWantItAll> GetWithAsyncAwait();
|
||||
Task<IWantItAll> GetWithTasks();
|
||||
}
|
||||
}
|
||||
27
back/Application/Mappers/MappingProfile.cs
Normal file
27
back/Application/Mappers/MappingProfile.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using AutoMapper;
|
||||
using back.Entities;
|
||||
using back.Entities.DTOs.Request;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
|
||||
namespace back.Application.Mappers
|
||||
{
|
||||
public class MappingProfile : Profile
|
||||
{
|
||||
public MappingProfile()
|
||||
{
|
||||
#region Mappings
|
||||
CreateMap<Casa, CasaRequestDto>();
|
||||
CreateMap<CasaRequestDto, Casa>();
|
||||
|
||||
CreateMap<Casa, CasaWitDirecctionRequestDto>();
|
||||
CreateMap<CasaWitDirecctionRequestDto, Casa>();
|
||||
|
||||
CreateMap<Persona, PersonaRequestDto>();
|
||||
CreateMap<PersonaRequestDto, Persona>();
|
||||
|
||||
CreateMap<Direccion, DireccionRequestDto>();
|
||||
CreateMap<DireccionRequestDto, Direccion>();
|
||||
#endregion Mappings
|
||||
}
|
||||
}
|
||||
}
|
||||
31
back/Application/Services/CasaService.cs
Normal file
31
back/Application/Services/CasaService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
back/Application/Services/DireccionService.cs
Normal file
23
back/Application/Services/DireccionService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
54
back/Application/Services/GenericService.cs
Normal file
54
back/Application/Services/GenericService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
back/Application/Services/PersonaService.cs
Normal file
22
back/Application/Services/PersonaService.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
57
back/Application/Services/WantItAllService.cs
Normal file
57
back/Application/Services/WantItAllService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user