111 lines
4.2 KiB
C#
111 lines
4.2 KiB
C#
using AlquilaFacil.Builder;
|
|
using Entidades;
|
|
using Entidades.Dto;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Modelo;
|
|
|
|
namespace AlquilaFacil.Controllers;
|
|
|
|
[ApiController]
|
|
public class DefectoController : ControllerBase
|
|
{
|
|
|
|
[HttpGet("api/defectos")]
|
|
public IActionResult ObtenerDefectosEnContrato([FromHeader(Name = "Auth")] string Auth, long Idcontrato = 0)
|
|
{
|
|
if (Idcontrato <= 0) return BadRequest(new { message = "La id de contrato no puede ser menor o igual a 0" });
|
|
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 12);
|
|
if (validacion1 == false)
|
|
{
|
|
validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 11);
|
|
if (validacion1 == false)
|
|
{
|
|
return Unauthorized();
|
|
}
|
|
}
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return Unauthorized();
|
|
|
|
Contrato? cont = RepositorioContratos.Singleton.ObtenerContratoPorId(Idcontrato);
|
|
if (cont == null) return BadRequest(new { message = "No hay contrato por esa id" });
|
|
|
|
if (cont.Dniinquilino != cli.Dni && cont.Dnipropietario != cli.Dni) return BadRequest(new { message = "no deberias tener acceso a esto" });
|
|
|
|
var l = RepositorioDefectos.Singleton.ObtenerDefectosPorIdContrato(Idcontrato);
|
|
List<DefectoDto> ll = new();
|
|
foreach (var i in l)
|
|
{
|
|
var n = new DefectoDtoBuilder()
|
|
.SetId(i.Id)
|
|
.SetDesc(i.Descripcion)
|
|
.SetCosto(i.Costo)
|
|
.SetEstado(i.IdestadoNavigation.Descipcion)
|
|
.SetIdContrato(i.Idcontrato ?? 0)
|
|
.SetPagaInquilino(i.Pagainquilino)
|
|
.SetDivisa(i.IddivisaNavigation.Signo)
|
|
.Build();
|
|
ll.Add(n);
|
|
}
|
|
return Ok(ll);
|
|
}
|
|
|
|
[HttpPost("api/defecto")]
|
|
public IActionResult AltaDefecto([FromHeader(Name = "Auth")] string Auth, AltaDefectoDto data)
|
|
{
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 11);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return Unauthorized();
|
|
|
|
string r = ValidarDto(data);
|
|
if (r != "") return BadRequest(new { message = r });
|
|
|
|
Defecto defecto = new Defecto
|
|
{
|
|
Costo = data.Costo,
|
|
Descripcion = data.Descripcion,
|
|
Idcontrato = data.Idcontrato,
|
|
Iddivisa = data.Iddivisa,
|
|
Pagainquilino = data.Pagainquilino == 0 ? 0Lu : 1Lu,
|
|
Idestado = 1,
|
|
|
|
};
|
|
var b = RepositorioDefectos.Singleton.AltaDefecto(defecto, cli.Dni);
|
|
return b ? Ok(new { message = "Se cargo el Defecto en el sistema" }) : BadRequest(new { message = "No se pudo cargar el defecto en el sistema" });
|
|
}
|
|
|
|
private string ValidarDto(AltaDefectoDto d)
|
|
{
|
|
string ret = "";
|
|
|
|
if (d == null) return "Dto nulo";
|
|
if (d.Iddivisa < 0 || d.Iddivisa > 1) ret += "No son divisas validas\n";
|
|
if (d.Descripcion == "") ret += "La descripcion no puede estar vacia\n";
|
|
if (d.Idcontrato <= 0) ret += "No puede haber un id de contrato igual o menor a 0\n";
|
|
|
|
return ret;
|
|
}
|
|
|
|
[HttpPut("api/defecto/marcarpago")]
|
|
public IActionResult MarcarPago([FromHeader(Name = "Auth")] string Auth, long iddefecto = 0)
|
|
{
|
|
if (String.IsNullOrEmpty(Auth)) return Unauthorized();
|
|
var validacion1 = RepositorioPermisos.Singleton.CheckPermisos(Auth, 12);
|
|
if (validacion1 == false) return Unauthorized();
|
|
|
|
Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth);
|
|
if (cli == null) return Unauthorized();
|
|
|
|
if (iddefecto <= 0) return BadRequest(new { message = "No hay canones con id 0 o menor" });
|
|
bool ret = RepositorioDefectos.Singleton.MarcarPago(iddefecto, cli.Dni);
|
|
|
|
return ret ?
|
|
Ok(new { message = "Se marco como pagado" }) : BadRequest(new { message = "Fallo el acceso a la base de datos o no se encontro el defecto" });
|
|
}
|
|
}
|