diff --git a/back/controllers/repuestos/CrearRepuesto.go b/back/controllers/repuestos/CrearRepuesto.go
index cf79b5b..c2cac40 100644
--- a/back/controllers/repuestos/CrearRepuesto.go
+++ b/back/controllers/repuestos/CrearRepuesto.go
@@ -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
}
diff --git a/back/controllers/repuestos/ObtenerRepuesto.go b/back/controllers/repuestos/ObtenerRepuesto.go
index 876b3ab..05ea044 100644
--- a/back/controllers/repuestos/ObtenerRepuesto.go
+++ b/back/controllers/repuestos/ObtenerRepuesto.go
@@ -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)
}
diff --git a/back/models/repuesto.go b/back/models/repuesto.go
index 9f8f438..ae295be 100644
--- a/back/models/repuesto.go
+++ b/back/models/repuesto.go
@@ -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 {
diff --git a/back/repositorios/IRepositorio.go b/back/repositorios/IRepositorio.go
index deb3701..f43c243 100644
--- a/back/repositorios/IRepositorio.go
+++ b/back/repositorios/IRepositorio.go
@@ -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
-}
\ No newline at end of file
+}
diff --git a/back/router.go b/back/router.go
index ea2133a..8070ef7 100644
--- a/back/router.go
+++ b/back/router.go
@@ -48,6 +48,7 @@ func main() {
routes.PermisoRoute(r)
routes.GroupRoute(r)
routes.UserRoute(r)
+ routes.RepuestoRoute(r)
r.Run(":8080")
}
diff --git a/back/routes/RepuestoRoute.go b/back/routes/RepuestoRoute.go
new file mode 100644
index 0000000..3e2a7d2
--- /dev/null
+++ b/back/routes/RepuestoRoute.go
@@ -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)
+
+ }
+}
diff --git a/front/src/lib/hooks/repuestos/obtenerRepuestos.ts b/front/src/lib/hooks/repuestos/obtenerRepuestos.ts
new file mode 100644
index 0000000..d70bd16
--- /dev/null
+++ b/front/src/lib/hooks/repuestos/obtenerRepuestos.ts
@@ -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';
+ }
+}
diff --git a/front/src/routes/grupos/+page.ts b/front/src/routes/grupos/+page.ts
index da970d1..811abdb 100644
--- a/front/src/routes/grupos/+page.ts
+++ b/front/src/routes/grupos/+page.ts
@@ -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 };
};
diff --git a/front/src/routes/repuestos/+page.svelte b/front/src/routes/repuestos/+page.svelte
index bcdd388..abefccf 100644
--- a/front/src/routes/repuestos/+page.svelte
+++ b/front/src/routes/repuestos/+page.svelte
@@ -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 @@
- {#each [{ id: 1 }] as p (p.id)}
-
- {#each headers as header}
- {String(p[header.key] ?? '-')}
- {/each}
-
-
-
-
-
-
-
- {/each}
+ {#if typeof data.repuestos !== 'string'}
+ {#each data.repuestos as p}
+
+ {#each headers as header}
+ {String(p[header.key] ?? '-')}
+ {/each}
+
+
+
+
+
+
+
+ {/each}
+ {/if}
diff --git a/front/src/routes/repuestos/+page.ts b/front/src/routes/repuestos/+page.ts
new file mode 100644
index 0000000..45ac5bc
--- /dev/null
+++ b/front/src/routes/repuestos/+page.ts
@@ -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
+ };
+}
diff --git a/front/src/routes/repuestos/CrearRepuestoForm.svelte b/front/src/routes/repuestos/CrearRepuestoForm.svelte
index 953573d..6f3ec78 100644
--- a/front/src/routes/repuestos/CrearRepuestoForm.svelte
+++ b/front/src/routes/repuestos/CrearRepuestoForm.svelte
@@ -1,6 +1,6 @@