Private
Public Access
funcionalidad de alta repuesto terminada
This commit is contained in:
1 parent
d0500dc764
commit
352973d2f5
15 files changed
+218
-38
No files matched your search
@@ -2,6 +2,8 @@ package repuestos
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
dto_builders "repuestera_back/dto/Builders"
|
||||||
|
dto "repuestera_back/dto/Repuestos"
|
||||||
"repuestera_back/models"
|
"repuestera_back/models"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -12,15 +14,28 @@ import (
|
|||||||
|
|
||||||
|
|
||||||
func CrearRepuesto(c *gin.Context){
|
func CrearRepuesto(c *gin.Context){
|
||||||
var repuesto *models.Repuesto
|
var data dto.CrearRepuestoDto
|
||||||
if err := c.ShouldBindJSON(&repuesto); err != nil {
|
if err := c.ShouldBindJSON(&data); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var marca models.Marca
|
||||||
|
marca = models.Marca{Nombre: data.Marca, ID: data.Marca[:3]}
|
||||||
|
|
||||||
if err := r.ObtenerRepositorios().Repuesto.Crear(repuesto); err != nil {
|
if err := r.ObtenerRepositorios().Marca.CrearFaltante(marca); err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
repuesto := dto_builders.NewRepuestoBuilder().
|
||||||
|
ConDto(data).
|
||||||
|
ConMarca(marca.ID).
|
||||||
|
Build()
|
||||||
|
|
||||||
|
if err := r.ObtenerRepositorios().Repuesto.Crear(&repuesto); err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusCreated, repuesto)
|
c.JSON(http.StatusCreated, repuesto)
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package dto_builders
|
||||||
|
|
||||||
|
import (
|
||||||
|
dto "repuestera_back/dto/Repuestos"
|
||||||
|
"repuestera_back/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RepuestoBuilder struct {
|
||||||
|
dto *dto.CrearRepuestoDto
|
||||||
|
marcaID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRepuestoBuilder() *RepuestoBuilder {
|
||||||
|
return &RepuestoBuilder{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *RepuestoBuilder) ConDto(dto dto.CrearRepuestoDto) *RepuestoBuilder {
|
||||||
|
b.dto = &dto
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *RepuestoBuilder) ConMarca(marcaID string) *RepuestoBuilder {
|
||||||
|
b.marcaID = marcaID
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
func (b *RepuestoBuilder) Build() models.Repuesto {
|
||||||
|
aliases := b.dto.Aliases
|
||||||
|
|
||||||
|
if aliases == nil {
|
||||||
|
aliases = []string{}
|
||||||
|
}
|
||||||
|
return models.Repuesto{
|
||||||
|
Nombre: b.dto.Nombre,
|
||||||
|
Descripcion: b.dto.Descripcion,
|
||||||
|
Precio: b.dto.Precio,
|
||||||
|
StockMinimo: int64(b.dto.StockMinimo),
|
||||||
|
MarcaID: b.marcaID,
|
||||||
|
Aliases: aliases,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type CrearRepuestoDto struct {
|
||||||
|
Nombre string `json:"nombre"`
|
||||||
|
Descripcion string `json:"descripcion"`
|
||||||
|
Marca string `json:"marca"`
|
||||||
|
Aliases []string `json:"aliases"`
|
||||||
|
Precio float64 `json:"precio"`
|
||||||
|
StockMinimo int `json:"stockMinimo"`
|
||||||
|
}
|
||||||
+5
-2
@@ -8,10 +8,11 @@ require (
|
|||||||
github.com/joho/godotenv v1.5.1
|
github.com/joho/godotenv v1.5.1
|
||||||
golang.org/x/crypto v0.31.0
|
golang.org/x/crypto v0.31.0
|
||||||
gorm.io/driver/mysql v1.5.7
|
gorm.io/driver/mysql v1.5.7
|
||||||
gorm.io/gorm v1.25.12
|
gorm.io/gorm v1.30.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/bytedance/sonic v1.11.6 // indirect
|
github.com/bytedance/sonic v1.11.6 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||||
@@ -21,9 +22,10 @@ require (
|
|||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.7.0 // indirect
|
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||||
github.com/goccy/go-json v0.10.2 // indirect
|
github.com/goccy/go-json v0.10.2 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/json-iterator/go v1.1.12 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
@@ -44,4 +46,5 @@ require (
|
|||||||
google.golang.org/protobuf v1.34.1 // indirect
|
google.golang.org/protobuf v1.34.1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
gorm.io/datatypes v1.2.7 // indirect
|
||||||
)
|
)
|
||||||
+10
@@ -1,3 +1,5 @@
|
|||||||
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||||
@@ -26,6 +28,8 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx
|
|||||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||||
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
|
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
|
||||||
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
|
||||||
|
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
|
||||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||||
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
|
||||||
@@ -33,6 +37,8 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI
|
|||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||||
@@ -101,10 +107,14 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gorm.io/datatypes v1.2.7 h1:ww9GAhF1aGXZY3EB3cJPJ7//JiuQo7DlQA7NNlVaTdk=
|
||||||
|
gorm.io/datatypes v1.2.7/go.mod h1:M2iO+6S3hhi4nAyYe444Pcb0dcIiOMJ7QHaUXxyiNZY=
|
||||||
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
|
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
|
||||||
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
|
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
|
||||||
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||||
|
gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs=
|
||||||
|
gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=
|
||||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type Marca struct {
|
type Marca struct {
|
||||||
ID string `gorm:"primaryKey;type:varchar(3)" json:"id"`
|
ID string `gorm:"primaryKey;type:varchar(3)" json:"id"`
|
||||||
Razon string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon"`
|
Nombre string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"nombre"`
|
||||||
Repuestos []Repuesto `gorm:"foreignKey:MarcaID"`
|
Repuestos []Repuesto `gorm:"foreignKey:MarcaID"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt time.Time `json:"updated_at"`
|
||||||
|
|||||||
+12
-11
@@ -3,22 +3,23 @@ package models
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gorm.io/datatypes"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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" binding:"required"`
|
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"`
|
Precio float64 `gorm:"not null;type:decimal(10,2)" json:"precio" binding:"required"`
|
||||||
StockMinimo int64 `gorm:"not null;type:bigint" json:"stockMinimo" 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"`
|
Descripcion string `gorm:"uniqueIndex;not null;type:varchar(70)" json:"razon" binding:"required"`
|
||||||
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId" binding:"required"`
|
MarcaID string `gorm:"type:varchar(3);index" json:"marcaId" binding:"required"`
|
||||||
Aliases []string `gorm:"type:json" json:"aliases,omitempty"`
|
Aliases datatypes.JSONSlice[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 {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ type Repositorios struct {
|
|||||||
Usuario RepositorioUsuario
|
Usuario RepositorioUsuario
|
||||||
Grupo RepositorioGrupo
|
Grupo RepositorioGrupo
|
||||||
Repuesto RepositorioRepuesto
|
Repuesto RepositorioRepuesto
|
||||||
|
Marca RepositorioMarcas
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -26,6 +27,7 @@ func InicializarRepositorios(db *gorm.DB) {
|
|||||||
Usuario: &repositorioUsuario{db: db},
|
Usuario: &repositorioUsuario{db: db},
|
||||||
Grupo: &repositorioGrupo{db: db},
|
Grupo: &repositorioGrupo{db: db},
|
||||||
Repuesto: NuevoRepositorioRepuesto(db),
|
Repuesto: NuevoRepositorioRepuesto(db),
|
||||||
|
Marca: NuevoRepositorioMarcas(db),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package repositorios
|
||||||
|
|
||||||
|
import (
|
||||||
|
"repuestera_back/models"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RepositorioMarcas interface {
|
||||||
|
IRepositorio[*models.Marca]
|
||||||
|
CrearFaltante(marca models.Marca) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type repositorioMarcas struct {
|
||||||
|
*RepositorioBase[*models.Marca]
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NuevoRepositorioMarcas(db *gorm.DB) RepositorioMarcas {
|
||||||
|
baseRepo := NewRepositorioBase[*models.Marca](db)
|
||||||
|
return &repositorioMarcas{
|
||||||
|
RepositorioBase: baseRepo,
|
||||||
|
db: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package routes
|
package routes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"repuestera_back/controllers/repuestos"
|
repuestos "repuestera_back/controllers/repuestos"
|
||||||
"repuestera_back/middleware"
|
"repuestera_back/middleware"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { API_URL } from '$lib/env';
|
||||||
|
import type { CrearRepuestoDto } from '../../../types';
|
||||||
|
import { getToken } from '../token';
|
||||||
|
|
||||||
|
export async function crearRepuesto(repuesto: CrearRepuestoDto) {
|
||||||
|
try {
|
||||||
|
const req = await fetch(`${API_URL}/repuestos/`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: `Bearer ${getToken()}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify(repuesto)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (req.ok) {
|
||||||
|
return 'Ok';
|
||||||
|
}
|
||||||
|
const data = await req.json();
|
||||||
|
return data.error;
|
||||||
|
} catch {
|
||||||
|
return 'no se puso alcanzar el servidor';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,8 @@ import { getToken } from '../token';
|
|||||||
export async function obtenerRepuestos(fetch2?: Function) {
|
export async function obtenerRepuestos(fetch2?: Function) {
|
||||||
const fetchfn = fetch2 ? fetch2 : fetch;
|
const fetchfn = fetch2 ? fetch2 : fetch;
|
||||||
try {
|
try {
|
||||||
const req = await fetchfn(`${API_URL}/repuestos`, {
|
const path = `${API_URL}/repuestos/`;
|
||||||
|
const req = await fetchfn(path, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
|
import { obtenerRepuestos } from '$lib/hooks/repuestos/obtenerRepuestos';
|
||||||
|
|
||||||
export let ssr = false;
|
export let ssr = false;
|
||||||
export async function load({ fetch }) {
|
export async function load({ fetch, depends }) {
|
||||||
|
depends('repuestos:load');
|
||||||
const repuestos = await obtenerRepuestos(fetch);
|
const repuestos = await obtenerRepuestos(fetch);
|
||||||
|
|
||||||
let error = '';
|
let error = '';
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
|
import { Button, Modal, NumberInput, TextInput } from 'carbon-components-svelte';
|
||||||
import { Add } from 'carbon-icons-svelte';
|
import type { CrearRepuestoDto } from '../../types';
|
||||||
|
import { crearRepuesto } from '$lib/hooks/repuestos/crearRepuesto';
|
||||||
|
import { invalidate } from '$app/navigation';
|
||||||
|
|
||||||
let { open = $bindable(), onClose } = $props();
|
let { open = $bindable(), onClose } = $props();
|
||||||
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
let repuesto = $state({
|
let repuesto: CrearRepuestoDto = $state({
|
||||||
nombre: '',
|
nombre: '',
|
||||||
descripcion: '',
|
descripcion: '',
|
||||||
marca: '',
|
marca: '',
|
||||||
aliases: [],
|
aliases: [''],
|
||||||
precio: 0,
|
precio: 0,
|
||||||
stockMinimo: 0
|
stockMinimo: 0
|
||||||
});
|
});
|
||||||
@@ -31,8 +33,21 @@
|
|||||||
|
|
||||||
<form
|
<form
|
||||||
id="crearRepuestoForm"
|
id="crearRepuestoForm"
|
||||||
onsubmit={(e) => {
|
onsubmit={async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
error = '';
|
||||||
|
if (repuesto.descripcion == '') error += 'Falta Descripcion, ';
|
||||||
|
if (repuesto.marca == '') error += 'Falta Marca, ';
|
||||||
|
if (repuesto.precio <= 0) error += 'El precio es 0 o menor, ';
|
||||||
|
if (repuesto.nombre == '') error += 'Falta Nombre';
|
||||||
|
|
||||||
|
if (error == '') {
|
||||||
|
error = await crearRepuesto(repuesto);
|
||||||
|
}
|
||||||
|
if (error == 'Ok') {
|
||||||
|
invalidate('repuestos:load');
|
||||||
|
handleClose();
|
||||||
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Modal
|
<Modal
|
||||||
@@ -62,20 +77,30 @@
|
|||||||
labelText="Stock Minimo (es para los avisos): "
|
labelText="Stock Minimo (es para los avisos): "
|
||||||
bind:value={repuesto.stockMinimo}
|
bind:value={repuesto.stockMinimo}
|
||||||
/>
|
/>
|
||||||
<div class="my-2! flex justify-between">
|
<div class="my-2!">
|
||||||
<h5>Aliases</h5>
|
<h5>Aliases</h5>
|
||||||
<Button
|
<table class="w-full">
|
||||||
size="small"
|
<tbody>
|
||||||
icon={Add}
|
{#each repuesto.aliases as _, index}
|
||||||
onclick={() => {
|
<tr>
|
||||||
repuesto.aliases.push('');
|
<td>
|
||||||
}}>Añadir Alias</Button
|
<TextInput
|
||||||
>
|
maxlength={70}
|
||||||
</div>
|
name={`alias-${index}`}
|
||||||
<div class="flex flex-col gap-2">
|
bind:value={repuesto.aliases[index]}
|
||||||
{#each repuesto.aliases as _, index}
|
on:input={() => {
|
||||||
<TextInput name={`alias-${index}`} bind:value={repuesto.aliases[index]} />
|
if (index === repuesto.aliases.length - 1 && repuesto.aliases[index] !== '') {
|
||||||
{/each}
|
repuesto.aliases.push('');
|
||||||
|
} else if (repuesto.aliases[index] == '') {
|
||||||
|
repuesto.aliases.pop();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{/each}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
</Modal>
|
</Modal>
|
||||||
</form>
|
</form>
|
||||||
Vendored
+9
@@ -4,6 +4,15 @@ export interface Usuario {
|
|||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface CrearRepuestoDto {
|
||||||
|
nombre: string;
|
||||||
|
descripcion: string;
|
||||||
|
marca: string;
|
||||||
|
aliases: string[];
|
||||||
|
precio: number;
|
||||||
|
stockMinimo: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Grupo {
|
export interface Grupo {
|
||||||
id: number;
|
id: number;
|
||||||
nombre: string;
|
nombre: string;
|
||||||
|
|||||||
Reference in new issue
Block a user