Private
Public Access
feat: añadido crear y eliminar usuarios
This commit is contained in:
1 parent
3d8883cac6
commit
69395cc6d7
3 files changed
+82
-3
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import type { Usuario } from '../../types';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function crearUsuario(dto: Partial<Usuario & { password?: string }>) {
|
||||
if (!dto.password || dto.password?.length <= 8 || !dto.email || !dto.name) return 'Faltan Datos';
|
||||
console.log(dto);
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios`, {
|
||||
method: 'POST',
|
||||
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';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function eliminarUsuario(id: number) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
|
||||
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';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user