85 lines
4.0 KiB
C#
85 lines
4.0 KiB
C#
using AlquilaFacil.Builder;
|
|
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Modelo;
|
|
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class NotificacionesController: ControllerBase {
|
|
[HttpGet("api/notificaciones")]
|
|
public IActionResult GetNotificaciones([FromHeader(Name ="Auth")]string Auth, bool leido = false) {
|
|
if (string.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
|
|
var cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return BadRequest(new {message = "Fallo al intentar encontrar tu usuario (puede que te hayas logeado desde otro dispositivo?)"});
|
|
|
|
|
|
IQueryable<Notificacione> notificaciones = RepositorioNotificaciones.Singleton.ObtenerNotificacionesDeUsuario(cli.Dni)
|
|
.Where(x=>x.Leido == leido);
|
|
List<NotificacionDto> noti = new();
|
|
Parallel.ForEach(notificaciones, i => {
|
|
var dto = new NotificacionDtoBuilder()
|
|
.SetRemitente(i.DniremitenteNavigation.Email)
|
|
.SetAccion(i.Accion)
|
|
.SetMensaje(i.Mensaje)
|
|
.SetFecha(i.Fecha)
|
|
.SetPropiedad(i.IdpropiedadNavigation.Id.ToString())
|
|
.SetReceptor(i.DniclienteNavigation.Email)
|
|
.Build();
|
|
|
|
noti.Add(dto);
|
|
});
|
|
return Ok(noti);
|
|
}
|
|
|
|
|
|
[HttpPut("api/notificaciones")]
|
|
public IActionResult MarcarComoLeido([FromHeader(Name = "Auth")]string Auth, NotificacionMarcarLeidoDto nota) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
|
|
if (nota.Fecha == null || String.IsNullOrWhiteSpace(nota.Email)) return BadRequest(new {message = "Faltan datos"});
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
|
|
if (cli == null) return BadRequest(new {message = "El token de autorizacion no pertenese a ningun Usuario"});
|
|
|
|
if (cli.Email != nota.Email) return BadRequest(new {message = "El token de autorizacion no corresponde a tu usuario"});
|
|
|
|
bool ret = RepositorioNotificaciones.Singleton.MarcarComoLeido(cli.Dni, nota.Fecha);
|
|
return ret ?
|
|
Ok(new{message = "Se Marco como leido"}):BadRequest(new{message = "Fallo al marcarse como leido"});
|
|
}
|
|
|
|
[HttpPost("api/notificaciones/consultaAlquiler")]
|
|
public IActionResult ConsultaAlquiler([FromHeader(Name ="Auth")]string Auth, [FromBody] AltaNotificacionDto data) {
|
|
if (String.IsNullOrWhiteSpace(Auth)) return Unauthorized();
|
|
bool validacion1 = RepositorioUsuarios.Singleton.CheckToken(data.Remitente, Auth);
|
|
if (validacion1 == false) return BadRequest(new {message = "el token no corresponde a su usuario"});
|
|
|
|
if (data.Accion == "") return BadRequest(new{message = "El campo Accion esta vacio"});
|
|
if (data.Mensaje == "") return BadRequest(new {message = "El campo Mensaje esta vacio"});
|
|
|
|
Cliente? inq = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (inq == null) return BadRequest(new { message = "no hay un usuario para el cual el token de autorizacion corresponda (vuelvase a logear)" });
|
|
|
|
Propiedade? prop = RepositorioPropiedades.Singleton.ObtenerPropiedadPorId(data.Propiedad);
|
|
if (prop == null || prop.Idestado != 1) return BadRequest(new{message="No hay una propiedad dada de alta para ese id"});
|
|
if (prop.DnipropietarioNavigation == null) return BadRequest(new{message="la propiedad no tiene propietario dado de alto ????"});
|
|
|
|
var noti = new NotificacioneBuilder()
|
|
.SetAccion(data.Accion)
|
|
.SetMensaje(data.Mensaje)
|
|
.SetLeido(false)
|
|
.SetDnicliente(prop.DnipropietarioNavigation.Dni)
|
|
.SetDniremitente(inq.Dni)
|
|
.SetIdpropiedad(prop.Id)
|
|
.SetFecha(DateTime.Now)
|
|
.Build();
|
|
|
|
var ret = RepositorioNotificaciones.Singleton.AltaNotificacion(noti);
|
|
return ret?
|
|
Ok(new {message = "Se envio la notificacion"}):BadRequest(new {message = "Fallo al intentar guardar la notificacion"});
|
|
}
|
|
} |