Private
Public Access
añadida funcionalidad de crear marcas
This commit is contained in:
1 parent
9e847396e3
commit
4625f75a45
9 files changed
+271
-21
No files matched your search
@@ -0,0 +1,33 @@
|
|||||||
|
package marca
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"repuestera_back/models"
|
||||||
|
r "repuestera_back/repositorios"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CrearMarca(c *gin.Context) {
|
||||||
|
var marca models.Marca
|
||||||
|
if err := c.ShouldBindJSON(&marca); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := r.ObtenerRepositorios().Marca.ObtenerPorNombre(marca.Nombre)
|
||||||
|
if err == nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ya existe una marca con ese nombre"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
uid := c.GetUint("userID")
|
||||||
|
|
||||||
|
err = r.ObtenerRepositorios().Marca.Crear(marca, uid)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Status(http.StatusCreated)
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
type RepositorioMarcas interface {
|
type RepositorioMarcas interface {
|
||||||
IRepositorio[models.Marca]
|
IRepositorio[models.Marca]
|
||||||
CrearFaltante(marca models.Marca) error
|
CrearFaltante(marca models.Marca) error
|
||||||
|
ObtenerPorNombre(nombre string) (*models.Marca, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type repositorioMarcas struct {
|
type repositorioMarcas struct {
|
||||||
@@ -24,16 +25,24 @@ func NuevoRepositorioMarcas(db *gorm.DB) RepositorioMarcas {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repositorioMarcas) CrearFaltante(marca models.Marca) (error) {
|
func (r *repositorioMarcas) ObtenerPorNombre(nombre string) (*models.Marca, error) {
|
||||||
var marcaa models.Marca
|
var marca *models.Marca
|
||||||
if err := r.db.Where("nombre = ?", marca.Nombre).First(&marcaa).Error; err != nil {
|
if err := r.db.Where("nombre = ?", nombre).First(&marca).Error; err != nil {
|
||||||
if err == gorm.ErrRecordNotFound {
|
return nil, err
|
||||||
if err := r.db.Create(&marca).Error; err != nil {
|
}
|
||||||
return err
|
return marca, nil
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
func (r *repositorioMarcas) CrearFaltante(marca models.Marca) error {
|
||||||
|
var marcaa models.Marca
|
||||||
|
if err := r.db.Where("nombre = ?", marca.Nombre).First(&marcaa).Error; err != nil {
|
||||||
|
if err == gorm.ErrRecordNotFound {
|
||||||
|
if err := r.db.Create(&marca).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,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("/", repuestos.CrearRepuesto)
|
repuestoGrupo.POST("/", marca.CrearMarca)
|
||||||
// repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
// repuestoGrupo.PUT("/:id", repuestos.ActualizarRepuesto)
|
||||||
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
// repuestoGrupo.DELETE("/:id", repuestos.EliminarRepuesto)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { API_URL } from "$lib/env";
|
||||||
|
import { getToken } from "../token";
|
||||||
|
|
||||||
|
export async function crearMarca(nombre: string){
|
||||||
|
nombre = nombre.trim()
|
||||||
|
if (nombre == "") return "No se acepta un nombre vacio o con espacios"
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/marcas/`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({nombre: nombre})
|
||||||
|
});
|
||||||
|
if (!req.ok) {
|
||||||
|
const data:{error:string} = await req.json();
|
||||||
|
return data.error || "No se pudo crear la marca";
|
||||||
|
}
|
||||||
|
return "ok";
|
||||||
|
} catch {
|
||||||
|
return "No se pudo alcanzar el servidor"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { API_URL } from "$lib/env";
|
||||||
|
import { handleResponse } from "$lib/handleRespondeJSON";
|
||||||
|
import type { Marca } from "../../../types";
|
||||||
|
import { getToken } from "../token";
|
||||||
|
|
||||||
|
export async function obtenerMarcas(fetch2?: Function) {
|
||||||
|
const fetchFn = fetch2 ? fetch2 : fetch;
|
||||||
|
const req = await fetchFn(`${API_URL}/marcas/`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (!req.ok) return "Hubo un error y no se pudieron obtener las marcas";
|
||||||
|
return await req.json();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import Header from '$lib/components/Header.svelte';
|
||||||
|
import { Button, DataTableSkeleton, InlineNotification, Table, TableBody, TableCell, TableContainer, TableHead, TableHeader, TableRow } from 'carbon-components-svelte';
|
||||||
|
import { Add, ArrowUp, Edit, Information, TrashCan } from 'carbon-icons-svelte';
|
||||||
|
import type { Marca } from '../../types';
|
||||||
|
import CrearMarcaForm from './CrearMarcaForm.svelte';
|
||||||
|
|
||||||
|
let error = $state("");
|
||||||
|
|
||||||
|
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 : []);
|
||||||
|
|
||||||
|
$effect(()=>{
|
||||||
|
if (typeof data.marcas === "string"){
|
||||||
|
error = data.marcas
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const headers = [
|
||||||
|
{ key: 'ID', value: 'ID' },
|
||||||
|
{ key: 'nombre', value: 'Nombre' },
|
||||||
|
{ key: 'CreatedAt', value: 'Creado' },
|
||||||
|
{ key: 'UpdatedAt', value: 'Actualizado' },
|
||||||
|
{ key: 'DeletedAt', value: 'Estado' }
|
||||||
|
];
|
||||||
|
|
||||||
|
let openCrear = $state(false)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Header />
|
||||||
|
<div class="page-container">
|
||||||
|
<div class="flex justify-between">
|
||||||
|
<h1 class="page-title">Gestión de Marcas</h1>
|
||||||
|
<Button onclick={() => {openCrear = true}} icon={Add}>Nueva Marca</Button>
|
||||||
|
</div>
|
||||||
|
{#if error}
|
||||||
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
||||||
|
{/if}
|
||||||
|
{#if typeof data.marcas === "undefined" || data.marcas === null}
|
||||||
|
<DataTableSkeleton />
|
||||||
|
{:else if marcas.length === 0}
|
||||||
|
<TableContainer title="Marcas">
|
||||||
|
<Table>
|
||||||
|
<TableHead>
|
||||||
|
<TableRow>
|
||||||
|
{#each headers as header}
|
||||||
|
<TableHeader>{header.value}</TableHeader>
|
||||||
|
{/each}
|
||||||
|
<TableHeader>Acciones</TableHeader>
|
||||||
|
</TableRow>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
<TableRow>
|
||||||
|
<TableCell colspan={headers.length+1}>No hay marcas para mostrar registre una</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</TableContainer>
|
||||||
|
{:else}
|
||||||
|
<TableContainer title="Listado de Repuestos">
|
||||||
|
<Table>
|
||||||
|
<TableHead>
|
||||||
|
<TableRow>
|
||||||
|
{#each headers as header}
|
||||||
|
<TableHeader>{header.value}</TableHeader>
|
||||||
|
{/each}
|
||||||
|
<TableHeader>Acciones</TableHeader>
|
||||||
|
</TableRow>
|
||||||
|
</TableHead>
|
||||||
|
<TableBody>
|
||||||
|
{#each marcas as p}
|
||||||
|
<TableRow>
|
||||||
|
{#each headers as header}
|
||||||
|
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
||||||
|
{/each}
|
||||||
|
<TableCell>{console.log(p)}
|
||||||
|
<Button kind="ghost" size="small" icon={Edit} onclick={() => {}}>Editar</Button>
|
||||||
|
{#if p.DeletedAt}
|
||||||
|
<Button kind="ghost" size="small" icon={ArrowUp} onclick={() => {}}>Alta</Button>
|
||||||
|
{:else}
|
||||||
|
<Button kind="ghost" size="small" icon={TrashCan} onclick={() => {}}>Eliminar</Button>
|
||||||
|
{/if}
|
||||||
|
</TableCell>
|
||||||
|
</TableRow>
|
||||||
|
{/each}
|
||||||
|
</TableBody>
|
||||||
|
</Table>
|
||||||
|
</TableContainer>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<CrearMarcaForm bind:open={openCrear} onClose={()=> openCrear = false}>
|
||||||
|
</CrearMarcaForm>
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { obtenerMarcas } from '$lib/hooks/marcas/obtenerMarcas';
|
||||||
|
|
||||||
|
export async function load({depends, fetch }) {
|
||||||
|
depends("marcas:load")
|
||||||
|
const marcas = await obtenerMarcas(fetch);
|
||||||
|
|
||||||
|
return { marcas : marcas}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Modal, TextInput } from 'carbon-components-svelte';
|
||||||
|
import type { Marca } from '../../types';
|
||||||
|
import { crearMarca } from '$lib/hooks/marcas/crearMarca';
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
|
||||||
|
// Props to control modal visibility
|
||||||
|
let { open = $bindable(), onClose } = $props();
|
||||||
|
|
||||||
|
let error = $state('');
|
||||||
|
let enviando = $state(false);
|
||||||
|
let nombre = $state('');
|
||||||
|
|
||||||
|
function handleClose() {
|
||||||
|
reset();
|
||||||
|
onClose?.();
|
||||||
|
}
|
||||||
|
function reset() {
|
||||||
|
nombre = '';
|
||||||
|
error = '';
|
||||||
|
enviando = false;
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<form
|
||||||
|
id="crearMarcaForm"
|
||||||
|
onsubmit={async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (enviando) return;
|
||||||
|
enviando = true;
|
||||||
|
error = '';
|
||||||
|
if (!nombre.trim()) {
|
||||||
|
error = 'Falta Nombre';
|
||||||
|
}
|
||||||
|
if (error === '') {
|
||||||
|
const result = await crearMarca(nombre );
|
||||||
|
if (result === 'ok') {
|
||||||
|
invalidate('marcas:load');
|
||||||
|
handleClose();
|
||||||
|
} else {
|
||||||
|
error = result ?? 'Error desconocido';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enviando = false;
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Modal
|
||||||
|
formId="crearMarcaForm"
|
||||||
|
bind:open
|
||||||
|
size={'lg'}
|
||||||
|
modalHeading="Registrar Marca"
|
||||||
|
primaryButtonText="Registrar"
|
||||||
|
primaryButtonDisabled={enviando}
|
||||||
|
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={nombre}
|
||||||
|
maxlength={70}
|
||||||
|
/>
|
||||||
|
<div class="text-sm text-gray-500 mt-1">{String(nombre).length}/70</div>
|
||||||
|
</Modal>
|
||||||
|
</form>
|
||||||
Vendored
+8
-12
@@ -87,12 +87,16 @@ export interface LoginResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface BaseModel {
|
export interface BaseModel {
|
||||||
id: number;
|
ID: number;
|
||||||
createdAt: string;
|
CreatedAt: string;
|
||||||
updatedAt: string;
|
UpdatedAt: string;
|
||||||
deletedAt?: string | null;
|
DeletedAt?: string | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Marca extends BaseModel {
|
||||||
|
nombre: string;
|
||||||
|
repuestos: Repuesto[];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface Repuesto extends BaseModel {
|
export interface Repuesto extends BaseModel {
|
||||||
@@ -105,11 +109,3 @@ export interface Repuesto extends BaseModel {
|
|||||||
aliases: string[];
|
aliases: string[];
|
||||||
// stocks?: Stock[]; // opcional si tienes entidad Stock
|
// stocks?: Stock[]; // opcional si tienes entidad Stock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface Marca extends BaseModel {
|
|
||||||
nombre: string;
|
|
||||||
repuestos: Repuesto[];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Reference in new issue
Block a user