Private
Public Access
correjida funcionalidad de alta, modificacion y eliminacion de repuestos
This commit is contained in:
1 parent
6c900f0afd
commit
6b9cd844e4
12 files changed
+139
-63
No files matched your search
@@ -10,9 +10,7 @@ import (
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
|
||||
|
||||
func CrearRepuesto(c *gin.Context){
|
||||
func CrearRepuesto(c *gin.Context) {
|
||||
var data dto.CrearRepuestoDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func ObtenerRepuestos(c *gin.Context) {
|
||||
repuestos, err := r.ObtenerRepositorios().Repuesto.ObtenerTodos()
|
||||
repuestos, err := r.ObtenerRepositorios().Repuesto.ObtenerTodosConMarca()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package dto
|
||||
|
||||
type ActualizarRepuestoDto struct {
|
||||
ID *uint64 `json:"id,omitempty"`
|
||||
ID *uint64 `json:"ID,omitempty"`
|
||||
Nombre string `json:"nombre"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Marca uint `json:"marca"`
|
||||
Marca uint `json:"marcaId"`
|
||||
Aliases []string `json:"aliases"`
|
||||
Precio float64 `json:"precio"`
|
||||
StockMinimo int `json:"stockMinimo"`
|
||||
|
||||
@@ -3,7 +3,7 @@ package dto
|
||||
type CrearRepuestoDto struct {
|
||||
Nombre string `json:"nombre"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Marca uint `json:"marca"`
|
||||
Marca uint `json:"marcaId"`
|
||||
Aliases []string `json:"aliases"`
|
||||
Precio float64 `json:"precio"`
|
||||
StockMinimo int `json:"stockMinimo"`
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
type RepositorioRepuesto interface {
|
||||
IRepositorio[*models.Repuesto]
|
||||
AltaRepuesto(id uint, uid uint) (*models.Repuesto, error)
|
||||
ObtenerTodosConMarca() ([]*models.Repuesto, error)
|
||||
}
|
||||
|
||||
type repositorioRepuesto struct {
|
||||
@@ -25,6 +26,14 @@ func NuevoRepositorioRepuesto(db *gorm.DB) RepositorioRepuesto {
|
||||
}
|
||||
}
|
||||
|
||||
func (r repositorioRepuesto) ObtenerTodosConMarca() ([]*models.Repuesto, error) {
|
||||
var repuestos []*models.Repuesto
|
||||
if err := r.db.Unscoped().Preload("Marca").Find(&repuestos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return repuestos, nil
|
||||
}
|
||||
|
||||
// Revierte la baja de un producto
|
||||
func (r repositorioRepuesto) AltaRepuesto(id uint, uid uint) (*models.Repuesto, error) {
|
||||
rep := models.Repuesto{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script>
|
||||
|
||||
import Header from '$lib/components/Header.svelte';
|
||||
import {
|
||||
Button,
|
||||
@@ -31,7 +32,7 @@
|
||||
let openEditar = $state(false);
|
||||
|
||||
let repuestoEliminar = $state({ id: 0 });
|
||||
let repuestoEditar = $state({ id: 0 });
|
||||
let repuestoEditar = $state({ ID: 0, marcaId:0 });
|
||||
let repuestoAlta = $state({ id: 0 });
|
||||
let repuestoInfo = $state();
|
||||
|
||||
@@ -41,13 +42,24 @@
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {any} repuesto
|
||||
* @param {string} key
|
||||
*/
|
||||
function valorCelda(repuesto, key) {
|
||||
const valor = key
|
||||
.split('.')
|
||||
.reduce((objeto, propiedad) => objeto?.[propiedad], repuesto);
|
||||
return valor ?? '-';
|
||||
}
|
||||
|
||||
const headers = [
|
||||
{ key: 'id', value: 'ID' },
|
||||
{ key: 'ID', value: 'ID' },
|
||||
{ key: 'nombre', value: 'Nombre' },
|
||||
{ key: 'descripcion', value: 'Descripción' },
|
||||
{ key: 'precio', value: 'Precio' },
|
||||
{ key: 'marcaId', value: 'Marca' },
|
||||
{ key: 'deleted_at', value: 'Estado' }
|
||||
{ key: 'marca.nombre', value: 'Marca' },
|
||||
{ key: 'DeletedAt', value: 'Estado' }
|
||||
];
|
||||
</script>
|
||||
|
||||
@@ -81,7 +93,17 @@
|
||||
{#each data.repuestos as p}
|
||||
<TableRow>
|
||||
{#each headers as header}
|
||||
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
||||
<TableCell>
|
||||
{#if header.key === 'DeletedAt'}
|
||||
{#if p.DeletedAt}
|
||||
<span title="Dado de baja">✗</span>
|
||||
{:else}
|
||||
<span title="Activo">✔</span>
|
||||
{/if}
|
||||
{:else}
|
||||
{String(valorCelda(p, header.key))}
|
||||
{/if}
|
||||
</TableCell>
|
||||
{/each}
|
||||
<TableCell class="acciones-cell">
|
||||
<!-- onclick={() => abrirModalEditar(p)} -->
|
||||
@@ -90,8 +112,8 @@
|
||||
size="small"
|
||||
icon={Edit}
|
||||
onclick={() => {
|
||||
repuestoEditar = p;
|
||||
repuestoEditar.marca = p.marcaId;
|
||||
repuestoEditar = p;
|
||||
repuestoEditar.marcaId = p.marcaId;
|
||||
openEditar = true;
|
||||
}}>Editar</Button
|
||||
>
|
||||
@@ -105,7 +127,7 @@
|
||||
size="small"
|
||||
icon={Information}>Ver registro</Button
|
||||
>
|
||||
{#if p.deleted_at == '✔'}
|
||||
{#if !p.DeletedAt}
|
||||
<Button
|
||||
onclick={() => {
|
||||
repuestoEliminar = p;
|
||||
@@ -165,7 +187,7 @@
|
||||
bind:open={openEditar}
|
||||
onClose={() => {
|
||||
openEditar = false;
|
||||
repuestoEditar = { id: 0 };
|
||||
repuestoEditar = { ID: 0, marcaId: 0 };
|
||||
}}
|
||||
bind:repuesto={repuestoEditar}
|
||||
></EditarRepuestoForm>
|
||||
|
||||
@@ -8,11 +8,6 @@ export async function load({ fetch, depends }) {
|
||||
let error = '';
|
||||
if (typeof repuestos === 'string') {
|
||||
error = repuestos;
|
||||
} else {
|
||||
if (repuestos.length != 0 && typeof repuestos[0].deleted_at !== 'undefined')
|
||||
repuestos.forEach((x:any) => {
|
||||
x.deleted_at = x.deleted_at ? '✗' : '✔';
|
||||
});
|
||||
}
|
||||
return {
|
||||
repuestos: repuestos,
|
||||
|
||||
@@ -7,18 +7,18 @@
|
||||
show = $bindable(),
|
||||
onclose,
|
||||
repuesto
|
||||
}: { show?: boolean; onclose: () => void; repuesto: { id: number } } = $props();
|
||||
}: { show?: boolean; onclose: () => void; repuesto: { ID: number } } = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
async function handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
error = '';
|
||||
if (!repuesto.id) {
|
||||
if (!repuesto.ID) {
|
||||
error = 'No se pudo obtener la id del Repuesto? recarga la pagina.';
|
||||
return;
|
||||
}
|
||||
const result = await altaRepuesto(repuesto.id);
|
||||
const result = await altaRepuesto(repuesto.ID);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
|
||||
@@ -1,17 +1,32 @@
|
||||
<script lang="ts">
|
||||
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
|
||||
import { Button, Modal, NumberInput, TextInput, ComboBox } from 'carbon-components-svelte';
|
||||
import type { CrearRepuestoDto } from '../../types';
|
||||
import { crearRepuesto } from '$lib/hooks/repuestos/crearRepuesto';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import type { Marca } from '../../types';
|
||||
import { obtenerMarcas } from '$lib/hooks/marcas/obtenerMarcas';
|
||||
|
||||
let { open = $bindable(), onClose } = $props();
|
||||
|
||||
let marcas: Marca[] = $state([]);
|
||||
|
||||
$effect(() => {
|
||||
let cancelado = false;
|
||||
(async () => {
|
||||
const data = await obtenerMarcas();
|
||||
if (!cancelado && Array.isArray(data)) marcas = data;
|
||||
})();
|
||||
return () => {
|
||||
cancelado = true;
|
||||
};
|
||||
});
|
||||
|
||||
let error = $state('');
|
||||
let enviando = $state(false);
|
||||
let repuesto: CrearRepuestoDto = $state({
|
||||
nombre: '',
|
||||
descripcion: '',
|
||||
marca: '',
|
||||
marcaId: 0,
|
||||
aliases: [''],
|
||||
precio: 0,
|
||||
stockMinimo: 0
|
||||
@@ -24,7 +39,7 @@
|
||||
repuesto = {
|
||||
nombre: '',
|
||||
descripcion: '',
|
||||
marca: '',
|
||||
marcaId: 0,
|
||||
aliases: [],
|
||||
precio: 0,
|
||||
stockMinimo: 0
|
||||
@@ -39,10 +54,10 @@
|
||||
if (enviando) return;
|
||||
enviando = true;
|
||||
error = '';
|
||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||
if (repuesto.marca == '') error += 'Falta Marca, ';
|
||||
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
|
||||
if (repuesto.nombre == '') error += 'Falta Nombre';
|
||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||
if (!repuesto.marcaId) error += 'Falta Marca, ';
|
||||
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
|
||||
if (repuesto.nombre == '') error += 'Falta Nombre';
|
||||
|
||||
if (error == '') {
|
||||
error = await crearRepuesto(repuesto);
|
||||
@@ -74,7 +89,12 @@
|
||||
labelText="Descripcion: "
|
||||
bind:value={repuesto.descripcion}
|
||||
/>
|
||||
<TextInput name="marca" type="number" labelText="Marca: " bind:value={repuesto.marca} />
|
||||
<ComboBox
|
||||
labelText="Marca"
|
||||
items={marcas.map((m) => ({ id: m.ID, text: m.nombre }))}
|
||||
itemToString={(item) => (item ? item.text : '')}
|
||||
bind:selectedId={repuesto.marcaId}
|
||||
/>
|
||||
|
||||
<NumberInput name="precio" labelText="Precio: " bind:value={repuesto.precio} />
|
||||
<NumberInput
|
||||
|
||||
@@ -1,14 +1,39 @@
|
||||
<script lang="ts">
|
||||
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
|
||||
import { Button, Modal, NumberInput, TextInput, ComboBox } from 'carbon-components-svelte';
|
||||
import type { ActualizarRepuestoDto } from '../../types';
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { actualizarRepuesto } from '$lib/hooks/repuestos/actualizarRepuesto';
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
repuesto = $bindable()
|
||||
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
||||
import { obtenerMarcas } from '$lib/hooks/marcas/obtenerMarcas';
|
||||
import type { Marca } from '../../types';
|
||||
let marcas: Marca[] = $state([]);
|
||||
let selectedBrand: { id: number; text: string } | null = $state(null);
|
||||
|
||||
// Cargar marcas
|
||||
$effect(() => {
|
||||
let cancelado = false;
|
||||
(async () => {
|
||||
const data = await obtenerMarcas();
|
||||
if (!cancelado && Array.isArray(data)) marcas = data;
|
||||
})();
|
||||
return () => {
|
||||
cancelado = true;
|
||||
};
|
||||
});
|
||||
|
||||
// Sincronizar la selección cuando el repuesto a editar ya tiene marcaId
|
||||
$effect( () => {
|
||||
if (repuesto && repuesto.marcaId && marcas.length) {
|
||||
const m = marcas.find((b) => b.ID === repuesto.marcaId);
|
||||
selectedBrand = m ? { id: m.ID, text: m.nombre } : null;
|
||||
}
|
||||
})
|
||||
|
||||
let {
|
||||
open = $bindable(),
|
||||
onClose,
|
||||
repuesto = $bindable()
|
||||
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
||||
|
||||
let error = $state('');
|
||||
let enviando = $state(false);
|
||||
@@ -19,10 +44,10 @@
|
||||
}
|
||||
function reset() {
|
||||
repuesto = {
|
||||
id: 0,
|
||||
ID: 0,
|
||||
nombre: '',
|
||||
descripcion: '',
|
||||
marca: '',
|
||||
marcaId: 0,
|
||||
aliases: [],
|
||||
precio: 0,
|
||||
stockMinimo: 0
|
||||
@@ -37,11 +62,10 @@
|
||||
if (enviando) return;
|
||||
enviando = true;
|
||||
error = '';
|
||||
if (repuesto.id == 0) error += 'Falta id';
|
||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||
if (repuesto.marca == '') error += 'Falta Marca, ';
|
||||
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
|
||||
if (repuesto.nombre == '') error += 'Falta Nombre';
|
||||
if (repuesto.ID == 0) error += 'Falta id';
|
||||
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
|
||||
if (repuesto.nombre == '') error += 'Falta Nombre';
|
||||
|
||||
if (error == '') {
|
||||
error = await actualizarRepuesto(repuesto);
|
||||
@@ -57,7 +81,7 @@
|
||||
formId="actualizarRepuestoForm"
|
||||
bind:open
|
||||
size={'lg'}
|
||||
modalHeading={`Editar Repuesto: ${repuesto.id}`}
|
||||
modalHeading={`Editar Repuesto: ${repuesto.ID}`}
|
||||
primaryButtonText="Editar"
|
||||
primaryButtonDisabled={enviando}
|
||||
secondaryButtonText="Cerrar"
|
||||
@@ -73,12 +97,11 @@
|
||||
labelText="Descripcion: "
|
||||
bind:value={repuesto.descripcion}
|
||||
/>
|
||||
<TextInput
|
||||
name="marca"
|
||||
type="text"
|
||||
labelText="ID Marca: "
|
||||
maxlength={3}
|
||||
bind:value={repuesto.marca}
|
||||
<ComboBox
|
||||
labelText="Marca"
|
||||
items={marcas.map((m) => ({ id: m.ID, text: m.nombre }))}
|
||||
itemToString={(item) => (item ? item.text : '')}
|
||||
selectedId={repuesto.marcaId}
|
||||
/>
|
||||
|
||||
<NumberInput name="precio" labelText="Precio: " bind:value={repuesto.precio} />
|
||||
|
||||
@@ -7,23 +7,31 @@
|
||||
show = $bindable(),
|
||||
onclose,
|
||||
repuesto
|
||||
}: { show?: boolean; onclose: () => void; repuesto: { id: number } } = $props();
|
||||
}: { show?: boolean; onclose: () => void; repuesto: { ID: number } } = $props();
|
||||
|
||||
let error = $state('');
|
||||
let eliminando = $state(false);
|
||||
|
||||
async function handleSubmit(e: Event) {
|
||||
e.preventDefault();
|
||||
if (eliminando) return;
|
||||
error = '';
|
||||
if (!repuesto.id) {
|
||||
if (!repuesto.ID) {
|
||||
error = 'No se pudo obtener la id del Repuesto? recarga la pagina.';
|
||||
return;
|
||||
}
|
||||
const result = await eliminarRepuesto(repuesto.id);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
eliminando = true;
|
||||
try {
|
||||
const result = await eliminarRepuesto(repuesto.ID);
|
||||
if (result !== 'Ok') {
|
||||
error = result;
|
||||
return;
|
||||
}
|
||||
invalidate("repuestos:load")
|
||||
handleClose();
|
||||
} finally {
|
||||
eliminando = false;
|
||||
}
|
||||
handleClose();
|
||||
}
|
||||
|
||||
function handleClose() {
|
||||
@@ -38,7 +46,8 @@
|
||||
formId="eliminarRepuestoForm"
|
||||
bind:open={show}
|
||||
modalHeading="Eliminar Repuesto"
|
||||
primaryButtonText="Eliminar"
|
||||
primaryButtonText={eliminando ? 'Eliminando...' : 'Eliminar'}
|
||||
primaryButtonDisabled={eliminando}
|
||||
secondaryButtonText="Cerrar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
|
||||
Vendored
+3
-3
@@ -8,7 +8,7 @@ export interface Usuario {
|
||||
export interface CrearRepuestoDto {
|
||||
nombre: string;
|
||||
descripcion: string;
|
||||
marca: string;
|
||||
marcaId: number;
|
||||
aliases: string[];
|
||||
precio: number;
|
||||
stockMinimo: number;
|
||||
@@ -39,10 +39,10 @@ export interface LogDetalle {
|
||||
}
|
||||
|
||||
interface ActualizarRepuestoDto {
|
||||
id: number;
|
||||
ID: number;
|
||||
nombre: string;
|
||||
descripcion: string;
|
||||
marca: string;
|
||||
marcaId: number;
|
||||
aliases: string[];
|
||||
precio: number;
|
||||
stockMinimo: number;
|
||||
|
||||
Reference in new issue
Block a user