diff --git a/Aspnet/Controllers/PropiedadesController.cs b/Aspnet/Controllers/PropiedadesController.cs index 5b03bbc..f91f4aa 100644 --- a/Aspnet/Controllers/PropiedadesController.cs +++ b/Aspnet/Controllers/PropiedadesController.cs @@ -6,9 +6,11 @@ using Modelo; namespace AlquilaFacil.Controllers; [ApiController] -public class PropiedadesController: ControllerBase { +public class PropiedadesController : ControllerBase +{ [HttpGet("api/propiedades")] - public IActionResult ListarPropiedades([FromHeader(Name = "Auth")] string Auth, int pag = 0) { + 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); @@ -16,9 +18,12 @@ public class PropiedadesController: ControllerBase { IQueryable ret; - if (pag == 0){ + if (pag == 0) + { ret = RepositorioPropiedades.Singleton.ListarPropiedades(); - } else{ + } + else + { ret = RepositorioPropiedades.Singleton.ListarPropiedadesPorPagina(pag); } @@ -26,31 +31,35 @@ public class PropiedadesController: ControllerBase { } [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) { + public IActionResult ObtenerPropiedadesParaVenta([FromHeader(Name = "Auth")] string Auth, int pag = 0) + { + var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 16); + if (validacion1 == false) + { return Unauthorized(); } - if (pag<=0) return BadRequest(new { message = "no existe una pagina 0"}); + if (pag <= 0) return BadRequest(new { message = "no existe una pagina 0" }); - pag-=1; + 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"}); + if (props == null) return BadRequest(new { message = "no tengo claro que fallo creo que no existen propiedades en venta" }); List l = new(); - foreach (var i in props) { - var p = new PropiedadesVentaDto{ + 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??"", + Letra = i.Letra ?? "", Monto = i.Monto, - Piso = i.Piso??0, - Servicios =string.Join(", ", i.IdServicios.Select(s => s.Descripcion)), + Piso = i.Piso ?? 0, + Servicios = string.Join(", ", i.IdServicios.Select(s => s.Descripcion)), Tipo = i.IdtipropiedadNavigation.Descripcion, }; l.Add(p); @@ -58,31 +67,35 @@ public class PropiedadesController: ControllerBase { int cantpag = RepositorioPropiedades.Singleton.ObtenerPaginasDePropiedadesEnVenta(); - return Ok(new { propiedades = l, cantpaginas = cantpag}); + 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) { + public IActionResult ObtenerPropiedadesVentaDePropietario([FromHeader(Name = "Auth")] string Auth) + { + var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 15); + if (validacion1 == false) + { return Unauthorized(); } - + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); if (cli == null) return Unauthorized(); - var props = RepositorioPropiedades.Singleton.ObtenerPropiedadesAVentaPorDni(cli.Dni); + var props = RepositorioPropiedades.Singleton.ObtenerPropiedadesAVentaPorDni(cli.Dni); List ll = new(); - foreach (var i in props) { - var a = new PropiedadesDto { + foreach (var i in props) + { + var a = new PropiedadesDto + { id = i.Id, Ubicacion = i.Ubicacion, canthabitaciones = i.Canthabitaciones, Iddivisa = i.Iddivisa, - letra = i.Letra??"", + letra = i.Letra ?? "", Monto = (int)i.Monto, //mmmm - piso = i.Piso??0, + piso = i.Piso ?? 0, Servicios = string.Join(", ", i.IdServicios.Select(x => x.Descripcion)), Tipo = i.IdtipropiedadNavigation.Descripcion, }; @@ -92,71 +105,79 @@ public class PropiedadesController: ControllerBase { } [HttpGet("api/propiedad")] - public IActionResult ObtenerPropiedadPorId(int Id, [FromHeader(Name = "Auth")] string Auth) { + 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"}); + 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"}); + 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) { + 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"}); + if (estado < 0) return BadRequest(new { message = "No puede tener un numero menor a 0" }); int cant; - if(estado == 0){ + if (estado == 0) + { cant = RepositorioPropiedades.Singleton.CuantasPaginas(); - }else{ + } + else + { cant = RepositorioPropiedades.Singleton.CuantasPaginas(estado); } - - return Ok(new {message = cant}); - + + return Ok(new { message = cant }); + } [HttpGet("api/propiedades/Propietario")] - public IActionResult ObtenerPropiedadesPorPropietario ( + public IActionResult ObtenerPropiedadesPorPropietario( [FromHeader(Name = "Email")] string email, - [FromHeader(Name = "Auth")] string Auth) { + [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"}); + if (String.IsNullOrEmpty(email)) return BadRequest(new { message = "falta campo email" }); IQueryable ret = RepositorioPropiedades.Singleton.ObtenerPropiedadesPorEmail(email); return Ok(ret); } [HttpGet("api/propiedades/Propietario/Bajas")] - public IActionResult ObtenerPropiedadesPorPropietarioBajas ( + public IActionResult ObtenerPropiedadesPorPropietarioBajas( [FromHeader(Name = "Email")] string email, - [FromHeader(Name = "Auth")] string Auth) { + [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"}); + if (String.IsNullOrEmpty(email)) return BadRequest(new { message = "falta campo email" }); IQueryable ret = RepositorioPropiedades.Singleton.ObtenerPropiedadesDeBajaPorEmail(email); return Ok(ret); } [HttpPost("api/propiedad")] - public IActionResult AltaPropiedad([FromBody] AltaPropiedadDto propiedad, [FromHeader(Name = "Auth")] string Auth) { + 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(); @@ -165,9 +186,10 @@ public class PropiedadesController: ControllerBase { 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"}); + if (cli == null) return BadRequest(new { message = "El email no corresponde a un propietario" }); - Propiedade Prop = new Propiedade{ + Propiedade Prop = new Propiedade + { Canthabitaciones = propiedad.Canthabitaciones, Dnipropietario = cli.Dni, Idtipropiedad = propiedad.Idtipropiedad, @@ -179,27 +201,29 @@ public class PropiedadesController: ControllerBase { }; 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"}); + 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) { - + 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"}); + if (cli == null) return BadRequest(new { message = "El email no corresponde a un propietario" }); List servs = RepositorioServicios.Singleton.ObtenerServiciosPorDescripcion(propiedad.Servicios); - - Propiedade Prop = new Propiedade{ + + Propiedade Prop = new Propiedade + { Id = propiedad.id, Canthabitaciones = propiedad.Canthabitaciones, Dnipropietario = cli.Dni, @@ -213,61 +237,64 @@ public class PropiedadesController: ControllerBase { }; 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"}); - } + 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){ + 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"}); + 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"}); - } + 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) { + 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"}); + 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"}); + 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"}); + 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) { + 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"}); + 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); @@ -276,49 +303,51 @@ public class PropiedadesController: ControllerBase { 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"}); + 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) { + 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$"; + + 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) { + 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 (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.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$"; + 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;