Private
Public Access
añadida funcionalidad de actualizar marcas
This commit is contained in:
1 parent
4625f75a45
commit
3a9dcdfce7
5 files changed
+165
-6
No files matched your search
@@ -0,0 +1,56 @@
|
|||||||
|
package marca
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"repuestera_back/models"
|
||||||
|
r "repuestera_back/repositorios"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ActualizarMarca actualiza una marca existente.
|
||||||
|
func ActualizarMarca(c *gin.Context) {
|
||||||
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID invalido"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if id < 1 {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Obtener la marca actual
|
||||||
|
marca, err := r.ObtenerRepositorios().Marca.ObtenerPorID(uint(id))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Marca no encontrada"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var input models.Marca
|
||||||
|
if err := c.ShouldBindJSON(&input); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if input.Nombre == "" {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "El nombre no puede estar vacío"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verificar que no exista otra marca con el mismo nombre
|
||||||
|
if existing, _ := r.ObtenerRepositorios().Marca.ObtenerPorNombre(input.Nombre); existing != nil && existing.ID != marca.ID {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ya existe una marca con ese nombre"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uid := c.GetUint("userID")
|
||||||
|
if err := r.ObtenerRepositorios().Marca.Actualizar(marca, input, uid); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(http.StatusOK, marca)
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ func MarcaRoute(r *gin.Engine) {
|
|||||||
repuestoGrupo.GET("/", marca.ObtenerMarcas)
|
repuestoGrupo.GET("/", marca.ObtenerMarcas)
|
||||||
// repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
// repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
||||||
repuestoGrupo.POST("/", marca.CrearMarca)
|
repuestoGrupo.POST("/", marca.CrearMarca)
|
||||||
// repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
repuestoGrupo.PUT("/:id", marca.ActualizarMarca)
|
||||||
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
||||||
|
|
||||||
// repuestoGrupo.PATCH("/:id/alta", repuestos.AltaRepuestos)
|
// repuestoGrupo.PATCH("/:id/alta", repuestos.AltaRepuestos)
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { API_URL } from "$lib/env";
|
||||||
|
import { getToken } from "../token";
|
||||||
|
|
||||||
|
export async function actualizarMarca(id: number, nombre: string) {
|
||||||
|
nombre = nombre.trim();
|
||||||
|
if (nombre === "") return "No se acepta un nombre vacío o con espacios";
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/marcas/${id}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ nombre })
|
||||||
|
});
|
||||||
|
if (!req.ok) {
|
||||||
|
const data: { error: string } = await req.json();
|
||||||
|
return data.error || "No se pudo actualizar la marca";
|
||||||
|
}
|
||||||
|
return "ok";
|
||||||
|
} catch {
|
||||||
|
return "No se pudo alcanzar el servidor";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
import { Add, ArrowUp, Edit, Information, TrashCan } from 'carbon-icons-svelte';
|
import { Add, ArrowUp, Edit, Information, TrashCan } from 'carbon-icons-svelte';
|
||||||
import type { Marca } from '../../types';
|
import type { Marca } from '../../types';
|
||||||
import CrearMarcaForm from './CrearMarcaForm.svelte';
|
import CrearMarcaForm from './CrearMarcaForm.svelte';
|
||||||
|
import EditarMarcaForm from './EditarMarcaForm.svelte';
|
||||||
|
|
||||||
let error = $state("");
|
let error = $state("");
|
||||||
|
|
||||||
@@ -26,7 +27,9 @@
|
|||||||
{ key: 'DeletedAt', value: 'Estado' }
|
{ key: 'DeletedAt', value: 'Estado' }
|
||||||
];
|
];
|
||||||
|
|
||||||
let openCrear = $state(false)
|
let openCrear = $state(false);
|
||||||
|
let openEdit = $state(false);
|
||||||
|
let marcaSeleccionada: Marca | null = $state(null);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Header />
|
<Header />
|
||||||
@@ -75,8 +78,8 @@
|
|||||||
{#each headers as header}
|
{#each headers as header}
|
||||||
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
||||||
{/each}
|
{/each}
|
||||||
<TableCell>{console.log(p)}
|
<TableCell>
|
||||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => {}}>Editar</Button>
|
<Button kind="ghost" size="small" icon={Edit} onclick={() => { marcaSeleccionada = p; openEdit = true; }}>Editar</Button>
|
||||||
{#if p.DeletedAt}
|
{#if p.DeletedAt}
|
||||||
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => {}}>Alta</Button>
|
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => {}}>Alta</Button>
|
||||||
{:else}
|
{:else}
|
||||||
@@ -91,5 +94,6 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<CrearMarcaForm bind:open={openCrear} onClose={()=> openCrear = false}>
|
|
||||||
</CrearMarcaForm>
|
<CrearMarcaForm bind:open={openCrear} onClose={() => openCrear = false} />
|
||||||
|
<EditarMarcaForm bind:open={openEdit} marca={marcaSeleccionada} onClose={() => { openEdit = false; marcaSeleccionada = null; }} />
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Modal, TextInput } from 'carbon-components-svelte';
|
||||||
|
import type { Marca } from '../../types';
|
||||||
|
import { actualizarMarca } from '$lib/hooks/marcas/actualizarMarca';
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
|
||||||
|
// Props to control modal visibility and the marca to edit
|
||||||
|
let { open = $bindable(), onClose, marca } = $props();
|
||||||
|
|
||||||
|
let error = $state('');
|
||||||
|
let enviando = $state(false);
|
||||||
|
let nombre = $state('');
|
||||||
|
|
||||||
|
$effect(()=>{
|
||||||
|
if (marca) {
|
||||||
|
nombre = marca.nombre ?? '';
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
reset();
|
||||||
|
onClose?.();
|
||||||
|
}
|
||||||
|
function reset() {
|
||||||
|
nombre = '';
|
||||||
|
error = '';
|
||||||
|
enviando = false;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form
|
||||||
|
id="editarMarcaForm"
|
||||||
|
onsubmit={async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (enviando) return;
|
||||||
|
enviando = true;
|
||||||
|
error = '';
|
||||||
|
if (!nombre.trim()) {
|
||||||
|
error = 'Falta Nombre';
|
||||||
|
}
|
||||||
|
if (error === '' && marca) {
|
||||||
|
const result = await actualizarMarca(marca.ID, nombre);
|
||||||
|
if (result === 'ok') {
|
||||||
|
invalidate('marcas:load');
|
||||||
|
handleClose();
|
||||||
|
} else {
|
||||||
|
error = result ?? 'Error desconocido';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enviando = false;
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Modal
|
||||||
|
formId="editarMarcaForm"
|
||||||
|
bind:open
|
||||||
|
size={'lg'}
|
||||||
|
modalHeading="Editar Marca"
|
||||||
|
primaryButtonText="Guardar"
|
||||||
|
primaryButtonDisabled={enviando}
|
||||||
|
secondaryButtonText="Cancelar"
|
||||||
|
on:click:button--secondary={handleClose}
|
||||||
|
>
|
||||||
|
{#if error}
|
||||||
|
<div class="error-msg">{error}</div>
|
||||||
|
{/if}
|
||||||
|
<TextInput
|
||||||
|
name="nombre"
|
||||||
|
type="text"
|
||||||
|
labelText="Nombre:"
|
||||||
|
bind:value={nombre}
|
||||||
|
maxlength={70}
|
||||||
|
/>
|
||||||
|
<div class="text-sm text-gray-500 mt-1">{String(nombre).length}/70</div>
|
||||||
|
</Modal>
|
||||||
|
</form>
|
||||||
Reference in new issue
Block a user