From b3ffa657f53df05dc9d100b32c4d539085e6bc54 Mon Sep 17 00:00:00 2001 From: fede Date: Fri, 14 Mar 2025 02:33:59 -0300 Subject: [PATCH] =?UTF-8?q?a=C3=B1adido=20endpoint=20para=20crear=20permis?= =?UTF-8?q?os?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aspnet/Controllers/AccionesController.cs | 48 +++++++++++++++++++----- Entidades/Dto/CrearAccionesDto.cs | 5 +++ Modelo/RepositorioPermisos.cs | 33 +++++++++++----- 3 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 Entidades/Dto/CrearAccionesDto.cs diff --git a/Aspnet/Controllers/AccionesController.cs b/Aspnet/Controllers/AccionesController.cs index 9be95b8..ea3dff9 100644 --- a/Aspnet/Controllers/AccionesController.cs +++ b/Aspnet/Controllers/AccionesController.cs @@ -1,20 +1,20 @@ -using System.ComponentModel.DataAnnotations; using Entidades.Dto; using Microsoft.AspNetCore.Mvc; using Modelo; -using System.Text.Json; +using Entidades; - namespace AlquilaFacil.Controllers; [ApiController] -public class AccionesController: ControllerBase { - +public class AccionesController : ControllerBase +{ + [HttpGet("api/acciones")] - public IActionResult ListarAccionesPorUsuario([FromHeader(Name ="Email")] string Email, [FromHeader(Name = "Auth")] string Auth) { + public IActionResult ListarAccionesPorUsuario([FromHeader(Name = "Email")] string Email, [FromHeader(Name = "Auth")] string Auth) + { if (Email == "" || Email == null) return BadRequest(); - - if (Auth == "") return Unauthorized(new { esValido = false}); + + if (Auth == "") return Unauthorized(new { esValido = false }); bool esValido = RepositorioUsuarios.Singleton.CheckToken(Email, Auth); if (!esValido) return Unauthorized(); @@ -37,4 +37,34 @@ public class AccionesController: ControllerBase { var permisos = RepositorioGrupos.Singleton.ListarPermisosDeGrupo(req.Grupo); return Ok(permisos); } -} \ No newline at end of file + + [HttpPost("api/acciones/crear")] + public IActionResult CrearAcciones([FromHeader(Name = "Auth")] string Auth, + [FromBody] CrearAccionesDto req) + { + if (string.IsNullOrEmpty(Auth)) return BadRequest(); + + Cliente? cli = RepositorioUsuarios.Singleton.ObtenerClientePorToken(Auth); + if (cli == null) return Unauthorized(); + + var ret = RepositorioPermisos.Singleton.CheckPermisos(Auth, 1); //wip lo voy a usar para crear el permiso que va + if (ret == false) return Unauthorized(); + + if (string.IsNullOrWhiteSpace(req.Descripcion)) + { + return BadRequest(new { message = "La descripción no puede estar vacía" }); + } + + if (req.Descripcion.Length > 25) + { + return BadRequest(new { message = "La descripción no puede ser mayor a 25 caracteres" }); + } + + var per = new Permiso + { + Descripcion = req.Descripcion, + } + var ret2 = RepositorioPermisos.Singleton.CrearPermiso(per); + return ret2 ? Ok(new { message = "se creo correctamente" }) : BadRequest(new { message = "No se pudo crear el permiso" }); + } +} diff --git a/Entidades/Dto/CrearAccionesDto.cs b/Entidades/Dto/CrearAccionesDto.cs new file mode 100644 index 0000000..5e8a23f --- /dev/null +++ b/Entidades/Dto/CrearAccionesDto.cs @@ -0,0 +1,5 @@ +namespace Entidades.Dto; +public class CrearAccionesDto +{ + public string Descripcion { get; set; } = ""; +} diff --git a/Modelo/RepositorioPermisos.cs b/Modelo/RepositorioPermisos.cs index a5d4445..f2d457a 100644 --- a/Modelo/RepositorioPermisos.cs +++ b/Modelo/RepositorioPermisos.cs @@ -3,21 +3,26 @@ using Entidades; using Microsoft.EntityFrameworkCore; namespace Modelo; -public class RepositorioPermisos: RepositorioBase { - public object? ListarPermisos(string email) { +public class RepositorioPermisos : RepositorioBase +{ + public object? ListarPermisos(string email) + { var con = Context; Cliente? cli = con.Clientes.Include(x => x.Idgrupos).FirstOrDefault(c => c.Email == email); if (cli == null) return null; var list = con.Clientes .Where(c => c.Dni == cli.Dni) - .SelectMany(c => c.Idgrupos) - .Include(x=> x.Idpermisos); - + .SelectMany(c => c.Idgrupos) + .Include(x => x.Idpermisos); + return list; } - public bool CheckPermisos(string token, int idpermiso){ + public bool CheckPermisos(string token, int idpermiso) + { + // Aca tengo que modificar esto para que haga una busqueda de profundidad para los permisos + // var con = Context; bool tienePermiso = false; @@ -41,12 +46,22 @@ public class RepositorioPermisos: RepositorioBase { //int.TryParse(match.Groups[1].Value, out int idpermiso); ///////////////////////////////////////////////////////////////// - Parallel.ForEach(permisos, (x, i) =>{ - if (x.Id == idpermiso) { + Parallel.ForEach(permisos, (x, i) => + { + if (x.Id == idpermiso) + { tienePermiso = true; } }); return tienePermiso; } -} \ No newline at end of file + + public bool CrearPermiso(Permiso per) + { + var con = Context; + per.Id = con.Permisos.Any() ? con.Permisos.Max(x => x.Id) + 1 : 1; + con.Permisos.Add(per); + return Guardar(con); + } +}