Primera subida
This commit is contained in:
84
back/Infra/Application_Db_Context.cs
Normal file
84
back/Infra/Application_Db_Context.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using back.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace back.Infra
|
||||
{
|
||||
public class Application_Db_Context : DbContext
|
||||
{
|
||||
public Application_Db_Context(DbContextOptions<Application_Db_Context> opt) : base(opt)
|
||||
{ }
|
||||
public virtual DbSet<Persona> Personas { get; set; }
|
||||
public virtual DbSet<Casa> Casas { get; set; }
|
||||
|
||||
public virtual DbSet<Direccion> Direcciones { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
SeedData(modelBuilder); //en Movidas
|
||||
}
|
||||
|
||||
#region Movidas
|
||||
private static void SeedData(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Persona>().HasData(GenerarPersonas());
|
||||
modelBuilder.Entity<Casa>().HasData(GenerarCasas());
|
||||
modelBuilder.Entity<Direccion>().HasData(GenerarDirecciones());
|
||||
}
|
||||
|
||||
private static List<Persona> GenerarPersonas()
|
||||
{
|
||||
var personas = new List<Persona>();
|
||||
|
||||
for (int i = 1; i <= 1000; i++)
|
||||
{
|
||||
personas.Add(new Persona
|
||||
{
|
||||
Id = i,
|
||||
Name = $"Nombre{i}",
|
||||
Surname = $"Apellido{i}",
|
||||
});
|
||||
}
|
||||
|
||||
return personas;
|
||||
}
|
||||
|
||||
private static List<Casa> GenerarCasas()
|
||||
{
|
||||
var casas = new List<Casa>();
|
||||
|
||||
for (int i = 1; i <= 1000; i++)
|
||||
{
|
||||
casas.Add(new Casa
|
||||
{
|
||||
Id = i,
|
||||
NombreCasa = $"Casa{i}",
|
||||
NumeroHabitaciones = new Random().Next(1, 5),
|
||||
DireccionId = i,
|
||||
// Asumimos que PersonaId es una propiedad que debería existir para relacionar con Persona
|
||||
PersonaId = i
|
||||
});
|
||||
}
|
||||
|
||||
return casas;
|
||||
}
|
||||
|
||||
private static List<Direccion> GenerarDirecciones()
|
||||
{
|
||||
var direcciones = new List<Direccion>();
|
||||
|
||||
for (int i = 1; i <= 1000; i++)
|
||||
{
|
||||
direcciones.Add(new Direccion
|
||||
{
|
||||
Id = i,
|
||||
Calle = $"Calle{i}",
|
||||
Numero = new Random().Next(1, 100),
|
||||
});
|
||||
}
|
||||
|
||||
return direcciones;
|
||||
}
|
||||
|
||||
#endregion Movidas
|
||||
}
|
||||
}
|
||||
95
back/Infra/BaseRepository.cs
Normal file
95
back/Infra/BaseRepository.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
using System.Linq;
|
||||
using back.Entities;
|
||||
using back.Application.Contracts.Persistence;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
|
||||
namespace back.Infra
|
||||
{
|
||||
public class BaseRepository<T> : IAsyncRepository<T> where T : BaseEntity
|
||||
{
|
||||
|
||||
|
||||
protected readonly Application_Db_Context context;
|
||||
private readonly IDbContextFactory<Application_Db_Context> _dbContextFactory;
|
||||
public BaseRepository(IDbContextFactory<Application_Db_Context> dbContextFactory, Application_Db_Context _context)
|
||||
{
|
||||
context = _context;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public virtual async Task<IReadOnlyList<T>> GetAllAsync()
|
||||
{
|
||||
using (var context = _dbContextFactory.CreateDbContext())
|
||||
{
|
||||
return await context.Set<T>().ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return await context.Set<T>().Where(predicate).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<IReadOnlyList<T>> GetAsync(Expression<Func<T, bool>>? predicate = null,
|
||||
Func<IQueryable<T>, IOrderedQueryable<T>>? orderBy = null,
|
||||
string includeString = null,
|
||||
bool disableTracking = true)
|
||||
{
|
||||
IQueryable<T> query = context.Set<T>();
|
||||
if (disableTracking) query = query.AsNoTracking();
|
||||
if (!string.IsNullOrWhiteSpace(includeString)) query = query.Include(includeString);
|
||||
if (predicate != null) query = query.Where(predicate);
|
||||
if (orderBy != null)
|
||||
return await orderBy(query).ToListAsync();
|
||||
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public virtual async 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)
|
||||
{
|
||||
IQueryable<T> query = context.Set<T>();
|
||||
if (disableTracking) query = query.AsNoTracking();
|
||||
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
if (predicate != null) query = query.Where(predicate);
|
||||
if (orderBy != null)
|
||||
return await orderBy(query).ToListAsync();
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
|
||||
public virtual async Task<T> GetByIdAsync(int id)
|
||||
{
|
||||
return await context.Set<T>().FindAsync(id);
|
||||
}
|
||||
|
||||
public virtual async Task<T> AddAsync(T entity)
|
||||
{
|
||||
context.Set<T>().Add(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<T> UpdateAsync(T entity)
|
||||
{
|
||||
context.Set<T>().Attach(entity);
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T entity)
|
||||
{
|
||||
context.Set<T>().Remove(entity);
|
||||
await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
53
back/Infra/CasaRepository.cs
Normal file
53
back/Infra/CasaRepository.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using back.Entities;
|
||||
using back.Entities.DTOs.Response;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Identity.Client;
|
||||
|
||||
namespace back.Infra
|
||||
{
|
||||
public class CasaRepository : BaseRepository<Casa>
|
||||
{
|
||||
private readonly Application_Db_Context context;
|
||||
private readonly IDbContextFactory<Application_Db_Context> _dbContextFactory;
|
||||
public CasaRepository(Application_Db_Context _context, IDbContextFactory<Application_Db_Context> dbContextFactory) : base(dbContextFactory, _context)
|
||||
{
|
||||
context = _context;
|
||||
_dbContextFactory = dbContextFactory;
|
||||
}
|
||||
|
||||
public override async Task<Casa> AddAsync(Casa casa)
|
||||
{
|
||||
if(casa.Direccion == null)
|
||||
{
|
||||
var toReturn = await base.AddAsync(casa);
|
||||
return toReturn;
|
||||
}
|
||||
else
|
||||
{
|
||||
await context.Casas.AddAsync(casa);
|
||||
await context.Direcciones.AddAsync(casa.Direccion);
|
||||
await context.SaveChangesAsync();
|
||||
return casa;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IWantItAll> WantItAlls()
|
||||
{
|
||||
var personas = Task.Run(async () => { return await context.Personas.ToListAsync(); });
|
||||
var casas = Task.Run(async () => { return await context.Casas.ToListAsync(); });
|
||||
var direcciones = Task.Run(async () => { return await context.Direcciones.ToListAsync(); });
|
||||
|
||||
await Task.WhenAll(personas, casas, direcciones);
|
||||
|
||||
|
||||
var toReturn = new IWantItAll()
|
||||
{
|
||||
Personas = await personas,
|
||||
Casas = await casas,
|
||||
Direcciones = await direcciones
|
||||
};
|
||||
return toReturn;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
13
back/Infra/DireccionRepository.cs
Normal file
13
back/Infra/DireccionRepository.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using back.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace back.Infra
|
||||
{
|
||||
public class DireccionRepository : BaseRepository<Direccion>
|
||||
{
|
||||
|
||||
public DireccionRepository(Application_Db_Context _context, IDbContextFactory<Application_Db_Context> _dbContextFactory) : base(_dbContextFactory, _context)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
15
back/Infra/PersonaRepository.cs
Normal file
15
back/Infra/PersonaRepository.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using back.Entities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace back.Infra
|
||||
{
|
||||
public class PersonaRepository : BaseRepository<Persona>
|
||||
{
|
||||
public PersonaRepository(Application_Db_Context _context, IDbContextFactory<Application_Db_Context> _dbContextFactory) : base(_dbContextFactory, _context)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user