Private
Public Access
falla
This commit is contained in:
1 parent
6c0e44cb45
commit
90e76d4963
11 files changed
+111
-37
No files matched your search
@@ -12,13 +12,13 @@ import (
|
|||||||
|
|
||||||
|
|
||||||
func CrearRepuesto(c *gin.Context){
|
func CrearRepuesto(c *gin.Context){
|
||||||
var repuesto models.Repuesto
|
var repuesto *models.Repuesto
|
||||||
if err := c.ShouldBindJSON(&repuesto); err != nil {
|
if err := c.ShouldBindJSON(&repuesto); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
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()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
package repuestos
|
package repuestos
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
r "repuestera_back/repositorios"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ObtenerRepuesto(c *gin.Context){
|
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)
|
||||||
}
|
}
|
||||||
@@ -9,16 +9,16 @@ import (
|
|||||||
|
|
||||||
type Repuesto struct {
|
type Repuesto struct {
|
||||||
ID uint64 `gorm:"primaryKey" json:"id"`
|
ID uint64 `gorm:"primaryKey" json:"id"`
|
||||||
Nombre string `gorm:"type:varchar(50);not null" json:"nombre"`
|
Nombre string `gorm:"type:varchar(50);not null" json:"nombre" binding:"required"`
|
||||||
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio"`
|
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio" binding:"required"`
|
||||||
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo"`
|
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo" binding:"required"`
|
||||||
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon"`
|
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon" binding:"required"`
|
||||||
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId"`
|
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId" binding:"required"`
|
||||||
Aliases []string `gorm:"type:json" json:"aliases,omitempty"`
|
Aliases []string `gorm:"type:json" json:"aliases,omitempty"`
|
||||||
Stocks []Stock `gorm:"foreignKey:RepuestoID" json:"stocks,omitempty"`
|
Stocks []Stock `gorm:"foreignKey:RepuestoID" json:"stocks,omitempty"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Repuesto) TableName() string {
|
func (Repuesto) TableName() string {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ type IRepositorio[T any] interface {
|
|||||||
Guardar(entidad *T) error
|
Guardar(entidad *T) error
|
||||||
ObtenerTodos() ([]T, error)
|
ObtenerTodos() ([]T, error)
|
||||||
ObtenerPorID(id uint) (*T, error)
|
ObtenerPorID(id uint) (*T, error)
|
||||||
Crear(entidad *T) error
|
Crear(entidad T) error
|
||||||
Actualizar(entidad *T, input T) error
|
Actualizar(entidad *T, input T) error
|
||||||
Eliminar(id uint) error
|
Eliminar(id uint) error
|
||||||
}
|
}
|
||||||
@@ -41,8 +41,8 @@ func (r *RepositorioBase[T]) ObtenerPorID(id uint) (*T, error) {
|
|||||||
return &entidad, nil
|
return &entidad, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RepositorioBase[T]) Crear(entidad *T) error {
|
func (r *RepositorioBase[T]) Crear(entidad T) error {
|
||||||
return r.db.Create(entidad).Error
|
return r.db.Create(&entidad).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RepositorioBase[T]) Actualizar(entidad *T, input T) error {
|
func (r *RepositorioBase[T]) Actualizar(entidad *T, input T) error {
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ func main() {
|
|||||||
routes.PermisoRoute(r)
|
routes.PermisoRoute(r)
|
||||||
routes.GroupRoute(r)
|
routes.GroupRoute(r)
|
||||||
routes.UserRoute(r)
|
routes.UserRoute(r)
|
||||||
|
routes.RepuestoRoute(r)
|
||||||
|
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
@@ -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';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,17 +6,9 @@ export const load: PageLoad = async ({ fetch }) => {
|
|||||||
let grupos: Grupo[] = [];
|
let grupos: Grupo[] = [];
|
||||||
let permisos: Permiso[] = [];
|
let permisos: Permiso[] = [];
|
||||||
|
|
||||||
try {
|
grupos = await obtenerGrupos(fetch);
|
||||||
grupos = await obtenerGrupos(fetch);
|
|
||||||
} catch {
|
|
||||||
// token might not be available on server
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
permisos = await obtenerPermisos(fetch);
|
||||||
permisos = await obtenerPermisos(fetch);
|
|
||||||
} catch {
|
|
||||||
// token might not be available on server
|
|
||||||
}
|
|
||||||
|
|
||||||
return { grupos, permisos };
|
return { grupos, permisos };
|
||||||
};
|
};
|
||||||
@@ -18,8 +18,15 @@
|
|||||||
let error = $state('');
|
let error = $state('');
|
||||||
let loading = $state('');
|
let loading = $state('');
|
||||||
|
|
||||||
|
let { data } = $props();
|
||||||
let openCrear = $state(false);
|
let openCrear = $state(false);
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (data.error) {
|
||||||
|
error = data.error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const headers = [
|
const headers = [
|
||||||
{ key: 'id', value: 'ID' },
|
{ key: 'id', value: 'ID' },
|
||||||
{ key: 'nombre', value: 'Nombre' },
|
{ key: 'nombre', value: 'Nombre' },
|
||||||
@@ -54,19 +61,21 @@
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{#each [{ id: 1 }] as p (p.id)}
|
{#if typeof data.repuestos !== 'string'}
|
||||||
<TableRow>
|
{#each data.repuestos as p}
|
||||||
{#each headers as header}
|
<TableRow>
|
||||||
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
{#each headers as header}
|
||||||
{/each}
|
<TableCell>{String(p[header.key] ?? '-')}</TableCell>
|
||||||
<TableCell class="acciones-cell">
|
{/each}
|
||||||
<!-- onclick={() => abrirModalEditar(p)} -->
|
<TableCell class="acciones-cell">
|
||||||
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
|
<!-- onclick={() => abrirModalEditar(p)} -->
|
||||||
<!-- onclick={() => abrirModalEliminar(p)} -->
|
<Button kind="ghost" size="small" icon={Edit}>Editar</Button>
|
||||||
<Button kind="ghost" size="small" icon={TrashCan}>Eliminar</Button>
|
<!-- onclick={() => abrirModalEliminar(p)} -->
|
||||||
</TableCell>
|
<Button kind="ghost" size="small" icon={TrashCan}>Eliminar</Button>
|
||||||
</TableRow>
|
</TableCell>
|
||||||
{/each}
|
</TableRow>
|
||||||
|
{/each}
|
||||||
|
{/if}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
</Table>
|
</Table>
|
||||||
</TableContainer>
|
</TableContainer>
|
||||||
|
|||||||
@@ -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>
|
<script>
|
||||||
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
|
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();
|
let { open = $bindable(), onClose } = $props();
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user