Private
Public Access
añadida funcionalidad para dar de baja y restaurar marcas dadas de baja
This commit is contained in:
1 parent
478fe331aa
commit
884fd6ff24
9 files changed
+206
-17
No files matched your search
@@ -13,6 +13,8 @@ type IRepositorio[T any] interface {
|
|||||||
Crear(entidad T, usuarioID uint) error
|
Crear(entidad T, usuarioID uint) error
|
||||||
Actualizar(entidad *T, input T, usuarioID uint) error
|
Actualizar(entidad *T, input T, usuarioID uint) error
|
||||||
Eliminar(id uint, usuarioID uint) error
|
Eliminar(id uint, usuarioID uint) error
|
||||||
|
// Alta updates a soft‑deleted record, clearing DeletedAt so it can be re‑activated.
|
||||||
|
Alta(entidad *T, input T, usuarioID uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type RepositorioBase[T any] struct {
|
type RepositorioBase[T any] struct {
|
||||||
@@ -41,7 +43,7 @@ func (r *RepositorioBase[T]) ObtenerTodos() ([]T, error) {
|
|||||||
|
|
||||||
func (r *RepositorioBase[T]) ObtenerPorID(id uint) (*T, error) {
|
func (r *RepositorioBase[T]) ObtenerPorID(id uint) (*T, error) {
|
||||||
var entidad T
|
var entidad T
|
||||||
if err := r.db.First(&entidad, id).Error; err != nil {
|
if err := r.db.Unscoped().First(&entidad, id).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &entidad, nil
|
return &entidad, nil
|
||||||
@@ -55,6 +57,13 @@ func (r *RepositorioBase[T]) Actualizar(entidad *T, input T, usuarioID uint) err
|
|||||||
return r.dbConAuditoria(usuarioID).Model(entidad).Updates(input).Error
|
return r.dbConAuditoria(usuarioID).Model(entidad).Updates(input).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *RepositorioBase[T]) Alta(entidad *T, input T, usuarioID uint) error {
|
||||||
|
if err := r.dbConAuditoria(usuarioID).Unscoped().Model(entidad).Update("deleted_at", nil).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return r.dbConAuditoria(usuarioID).Unscoped().Model(entidad).Updates(input).Error
|
||||||
|
}
|
||||||
|
|
||||||
func (r *RepositorioBase[T]) Eliminar(id uint, usuarioID uint) error {
|
func (r *RepositorioBase[T]) Eliminar(id uint, usuarioID uint) error {
|
||||||
var entidad T
|
var entidad T
|
||||||
return r.dbConAuditoria(usuarioID).Delete(&entidad, id).Error
|
return r.dbConAuditoria(usuarioID).Delete(&entidad, id).Error
|
||||||
|
|||||||
@@ -8,16 +8,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func MarcaRoute(r *gin.Engine) {
|
func MarcaRoute(r *gin.Engine) {
|
||||||
repuestoGrupo := r.Group("/marcas")
|
marcaGrupo := r.Group("/marcas")
|
||||||
repuestoGrupo.Use(middleware.AuthMiddleware(), middleware.PermisoMiddleware(5))
|
marcaGrupo.Use(middleware.AuthMiddleware(), middleware.PermisoMiddleware(5))
|
||||||
{
|
{
|
||||||
repuestoGrupo.GET("/", marca.ObtenerMarcas)
|
marcaGrupo.GET("/", marca.ObtenerMarcas)
|
||||||
// repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
// marcaGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
||||||
repuestoGrupo.POST("/", marca.CrearMarca)
|
marcaGrupo.POST("/", marca.CrearMarca)
|
||||||
repuestoGrupo.PUT("/:id", marca.ActualizarMarca)
|
marcaGrupo.PUT("/:id", marca.ActualizarMarca)
|
||||||
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
|
||||||
|
|
||||||
// repuestoGrupo.PATCH("/:id/alta", repuestos.AltaRepuestos)
|
marcaGrupo.DELETE("/:id", marca.EliminarMarca)
|
||||||
|
|
||||||
|
marcaGrupo.PATCH("/:id/alta", marca.AltaMarca)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { API_URL } from '$lib/env';
|
||||||
|
import { getToken } from '../token';
|
||||||
|
|
||||||
|
export async function AltaMarca(id: number) {
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/marcas/${id}/alta`, {
|
||||||
|
method: 'PATCH',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (req.ok) {
|
||||||
|
return 'ok';
|
||||||
|
}
|
||||||
|
const data = await req.json();
|
||||||
|
return data.error;
|
||||||
|
} catch {
|
||||||
|
return 'no se pudo alcanzar el servidor';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { API_URL } from '$lib/env';
|
||||||
|
import { getToken } from '../token';
|
||||||
|
|
||||||
|
export async function eliminarMarca(id: number) {
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/marcas/${id}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (req.ok) {
|
||||||
|
return 'Ok';
|
||||||
|
}
|
||||||
|
const data = await req.json();
|
||||||
|
return data.error;
|
||||||
|
} catch {
|
||||||
|
return 'no se pudo alcanzar el servidor';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,12 +5,13 @@
|
|||||||
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';
|
import EditarMarcaForm from './EditarMarcaForm.svelte';
|
||||||
|
import EliminarMarcaForm from './EliminarMarcaForm.svelte';
|
||||||
|
import AltaMarcaForm from './AltaMarcaForm.svelte';
|
||||||
|
|
||||||
let error = $state("");
|
let error = $state("");
|
||||||
|
|
||||||
let { data }: {data: {marcas: string | Array<Marca>}} = $props();
|
let { data }: {data: {marcas: string | Array<Marca>}} = $props();
|
||||||
|
|
||||||
// Ensure we have an array of Marca for iteration
|
|
||||||
let marcas = $derived(Array.isArray(data.marcas) ? data.marcas : []);
|
let marcas = $derived(Array.isArray(data.marcas) ? data.marcas : []);
|
||||||
|
|
||||||
$effect(()=>{
|
$effect(()=>{
|
||||||
@@ -19,7 +20,7 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const headers = [
|
const headers: { key: keyof Marca; value: string }[] = [
|
||||||
{ key: 'ID', value: 'ID' },
|
{ key: 'ID', value: 'ID' },
|
||||||
{ key: 'nombre', value: 'Nombre' },
|
{ key: 'nombre', value: 'Nombre' },
|
||||||
{ key: 'CreatedAt', value: 'Creado' },
|
{ key: 'CreatedAt', value: 'Creado' },
|
||||||
@@ -29,7 +30,13 @@
|
|||||||
|
|
||||||
let openCrear = $state(false);
|
let openCrear = $state(false);
|
||||||
let openEdit = $state(false);
|
let openEdit = $state(false);
|
||||||
|
let openDelete = $state(false);
|
||||||
let marcaSeleccionada: Marca | null = $state(null);
|
let marcaSeleccionada: Marca | null = $state(null);
|
||||||
|
let marcaAEliminar: Marca | null = $state(null);
|
||||||
|
let openAlta = $state(false);
|
||||||
|
let marcaAAlta: Marca | null = $state(null);
|
||||||
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Header />
|
<Header />
|
||||||
@@ -76,14 +83,14 @@
|
|||||||
{#each marcas as p}
|
{#each marcas as p}
|
||||||
<TableRow>
|
<TableRow>
|
||||||
{#each headers as header}
|
{#each headers as header}
|
||||||
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
<TableCell>{header.key === 'DeletedAt' ? (p.DeletedAt ? '✖' : '✔') : String(p[header.key] ?? '-')}</TableCell>
|
||||||
{/each}
|
{/each}
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<Button kind="ghost" size="small" icon={Edit} onclick={() => { marcaSeleccionada = p; openEdit = true; }}>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={() => { marcaAAlta = p; openAlta = true; }}>Alta</Button>
|
||||||
{:else}
|
{:else}
|
||||||
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => {}}>Eliminar</Button>
|
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => { marcaAEliminar = p; openDelete = true; }}>Eliminar</Button>
|
||||||
{/if}
|
{/if}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
@@ -96,4 +103,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<CrearMarcaForm bind:open={openCrear} onClose={() => openCrear = false} />
|
<CrearMarcaForm bind:open={openCrear} onClose={() => openCrear = false} />
|
||||||
<EditarMarcaForm bind:open={openEdit} marca={marcaSeleccionada} onClose={() => { openEdit = false; marcaSeleccionada = null; }} />
|
|
||||||
|
<EliminarMarcaForm bind:open={openDelete} marca={marcaAEliminar} onClose={() => { openDelete = false; marcaAEliminar = null; }} />
|
||||||
|
|
||||||
|
<AltaMarcaForm bind:open={openAlta} marca={marcaAAlta} onClose={()=> openAlta = false} />
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Modal, InlineNotification } from 'carbon-components-svelte';
|
||||||
|
import type { Marca } from '../../types';
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
import { AltaMarca } from '$lib/hooks/marcas/altaMarca';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
open?: boolean;
|
||||||
|
marca?: Marca | null;
|
||||||
|
onClose: Function
|
||||||
|
}
|
||||||
|
let { open = $bindable(), marca = null, onClose}:Props = $props();
|
||||||
|
|
||||||
|
let error = $state('');
|
||||||
|
let loading = $state(false);
|
||||||
|
let success = $state(false);
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
error = '';
|
||||||
|
loading = false;
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
reset();
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function altaMarca() {
|
||||||
|
if (!marca) return;
|
||||||
|
loading = true;
|
||||||
|
error = '';
|
||||||
|
success = false;
|
||||||
|
try {
|
||||||
|
const result = await AltaMarca(marca.ID);
|
||||||
|
if (result === 'ok') {
|
||||||
|
success = true;
|
||||||
|
invalidate('marcas:load');
|
||||||
|
// Close after success
|
||||||
|
handleClose();
|
||||||
|
} else {
|
||||||
|
throw new Error(result);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
error = e instanceof Error ? e.message : String(e);
|
||||||
|
} finally {
|
||||||
|
loading = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form
|
||||||
|
id="altaMarcaForm"
|
||||||
|
onsubmit={async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (loading) return;
|
||||||
|
await altaMarca();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Modal
|
||||||
|
formId="altaMarcaForm"
|
||||||
|
bind:open
|
||||||
|
size="sm"
|
||||||
|
modalHeading="Dar de alta marca"
|
||||||
|
primaryButtonText="Dar de alta"
|
||||||
|
primaryButtonDisabled={loading}
|
||||||
|
secondaryButtonText="Cancelar"
|
||||||
|
on:click:button--secondary={handleClose}
|
||||||
|
>
|
||||||
|
{#if error}
|
||||||
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||||
|
{/if}
|
||||||
|
{#if marca}
|
||||||
|
<p>¿Dar de alta la marca <strong>{marca.nombre}</strong>?</p>
|
||||||
|
{/if}
|
||||||
|
{#if success}
|
||||||
|
<InlineNotification kind="success" title="Éxito" subtitle="Marca dada de alta correctamente" hideCloseButton />
|
||||||
|
{/if}
|
||||||
|
</Modal>
|
||||||
|
</form>
|
||||||
@@ -4,7 +4,6 @@
|
|||||||
import { crearMarca } from '$lib/hooks/marcas/crearMarca';
|
import { crearMarca } from '$lib/hooks/marcas/crearMarca';
|
||||||
import { invalidate } from '$app/navigation';
|
import { invalidate } from '$app/navigation';
|
||||||
|
|
||||||
// Props to control modal visibility
|
|
||||||
let { open = $bindable(), onClose } = $props();
|
let { open = $bindable(), onClose } = $props();
|
||||||
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
import { actualizarMarca } from '$lib/hooks/marcas/actualizarMarca';
|
import { actualizarMarca } from '$lib/hooks/marcas/actualizarMarca';
|
||||||
import { invalidate } from '$app/navigation';
|
import { invalidate } from '$app/navigation';
|
||||||
|
|
||||||
// Props to control modal visibility and the marca to edit
|
|
||||||
let { open = $bindable(), onClose, marca } = $props();
|
let { open = $bindable(), onClose, marca } = $props();
|
||||||
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Modal } from 'carbon-components-svelte';
|
||||||
|
import { eliminarMarca } from '$lib/hooks/marcas/eliminarMarca';
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
import type { Marca } from '../../types';
|
||||||
|
|
||||||
|
let {
|
||||||
|
open = $bindable(),
|
||||||
|
onClose,
|
||||||
|
marca
|
||||||
|
}: { open?: boolean; onClose: () => void; marca: Marca} = $props();
|
||||||
|
|
||||||
|
let error = $state('');
|
||||||
|
|
||||||
|
async function handleSubmit(e: Event) {
|
||||||
|
e.preventDefault();
|
||||||
|
error = '';
|
||||||
|
if (!marca?.ID) {
|
||||||
|
error = 'No se pudo obtener la id de la Marca? recarga la página.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const result = await eliminarMarca(marca.ID);
|
||||||
|
if (result !== 'Ok') {
|
||||||
|
error = result;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
invalidate('marcas:load');
|
||||||
|
handleClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
error = '';
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form id="eliminarMarcaForm" onsubmit={handleSubmit}>
|
||||||
|
<Modal
|
||||||
|
formId="eliminarMarcaForm"
|
||||||
|
bind:open={open}
|
||||||
|
modalHeading="Eliminar Marca"
|
||||||
|
primaryButtonText="Eliminar"
|
||||||
|
secondaryButtonText="Cerrar"
|
||||||
|
on:click:button--secondary={handleClose}
|
||||||
|
>
|
||||||
|
{#if error}
|
||||||
|
<div class="text-red-500">{error}</div>
|
||||||
|
{/if}
|
||||||
|
¿Está seguro de que desea borrar esta Marca?
|
||||||
|
</Modal>
|
||||||
|
</form>
|
||||||
Reference in new issue
Block a user