This commit is contained in:
fede committed 2026-06-19 23:04:13 -03:00
1 parent 6c0e44cb45
commit 90e76d4963
11 files changed
+112 -38

No files matched your search

+2 -2
View File
@@ -12,13 +12,13 @@ import (
func CrearRepuesto(c *gin.Context){
var repuesto models.Repuesto
var repuesto *models.Repuesto
if err := c.ShouldBindJSON(&repuesto); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err := r.ObtenerRepositorios().Repuesto.Crear(&repuesto); err != nil {
if err := r.ObtenerRepositorios().Repuesto.Crear(repuesto); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
@@ -1,9 +1,25 @@
package repuestos
import (
"net/http"
"strconv"
r "repuestera_back/repositorios"
"github.com/gin-gonic/gin"
)
func ObtenerRepuesto(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
}
repuesto, err := r.ObtenerRepositorios().Repuesto.ObtenerPorID(uint(id))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, repuesto)
}
+8 -8
View File
@@ -9,16 +9,16 @@ import (
type Repuesto struct {
ID uint64 `gorm:"primaryKey" json:"id"`
Nombre string `gorm:"type:varchar(50);not null" json:"nombre"`
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio"`
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo"`
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon"`
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId"`
Nombre string `gorm:"type:varchar(50);not null" json:"nombre" binding:"required"`
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio" binding:"required"`
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo" binding:"required"`
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon" binding:"required"`
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId" binding:"required"`
Aliases []string `gorm:"type:json" json:"aliases,omitempty"`
Stocks []Stock `gorm:"foreignKey:RepuestoID" json:"stocks,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (Repuesto) TableName() string {
+4 -4
View File
@@ -8,7 +8,7 @@ type IRepositorio[T any] interface {
Guardar(entidad *T) error
ObtenerTodos() ([]T, error)
ObtenerPorID(id uint) (*T, error)
Crear(entidad *T) error
Crear(entidad T) error
Actualizar(entidad *T, input T) error
Eliminar(id uint) error
}
@@ -41,8 +41,8 @@ func (r *RepositorioBase[T]) ObtenerPorID(id uint) (*T, error) {
return &entidad, nil
}
func (r *RepositorioBase[T]) Crear(entidad *T) error {
return r.db.Create(entidad).Error
func (r *RepositorioBase[T]) Crear(entidad T) error {
return r.db.Create(&entidad).Error
}
func (r *RepositorioBase[T]) Actualizar(entidad *T, input T) error {
@@ -52,4 +52,4 @@ func (r *RepositorioBase[T]) Actualizar(entidad *T, input T) error {
func (r *RepositorioBase[T]) Eliminar(id uint) error {
var entidad T
return r.db.Delete(&entidad, id).Error
}
}
+1
View File
@@ -48,6 +48,7 @@ func main() {
routes.PermisoRoute(r)
routes.GroupRoute(r)
routes.UserRoute(r)
routes.RepuestoRoute(r)
r.Run(":8080")
}
+19
View File
@@ -0,0 +1,19 @@
package routes
import (
"repuestera_back/controllers/repuestos"
"repuestera_back/middleware"
"github.com/gin-gonic/gin"
)
func RepuestoRoute(r *gin.Engine) {
repuestoGrupo := r.Group("/repuestos")
repuestoGrupo.Use(middleware.AuthMiddleware(), middleware.PermisoMiddleware(4))
{
repuestoGrupo.GET("/", repuestos.ObtenerRepuestos)
repuestoGrupo.GET("/:id", repuestos.ObtenerRepuesto)
repuestoGrupo.POST("/", repuestos.CrearRepuesto)
}
}
@@ -0,0 +1,22 @@
import { API_URL } from '$lib/env';
import { getToken } from '../token';
export async function obtenerRepuestos(fetch2?: Function) {
const fetchfn = fetch2 ? fetch2 : fetch;
try {
const req = await fetchfn(`${API_URL}/repuestos`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${getToken()}`
}
});
const data = await req.json();
if (req.ok) {
return data;
}
return data.error;
} catch {
return 'no se puso alcanzar el servidor';
}
}
+2 -10
View File
@@ -6,17 +6,9 @@ export const load: PageLoad = async ({ fetch }) => {
let grupos: Grupo[] = [];
let permisos: Permiso[] = [];
try {
grupos = await obtenerGrupos(fetch);
} catch {
// token might not be available on server
}
grupos = await obtenerGrupos(fetch);
try {
permisos = await obtenerPermisos(fetch);
} catch {
// token might not be available on server
}
permisos = await obtenerPermisos(fetch);
return { grupos, permisos };
};
+22 -13
View File
@@ -18,8 +18,15 @@
let error = $state('');
let loading = $state('');
let { data } = $props();
let openCrear = $state(false);
$effect(() => {
if (data.error) {
error = data.error;
}
});
const headers = [
{ key: 'id', value: 'ID' },
{ key: 'nombre', value: 'Nombre' },
@@ -54,19 +61,21 @@
</TableRow>
</TableHead>
<TableBody>
{#each [{ id: 1 }] as p (p.id)}
<TableRow>
{#each headers as header}
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
{/each}
<TableCell class="acciones-cell">
<!-- onclick={() => abrirModalEditar(p)} -->
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
<!-- onclick={() => abrirModalEliminar(p)} -->
<Button kind="ghost" size="small" icon={TrashCan}>Eliminar</Button>
</TableCell>
</TableRow>
{/each}
{#if typeof data.repuestos !== 'string'}
{#each data.repuestos as p}
<TableRow>
{#each headers as header}
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
{/each}
<TableCell class="acciones-cell">
<!-- onclick={() => abrirModalEditar(p)} -->
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
<!-- onclick={() => abrirModalEliminar(p)} -->
<Button kind="ghost" size="small" icon={TrashCan}>Eliminar</Button>
</TableCell>
</TableRow>
{/each}
{/if}
</TableBody>
</Table>
</TableContainer>
+15
View File
@@ -0,0 +1,15 @@
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
export let ssr = false;
export async function load({ fetch }) {
const repuestos = await obtenerRepuestos(fetch);
let error = '';
if (typeof repuestos === 'string') {
error = repuestos;
}
return {
repuestos: repuestos,
error: error
};
}
@@ -1,6 +1,6 @@
<script>
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
import { Add, CPlusPlus } from 'carbon-icons-svelte';
import { Add } from 'carbon-icons-svelte';
let { open = $bindable(), onClose } = $props();