Private
Public Access
wip: skeleton de la funcionalidad falta arreglar un bug
This commit is contained in:
1 parent
266365ae0f
commit
f9cf001fca
9 files changed
+270
-1
No files matched your search
@@ -1,9 +1,51 @@
|
||||
package repuestos
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_builders "repuestera_back/dto/Builders"
|
||||
dto "repuestera_back/dto/Repuestos"
|
||||
"repuestera_back/models"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ActualizarRepuesto(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
|
||||
}
|
||||
|
||||
var data dto.ActualizarRepuestoDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
data.ID = &id
|
||||
|
||||
marca, err := r.ObtenerRepositorios().Marca.ObtenerPorId(data.Marca)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
repuesto := dto_builders.NewActualizarRepuestoBuilder().
|
||||
ConDto(data).
|
||||
ConMarca(marca.ID).
|
||||
Build()
|
||||
|
||||
entity := &models.Repuesto{ID: id}
|
||||
if err := r.ObtenerRepositorios().Repuesto.Actualizar(&entity, &repuesto); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, repuesto)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dto_builders
|
||||
|
||||
import (
|
||||
dto "repuestera_back/dto/Repuestos"
|
||||
"repuestera_back/models"
|
||||
)
|
||||
|
||||
type ActualizarRepuestoBuilder struct {
|
||||
dto *dto.ActualizarRepuestoDto
|
||||
marcaID string
|
||||
}
|
||||
|
||||
func NewActualizarRepuestoBuilder() *ActualizarRepuestoBuilder {
|
||||
return &ActualizarRepuestoBuilder{}
|
||||
}
|
||||
|
||||
func (b *ActualizarRepuestoBuilder) ConDto(dto dto.ActualizarRepuestoDto) *ActualizarRepuestoBuilder {
|
||||
b.dto = &dto
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *ActualizarRepuestoBuilder) ConMarca(marcaID string) *ActualizarRepuestoBuilder {
|
||||
b.marcaID = marcaID
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *ActualizarRepuestoBuilder) Build() models.Repuesto {
|
||||
aliases := b.dto.Aliases
|
||||
if aliases == nil {
|
||||
aliases = []string{}
|
||||
}
|
||||
|
||||
repuesto := models.Repuesto{
|
||||
Nombre: b.dto.Nombre,
|
||||
Descripcion: b.dto.Descripcion,
|
||||
Precio: b.dto.Precio,
|
||||
StockMinimo: int64(b.dto.StockMinimo),
|
||||
MarcaID: b.marcaID,
|
||||
Aliases: aliases,
|
||||
}
|
||||
|
||||
if b.dto.ID != nil {
|
||||
repuesto.ID = *b.dto.ID
|
||||
}
|
||||
|
||||
return repuesto
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package dto
|
||||
|
||||
type ActualizarRepuestoDto struct {
|
||||
ID *uint64 `json:"id,omitempty"`
|
||||
Nombre string `json:"nombre"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Marca string `json:"marca"`
|
||||
Aliases []string `json:"aliases"`
|
||||
Precio float64 `json:"precio"`
|
||||
StockMinimo int `json:"stockMinimo"`
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
type RepositorioMarcas interface {
|
||||
IRepositorio[*models.Marca]
|
||||
CrearFaltante(marca models.Marca) error
|
||||
ObtenerPorId(id string) (*models.Marca, error)
|
||||
}
|
||||
|
||||
type repositorioMarcas struct {
|
||||
@@ -37,3 +38,10 @@ func (r *repositorioMarcas) CrearFaltante(marca models.Marca) (error) {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *repositorioMarcas) ObtenerPorId(id string) (*models.Marca, error) {
|
||||
var entidad models.Marca
|
||||
if err := r.db.First(&entidad, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &entidad, nil
|
||||
}
|
||||
@@ -14,6 +14,7 @@ func RepuestoRoute(r *gin.Engine) {
|
||||
repuestoGrupo.GET("/", repuestos.ObtenerRepuestos)
|
||||
repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
|
||||
repuestoGrupo.POST("/", repuestos.CrearRepuesto)
|
||||
repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
||||
repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import type { ActualizarRepuestoDto } from '../../../types';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function actualizarRepuesto(repuesto: ActualizarRepuestoDto) {
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/repuestos/${repuesto.id || 0}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify((({ id, ...rest }) => rest)(repuesto))
|
||||
});
|
||||
|
||||
if (req.ok) {
|
||||
return 'Ok';
|
||||
}
|
||||
const data = await req.json();
|
||||
return data.error;
|
||||
} catch {
|
||||
return 'no se puso alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -16,16 +16,20 @@
|
||||
import CrearRepuestoForm from './CrearRepuestoForm.svelte';
|
||||
import EliminarRepuestoForm from './EliminarRepuestoForm.svelte';
|
||||
import ModalStructured from '$lib/components/ModalStructured.svelte';
|
||||
import EditarRepuestoForm from './EditarRepuestoForm.svelte';
|
||||
|
||||
let error = $state('');
|
||||
let loading = $state('');
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
let openCrear = $state(false);
|
||||
let openEliminar = $state(false);
|
||||
let openInfo = $state(false);
|
||||
let openEditar = $state(false);
|
||||
|
||||
let repuestoEliminar = $state({ id: 0 });
|
||||
let repuestoEditar = $state({ id: 0 });
|
||||
let repuestoInfo = $state();
|
||||
|
||||
$effect(() => {
|
||||
@@ -77,7 +81,16 @@
|
||||
{/each}
|
||||
<TableCell class="acciones-cell">
|
||||
<!-- onclick={() => abrirModalEditar(p)} -->
|
||||
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
|
||||
<Button
|
||||
kind="ghost"
|
||||
size="small"
|
||||
icon={Edit}
|
||||
onclick={() => {
|
||||
repuestoEditar = p;
|
||||
repuestoEditar.marca = p.marcaId;
|
||||
openEditar = true;
|
||||
}}>Editar</Button
|
||||
>
|
||||
<Button kind="ghost" size="small">Ver Existencias</Button>
|
||||
<Button
|
||||
onclick={() => {
|
||||
@@ -123,4 +136,12 @@
|
||||
repuesto={repuestoEliminar}
|
||||
></EliminarRepuestoForm>
|
||||
|
||||
<EditarRepuestoForm
|
||||
bind:open={openEditar}
|
||||
onClose={() => {
|
||||
openEditar = false;
|
||||
repuestoEditar = { id: 0 };
|
||||
}}
|
||||
repuesto={repuestoEditar}
|
||||
></EditarRepuestoForm>
|
||||
<ModalStructured objeto={repuestoInfo} bind:open={openInfo} />
|
||||
@@ -0,0 +1,105 @@
|
||||
<script lang="ts">
|
||||
import { Button, Modal, NumberInput, TextInput } 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
|
||||
}: { open: boolean; onClose: Function; repuesto: ActualizarRepuestoDto } = $props();
|
||||
|
||||
let error = $state('');
|
||||
|
||||
function handleClose() {
|
||||
reset();
|
||||
onClose();
|
||||
}
|
||||
function reset() {
|
||||
repuesto = {
|
||||
id: 0,
|
||||
nombre: '',
|
||||
descripcion: '',
|
||||
marca: '',
|
||||
aliases: [],
|
||||
precio: 0,
|
||||
stockMinimo: 0
|
||||
};
|
||||
}
|
||||
</script>
|
||||
|
||||
<form
|
||||
id="actualizarRepuestoForm"
|
||||
onsubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
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 (error == '') {
|
||||
error = await actualizarRepuesto(repuesto);
|
||||
}
|
||||
if (error == 'Ok') {
|
||||
invalidate('repuestos:load');
|
||||
handleClose();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Modal
|
||||
formId="actualizarRepuestoForm"
|
||||
bind:open
|
||||
size={'lg'}
|
||||
modalHeading={`Editar Repuesto: ${repuesto.id}`}
|
||||
primaryButtonText="Editar"
|
||||
secondaryButtonText="Cerrar"
|
||||
on:click:button--secondary={handleClose}
|
||||
>
|
||||
{#if error}
|
||||
<div class="error-msg">{error}</div>
|
||||
{/if}
|
||||
<TextInput name="nombre" type="text" labelText="Nombre: " bind:value={repuesto.nombre} />
|
||||
<TextInput
|
||||
name="descripcion"
|
||||
type="text"
|
||||
labelText="Descripcion: "
|
||||
bind:value={repuesto.descripcion}
|
||||
/>
|
||||
<TextInput name="marca" type="text" labelText="Marca: " bind:value={repuesto.marca} />
|
||||
|
||||
<NumberInput name="precio" labelText="Precio: " bind:value={repuesto.precio} />
|
||||
<NumberInput
|
||||
name="stockMinimo"
|
||||
labelText="Stock Minimo (es para los avisos): "
|
||||
bind:value={repuesto.stockMinimo}
|
||||
/>
|
||||
<div class="my-2!">
|
||||
<h5>Aliases</h5>
|
||||
<table class="w-full">
|
||||
<tbody>
|
||||
{#each repuesto.aliases as _, index}
|
||||
<tr>
|
||||
<td>
|
||||
<TextInput
|
||||
maxlength={70}
|
||||
name={`alias-${index}`}
|
||||
bind:value={repuesto.aliases[index]}
|
||||
on:input={() => {
|
||||
if (index === repuesto.aliases.length - 1 && repuesto.aliases[index] !== '') {
|
||||
repuesto.aliases.push('');
|
||||
} else if (repuesto.aliases[index] == '') {
|
||||
repuesto.aliases.pop();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</Modal>
|
||||
</form>
|
||||
Vendored
+10
@@ -13,6 +13,16 @@ interface CrearRepuestoDto {
|
||||
stockMinimo: number;
|
||||
}
|
||||
|
||||
interface ActualizarRepuestoDto {
|
||||
id: number;
|
||||
nombre: string;
|
||||
descripcion: string;
|
||||
marca: string;
|
||||
aliases: string[];
|
||||
precio: number;
|
||||
stockMinimo: number;
|
||||
}
|
||||
|
||||
export interface Grupo {
|
||||
id: number;
|
||||
nombre: string;
|
||||
|
||||
Reference in new issue
Block a user