327 lines
14 KiB
C#
327 lines
14 KiB
C#
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Modelo;
|
|
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class PropiedadesController: ControllerBase {
|
|
[HttpGet("api/propiedades")]
|
|
public IActionResult ListarPropiedades([FromHeader(Name = "Auth")] string Auth, int pag = 0) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 10);
|
|
if (validacion1 == false) validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
IQueryable<PropiedadesDto> ret;
|
|
|
|
if (pag == 0){
|
|
ret = RepositorioPropiedades.Singleton.ListarPropiedades();
|
|
} else{
|
|
ret = RepositorioPropiedades.Singleton.ListarPropiedadesPorPagina(pag);
|
|
}
|
|
|
|
return Ok(ret);
|
|
}
|
|
|
|
[HttpGet("/api/propiedades/Venta")]
|
|
public IActionResult ObtenerPropiedadesParaVenta([FromHeader(Name = "Auth")] string Auth, int pag = 0) {
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) {
|
|
return Unauthorized();
|
|
}
|
|
|
|
if (pag<=0) return BadRequest(new { message = "no existe una pagina 0"});
|
|
|
|
pag-=1;
|
|
|
|
var props = RepositorioPropiedades.Singleton.ObtenerPropiedadesEnVenta(pag);
|
|
if (props == null) return BadRequest(new { message = "no tengo claro que fallo creo que no existen propiedades en venta"});
|
|
|
|
List<PropiedadesVentaDto> l = new();
|
|
|
|
foreach (var i in props) {
|
|
var p = new PropiedadesVentaDto{
|
|
Id = i.Id,
|
|
Ubicacion = i.Ubicacion,
|
|
Canthabitaciones = i.Canthabitaciones,
|
|
Divisa = i.IddivisaNavigation.Signo,
|
|
Letra = i.Letra??"",
|
|
Monto = i.Monto,
|
|
Piso = i.Piso??0,
|
|
Servicios =string.Join(", ", i.IdServicios.Select(s => s.Descripcion)),
|
|
Tipo = i.IdtipropiedadNavigation.Descripcion,
|
|
};
|
|
l.Add(p);
|
|
}
|
|
|
|
int cantpag = RepositorioPropiedades.Singleton.ObtenerPaginasDePropiedadesEnVenta();
|
|
|
|
return Ok(new { propiedades = l, cantpaginas = cantpag});
|
|
}
|
|
|
|
[HttpGet("api/propiedades/Venta/Propietario")]
|
|
public IActionResult ObtenerPropiedadesVentaDePropietario( [FromHeader(Name = "Auth")] string Auth){
|
|
var validacion1 = RepositorioGrupos.Singleton.CheckGrupos(Auth, "Propietario");
|
|
if (validacion1 == false) {
|
|
return Unauthorized();
|
|
}
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return Unauthorized();
|
|
|
|
var props = RepositorioPropiedades.Singleton.ObtenerPropiedadesAVentaPorDni(cli.Dni);
|
|
List<PropiedadesDto> ll = new();
|
|
|
|
foreach (var i in props) {
|
|
var a = new PropiedadesDto {
|
|
id = i.Id,
|
|
Ubicacion = i.Ubicacion,
|
|
canthabitaciones = i.Canthabitaciones,
|
|
Iddivisa = i.Iddivisa,
|
|
letra = i.Letra??"",
|
|
Monto = (int)i.Monto, //mmmm
|
|
piso = i.Piso??0,
|
|
Servicios = string.Join(", ", i.IdServicios.Select(x => x.Descripcion)),
|
|
Tipo = i.IdtipropiedadNavigation.Descripcion,
|
|
};
|
|
ll.Add(a);
|
|
}
|
|
return Ok(ll);
|
|
}
|
|
|
|
[HttpGet("api/propiedad")]
|
|
public IActionResult ObtenerPropiedadPorId(int Id, [FromHeader(Name = "Auth")] string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 10);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (Id < 0) return BadRequest(new {message ="la id de propiedad no puede ser negativa"});
|
|
|
|
var ret = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(Id);
|
|
if (ret == null) return BadRequest(new {message ="No existe la propiedad"});
|
|
return Ok(ret);
|
|
}
|
|
|
|
[HttpGet("api/propiedad/cantPagina")]
|
|
public IActionResult ObtenerCantidadDePaginas([FromHeader(Name = "Auth")] string Auth, int estado = 0) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 10);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (estado < 0) return BadRequest(new {message = "No puede tener un numero menor a 0"});
|
|
int cant;
|
|
|
|
if(estado == 0){
|
|
cant = RepositorioPropiedades.Singleton.CuantasPaginas();
|
|
}else{
|
|
cant = RepositorioPropiedades.Singleton.CuantasPaginas(estado);
|
|
}
|
|
|
|
return Ok(new {message = cant});
|
|
|
|
}
|
|
|
|
[HttpGet("api/propiedades/Propietario")]
|
|
public IActionResult ObtenerPropiedadesPorPropietario (
|
|
[FromHeader(Name = "Email")] string email,
|
|
[FromHeader(Name = "Auth")] string Auth) {
|
|
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
email = email.Trim();
|
|
if (String.IsNullOrEmpty(email)) return BadRequest(new {message ="falta campo email"});
|
|
|
|
IQueryable<PropiedadesDto> ret = RepositorioPropiedades.Singleton.ObtenerPropiedadesPorEmail(email);
|
|
return Ok(ret);
|
|
}
|
|
|
|
[HttpGet("api/propiedades/Propietario/Bajas")]
|
|
public IActionResult ObtenerPropiedadesPorPropietarioBajas (
|
|
[FromHeader(Name = "Email")] string email,
|
|
[FromHeader(Name = "Auth")] string Auth) {
|
|
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 8);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
email = email.Trim();
|
|
if (String.IsNullOrEmpty(email)) return BadRequest(new {message ="falta campo email"});
|
|
|
|
IQueryable<PropiedadesDto> ret = RepositorioPropiedades.Singleton.ObtenerPropiedadesDeBajaPorEmail(email);
|
|
return Ok(ret);
|
|
}
|
|
|
|
[HttpPost("api/propiedad")]
|
|
public IActionResult AltaPropiedad([FromBody] AltaPropiedadDto propiedad, [FromHeader(Name = "Auth")] string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 1);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
string validacion2 = ValidarPropiedad(propiedad);
|
|
if (validacion2 != "") return BadRequest(new { message = validacion2 });
|
|
|
|
Cliente? cli = RepositorioPropietario.Singleton.ObtenerPropietarioPorEmail(propiedad.Email);
|
|
if (cli == null) return BadRequest(new { message = "El email no corresponde a un propietario"});
|
|
|
|
Propiedade Prop = new Propiedade{
|
|
Canthabitaciones = propiedad.Canthabitaciones,
|
|
Dnipropietario = cli.Dni,
|
|
Idtipropiedad = propiedad.Idtipropiedad,
|
|
Ubicacion = propiedad.Ubicacion,
|
|
Letra = propiedad.Letra ?? null,
|
|
Piso = propiedad.Piso ?? null,
|
|
Monto = propiedad.Monto,
|
|
Iddivisa = propiedad.Iddivisa,
|
|
};
|
|
|
|
var ret = RepositorioPropiedades.Singleton.AñadirPropiedad(Prop);
|
|
return (ret)?
|
|
Ok(new { message = "Fue Cargado Correctamente"}) :
|
|
BadRequest(new { message = "Fallo al momento de añadir la propiedad a la base de datos"});
|
|
}
|
|
|
|
[HttpPatch("api/propiedad")]
|
|
public IActionResult PatchPropiedad([FromBody] PatchPropiedadDto propiedad, [FromHeader(Name = "Auth")] string Auth) {
|
|
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
string validacion2 = ValidarPropiedad(propiedad);
|
|
if (validacion2 != "") return BadRequest(new { message = validacion2 });
|
|
|
|
Cliente? cli = RepositorioPropietario.Singleton.ObtenerPropietarioPorEmail(propiedad.Email);
|
|
if (cli == null) return BadRequest(new { message = "El email no corresponde a un propietario"});
|
|
|
|
List<Servicio> servs = RepositorioServicios.Singleton.ObtenerServiciosPorDescripcion(propiedad.Servicios);
|
|
|
|
Propiedade Prop = new Propiedade{
|
|
Id = propiedad.id,
|
|
Canthabitaciones = propiedad.Canthabitaciones,
|
|
Dnipropietario = cli.Dni,
|
|
Idtipropiedad = propiedad.tipo,
|
|
Ubicacion = propiedad.Ubicacion,
|
|
Letra = propiedad.Letra ?? null,
|
|
Piso = propiedad.Piso ?? null,
|
|
IdServicios = servs,
|
|
Monto = propiedad.Monto,
|
|
Iddivisa = propiedad.Iddivisa,
|
|
};
|
|
|
|
bool ret = RepositorioPropiedades.Singleton.PatchPropiedad(Prop, cli.Dni);
|
|
return (ret)?
|
|
Ok(new {message = "Fue modificado Correctamente"}):
|
|
BadRequest(new {message = "Fallo al modificar la base de datos"});
|
|
}
|
|
|
|
[HttpDelete("api/propiedad")]
|
|
public IActionResult BajaPropiedad(int id, [FromHeader(Name = "Auth")] string Auth, [FromHeader(Name = "Email")] string email){
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (String.IsNullOrEmpty(email)) return BadRequest(new { message = "Fallo al identificarse el usuario"});
|
|
if (id <= 0) return BadRequest(new { message = "No es una id valida"});
|
|
|
|
Cliente? propie = RepositorioPropietario.Singleton.ObtenerPropietarioPorEmail(email);
|
|
var ret = RepositorioPropiedades.Singleton.BajaPropiedad(id, propie);
|
|
|
|
return ret ?
|
|
Ok(new { message = $"Se Cambio el estado de la propiedad con id {id}"}):
|
|
BadRequest(new {message="Fallo al cambiar el estado de la propiedad"});
|
|
}
|
|
|
|
[HttpPut("api/propiedades/addServicio")]
|
|
public IActionResult AñadirServicio([FromBody] ServicioAPropiedadDto Servicios, [FromHeader(Name = "Auth")] string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (Servicios.propiedadid <= 0) return BadRequest(new {message ="No puede tener una id negativa o cero"});
|
|
if (Servicios.idServicios.Count() < 1) return BadRequest(new {message ="Falta añadir servicios"});
|
|
if (Servicios.idServicios.Any(x => x<= 0)) return BadRequest(new {message ="No tienen haber ids negativas o cero de servicio"});
|
|
|
|
var serv = RepositorioServicios.Singleton.ObtenerServiciosPorPropiedad(Servicios.propiedadid);
|
|
|
|
bool validacion2 = Servicios.idServicios.Any(x => serv.Contains(x));
|
|
|
|
if (validacion2 == true) return BadRequest(new {message ="Hay elementos repetidos"});
|
|
|
|
bool ret = RepositorioPropiedades.
|
|
Singleton.AñadirServicioAPropiedad(Servicios.propiedadid, Servicios.idServicios);
|
|
|
|
return ret ?
|
|
Ok(new {message ="Los Servicios Se Cargaron correctamente a la propiedad"}) : BadRequest(new {message ="No se pudo Cargar los Servicios a la propiedad"});
|
|
|
|
}
|
|
|
|
[HttpPut("api/propiedades/RmServicio")]
|
|
public IActionResult EliminarServicio([FromBody] ServicioAPropiedadDto servicio, [FromHeader(Name = "Auth")] string Auth) {
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 2);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
if (servicio.propiedadid <= 0) return BadRequest(new {message ="No puede tener una id negativa o cero"});
|
|
if (servicio.idServicios.Count() < 1) return BadRequest(new {message ="Falta añadir servicios"});
|
|
if (servicio.idServicios.Any(x => x<= 0)) return BadRequest(new {message ="No tienen haber ids negativas o cero de servicio"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
|
|
var serv = RepositorioServicios.Singleton.ObtenerServiciosPorPropiedad(servicio.propiedadid);
|
|
|
|
var repetidos = serv.Intersect(servicio.idServicios);
|
|
|
|
bool ret = RepositorioPropiedades.Singleton.BajaServiciosAPropiedad(servicio.propiedadid, servicio.idServicios, cli.Dni);
|
|
|
|
return ret ?
|
|
Ok(new {message ="Se Eliminaron los servicios seleccionados de la propiedad"}) : BadRequest(new {message ="Fallo al eliminarse los servicios de la propiedad"});
|
|
|
|
}
|
|
|
|
private string ValidarPropiedad(AltaPropiedadDto prop) {
|
|
if (prop == null) return "Esta mal formado el body de la request";
|
|
|
|
string ret = "";
|
|
if (String.IsNullOrEmpty(prop.Email)) ret += "Falta Definir un email de propietario\n";
|
|
|
|
if (prop.Canthabitaciones < 0) ret += "No se puede tener una cantidad de habitaciones negativa\n";
|
|
|
|
if (prop.Idtipropiedad <= 0) ret += "No tiene un tipo de propiedad asociada\n";
|
|
|
|
if (String.IsNullOrEmpty(prop.Ubicacion)) ret += "Tiene que definir la ubicacion de la propiedad\n";
|
|
|
|
if (prop.Monto<=1) ret += "El monto tiene que ser como minimo mayor a 0";
|
|
|
|
if (prop.Iddivisa<0 || prop.Iddivisa>1) ret += "se tiene que elejir entre AR$ y US$";
|
|
|
|
return ret;
|
|
|
|
}
|
|
private string ValidarPropiedad(PatchPropiedadDto prop) {
|
|
if (prop == null) return "Esta mal formado el body de la request";
|
|
|
|
string ret = "";
|
|
if (prop.id <1) ret += "No Cargo el dato de id";
|
|
if (String.IsNullOrEmpty(prop.Email)) ret += "Falta Definir un email de propietario\n";
|
|
|
|
if (prop.id <1 ) ret += "No puede haber una id menor a 1\n";
|
|
|
|
if (prop.Canthabitaciones < 0) ret += "No se puede tener una cantidad de habitaciones negativa\n";
|
|
|
|
if (prop.tipo <= 0) ret += "No tiene un tipo de propiedad asociada\n";
|
|
|
|
if (String.IsNullOrEmpty(prop.Ubicacion)) ret += "Tiene que definir la ubicacion de la propiedad\n";
|
|
|
|
if (prop.Monto<=1) ret += "El monto tiene que ser como minimo mayor a 0";
|
|
|
|
if (prop.Iddivisa<0 || prop.Iddivisa>1) ret += "se tiene que elejir entre AR$ y US$";
|
|
|
|
return ret;
|
|
|
|
}
|
|
}
|