Private
Public Access
feat: hecho lo de actualizar usuario
This commit is contained in:
1 parent
d0949e2560
commit
3d8883cac6
7 files changed
+52
-36
No files matched your search
@@ -82,14 +82,15 @@ func ActualizarUsuario(c *gin.Context){
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
idconv, _ := strconv.ParseUint(id, 10, 64)
|
||||||
|
|
||||||
var usuario models_seguridad.Usuario
|
var usuario models_seguridad.Usuario
|
||||||
if err := config.DB.Where("id = ?", id).First(&usuario).Error; err != nil {
|
|
||||||
c.JSON(http.StatusNotFound, gin.H{"error": "Usuario no encontrado"})
|
config.DB.First(&usuario, uint(idconv))
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
usuario.Email = usuarioReq.Email
|
usuario.Email = usuarioReq.Email
|
||||||
usuario.Name = usuarioReq.Name
|
usuario.Name = usuarioReq.Name
|
||||||
|
|
||||||
config.DB.Save(&usuario)
|
config.DB.Save(&usuario)
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, usuario)
|
c.JSON(http.StatusCreated, usuario)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
export { login } from './hooks/login';
|
export { login } from './hooks/login';
|
||||||
export { register } from './hooks/register';
|
export { register } from './hooks/register';
|
||||||
export { obtenerUsuarioPorId } from './hooks/obtenerUsuarioPorId';
|
export { obtenerUsuarioPorId } from './hooks/obtenerUsuarioPorId';
|
||||||
export { modificarUsuario } from './hooks/modificarUsuario';
|
|
||||||
export { borrarUsuario } from './hooks/borrarUsuario';
|
export { borrarUsuario } from './hooks/borrarUsuario';
|
||||||
export { ObtenerGruposDeUsuario } from './hooks/ObtenerGruposDeUsuario';
|
export { ObtenerGruposDeUsuario } from './hooks/ObtenerGruposDeUsuario';
|
||||||
export { obtenerPermisos } from './hooks/obtenerPermisos';
|
export { obtenerPermisos } from './hooks/obtenerPermisos';
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
import { API_URL } from '$lib/env';
|
||||||
|
import type { Usuario } from '../../types';
|
||||||
|
import { getToken } from './token';
|
||||||
|
|
||||||
|
export async function actualizarUsuario(dto: Partial<Usuario>) {
|
||||||
|
if (dto.id <= 0 || !dto.email || !dto.name) return 'Faltan Datos';
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/usuarios/${dto.id}`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify(dto)
|
||||||
|
});
|
||||||
|
|
||||||
|
let data = await req.json();
|
||||||
|
if (!req.ok) {
|
||||||
|
return data.error || 'Error';
|
||||||
|
}
|
||||||
|
invalidate('usuarios:load');
|
||||||
|
return 'Ok';
|
||||||
|
} catch {
|
||||||
|
return 'No se pudo conectar con el servidor';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
import type { Usuario } from '../../types';
|
|
||||||
import { API_URL } from '../env';
|
|
||||||
import { getToken } from './token';
|
|
||||||
|
|
||||||
async function handleResponse<T>(response: Response): Promise<T> {
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
|
||||||
throw new Error(error.message || 'Error en la petición');
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function modificarUsuario(
|
|
||||||
id: number,
|
|
||||||
data: Partial<{ email: string; name: string }>
|
|
||||||
): Promise<Usuario> {
|
|
||||||
const response = await fetch(`${API_URL}/users/${id}`, {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Bearer ${getToken()}`
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
return handleResponse<Usuario>(response);
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
import Header from '$lib/components/Header.svelte';
|
import Header from '$lib/components/Header.svelte';
|
||||||
|
import { actualizarUsuario } from '$lib/hooks/actualizarUsuario.js';
|
||||||
import {
|
import {
|
||||||
Button,
|
Button,
|
||||||
Modal,
|
Modal,
|
||||||
@@ -22,8 +23,9 @@
|
|||||||
let showEditarModal = $state(false);
|
let showEditarModal = $state(false);
|
||||||
let showEliminarModal = $state(false);
|
let showEliminarModal = $state(false);
|
||||||
let showCambiarContraseñaModal = $state(false);
|
let showCambiarContraseñaModal = $state(false);
|
||||||
|
let showError: string | Promise<string> = $state('');
|
||||||
|
|
||||||
let usuarioModificar = $state();
|
let usuarioModificar = $state({ name: '', email: '' });
|
||||||
let usuarioEliminar = $state();
|
let usuarioEliminar = $state();
|
||||||
let usuarioCambiarContraseña = $state();
|
let usuarioCambiarContraseña = $state();
|
||||||
|
|
||||||
@@ -113,6 +115,15 @@
|
|||||||
</TableContainer>
|
</TableContainer>
|
||||||
{/await}
|
{/await}
|
||||||
</div>
|
</div>
|
||||||
|
{#await showError then msgerr}
|
||||||
|
<Modal
|
||||||
|
open={!!showError && msgerr != 'Ok'}
|
||||||
|
primaryButtonText="Cerrar"
|
||||||
|
on:click:button--primary={() => (showError = '')}
|
||||||
|
>
|
||||||
|
{msgerr}
|
||||||
|
</Modal>
|
||||||
|
{/await}
|
||||||
<form
|
<form
|
||||||
id="crearForm"
|
id="crearForm"
|
||||||
onsubmit={(e) => {
|
onsubmit={(e) => {
|
||||||
@@ -132,6 +143,8 @@
|
|||||||
id="editarForm"
|
id="editarForm"
|
||||||
onsubmit={(e) => {
|
onsubmit={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
showError = actualizarUsuario(usuarioModificar);
|
||||||
|
showEditarModal = false;
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -142,8 +155,8 @@
|
|||||||
secondaryButtonText="Cerrar"
|
secondaryButtonText="Cerrar"
|
||||||
on:click:button--secondary={cerrarModal}
|
on:click:button--secondary={cerrarModal}
|
||||||
>
|
>
|
||||||
<TextInput name="nombre" type="text" labelText="Nombre: " />
|
<TextInput name="nombre" type="text" labelText="Nombre: " bind:value={usuarioModificar.name} />
|
||||||
<TextInput name="email" type="text" labelText="Email" />
|
<TextInput name="email" type="text" labelText="Email" bind:value={usuarioModificar.email} />
|
||||||
</Modal>
|
</Modal>
|
||||||
</form>
|
</form>
|
||||||
<form
|
<form
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { ObtenerUsuarios } from '$lib/hooks/ObtenerUsuarios';
|
import { ObtenerUsuarios } from '$lib/hooks/ObtenerUsuarios';
|
||||||
|
|
||||||
export let ssr = false;
|
export let ssr = false;
|
||||||
export async function load({ fetch }) {
|
export async function load({ fetch, depends }) {
|
||||||
|
depends('usuarios:load');
|
||||||
const usuarios = ObtenerUsuarios(10, 0, fetch);
|
const usuarios = ObtenerUsuarios(10, 0, fetch);
|
||||||
return { usuarios };
|
return { usuarios };
|
||||||
}
|
}
|
||||||
Vendored
+1
@@ -1,4 +1,5 @@
|
|||||||
export interface Usuario {
|
export interface Usuario {
|
||||||
|
id: number;
|
||||||
email: string;
|
email: string;
|
||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user