refactor: arreglado formato de los repositorios
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
@@ -4,12 +4,17 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace Modelo;
|
namespace Modelo;
|
||||||
|
|
||||||
public abstract class RepositorioBase<T> where T : class, new()
|
public abstract class RepositorioBase<T, S>
|
||||||
|
where T : class, new()
|
||||||
|
where S : new()
|
||||||
{
|
{
|
||||||
protected AlquilaFacilContext Context { get; set; } = new AlquilaFacilContext();
|
protected AlquilaFacilContext Context { get; set; } = new AlquilaFacilContext();
|
||||||
|
|
||||||
public abstract ReadOnlyCollection<T> Listar();
|
public abstract ReadOnlyCollection<T> Listar();
|
||||||
|
|
||||||
|
private static readonly S instance = new();
|
||||||
|
public static S Singleton { get{return instance;}}
|
||||||
|
|
||||||
public bool Guardar(){
|
public bool Guardar(){
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
try
|
try
|
||||||
@@ -24,5 +29,4 @@ public abstract class RepositorioBase<T> where T : class, new()
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,34 +2,49 @@ using System.Collections.ObjectModel;
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Entidades.Dto;
|
using Entidades.Dto;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Entidades;
|
||||||
|
|
||||||
namespace Modelo;
|
namespace Modelo;
|
||||||
|
|
||||||
public class RepositorioUsuarios: RepositorioBase<Usuario>
|
public class RepositorioUsuarios: RepositorioBase<Usuario, RepositorioUsuarios>
|
||||||
{
|
{
|
||||||
public static RepositorioUsuarios Singleton = new();
|
public bool CheckUsuario(LoginDto logindto) {
|
||||||
public Usuario? CheckUsuario(LoginDto logindto) {
|
|
||||||
|
|
||||||
byte[] Contraseña = HacerHash(logindto.Contraseña);
|
string Contraseña = HacerHash(logindto.Contraseña);
|
||||||
|
|
||||||
|
Usuario? usu = Context.Usuarios.FirstOrDefault(a => a.email == logindto.Email);
|
||||||
|
if (usu == null) return false;
|
||||||
|
|
||||||
|
string hashdb = Encoding.UTF8.GetString(usu.contraseña);
|
||||||
|
|
||||||
|
if (hashdb == Contraseña) return true;
|
||||||
|
return false;
|
||||||
|
|
||||||
bool usu = Context.Inquilinos.Any(x=>x.Email == logindto.Email && x.Contrasena == Contraseña);
|
|
||||||
if (usu){
|
|
||||||
return Context.Inquilinos.FirstOrDefault(x=>x.Email == logindto.Email);
|
|
||||||
}
|
|
||||||
usu = Context.Propietarios.Any(x=>x.Email == logindto.Email && x.Contrasena == Contraseña);
|
|
||||||
if (usu){
|
|
||||||
return Context.Propietarios.FirstOrDefault(x=>x.Email == logindto.Email);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override ReadOnlyCollection<Usuario> Listar()
|
public override ReadOnlyCollection<Usuario> Listar()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
public byte[] HacerHash(string pass){
|
|
||||||
|
public string HacerHash(string pass){
|
||||||
var buf = SHA256.HashData(Encoding.UTF8.GetBytes(pass));
|
var buf = SHA256.HashData(Encoding.UTF8.GetBytes(pass));
|
||||||
return Encoding.UTF8.GetBytes(BitConverter.ToString(buf).Replace("-","").ToLower());
|
return BitConverter.ToString(buf).Replace("-","");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool CheckToken(TokenDto token){
|
||||||
|
var usu = Context.Usuarios.FirstOrDefault(x => x.email == token.Email);
|
||||||
|
if (usu == null) return false;
|
||||||
|
|
||||||
|
return usu.token == token.Token;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GuardarToken(LoginDto login, string tokenString)
|
||||||
|
{
|
||||||
|
var usu = Context.Usuarios.FirstOrDefault(x => x.email == login.Email);
|
||||||
|
if (usu == null) return;
|
||||||
|
usu.token = tokenString;
|
||||||
|
Guardar();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user