Private
Public Access
falla
This commit is contained in:
1 parent
6c0e44cb45
commit
90e76d4963
11 files changed
+112
-38
No files matched your search
@@ -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)
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,7 @@ func main() {
|
||||
routes.PermisoRoute(r)
|
||||
routes.GroupRoute(r)
|
||||
routes.UserRoute(r)
|
||||
routes.RepuestoRoute(r)
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user