Update proyecto_controller.go
This commit is contained in:
@@ -67,18 +67,19 @@ func LoadProyectos(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func CreateProyecto(c *fiber.Ctx) error {
|
func CreateProyecto(c *fiber.Ctx) error {
|
||||||
type Req struct {
|
type Req struct {
|
||||||
ClienteID uint `json:"cliente_id"`
|
ClienteID interface{} `json:"cliente_id"`
|
||||||
Nombre string `json:"nombre"`
|
Nombre string `json:"nombre"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Descripcion string `json:"descripcion"`
|
Descripcion string `json:"descripcion"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
Estado string `json:"estado"`
|
Estado string `json:"estado"`
|
||||||
}
|
}
|
||||||
var req Req
|
var req Req
|
||||||
if err := c.BodyParser(&req); err != nil {
|
if err := c.BodyParser(&req); err != nil {
|
||||||
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
if req.ClienteID == 0 || strings.TrimSpace(req.Nombre) == "" {
|
clienteID := parseUintField(req.ClienteID)
|
||||||
|
if clienteID == 0 || strings.TrimSpace(req.Nombre) == "" {
|
||||||
return c.Status(400).JSON(fiber.Map{"error": "cliente y nombre son requeridos"})
|
return c.Status(400).JSON(fiber.Map{"error": "cliente y nombre son requeridos"})
|
||||||
}
|
}
|
||||||
if req.Slug == "" {
|
if req.Slug == "" {
|
||||||
@@ -91,7 +92,7 @@ func CreateProyecto(c *fiber.Ctx) error {
|
|||||||
req.Estado = "activo"
|
req.Estado = "activo"
|
||||||
}
|
}
|
||||||
p := &models.Proyecto{
|
p := &models.Proyecto{
|
||||||
ClienteID: req.ClienteID,
|
ClienteID: clienteID,
|
||||||
Nombre: req.Nombre,
|
Nombre: req.Nombre,
|
||||||
Slug: req.Slug,
|
Slug: req.Slug,
|
||||||
Descripcion: req.Descripcion,
|
Descripcion: req.Descripcion,
|
||||||
@@ -110,20 +111,21 @@ func UpdateProyecto(c *fiber.Ctx) error {
|
|||||||
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
||||||
}
|
}
|
||||||
type Req struct {
|
type Req struct {
|
||||||
ClienteID uint `json:"cliente_id"`
|
ClienteID interface{} `json:"cliente_id"`
|
||||||
Nombre string `json:"nombre"`
|
Nombre string `json:"nombre"`
|
||||||
Slug string `json:"slug"`
|
Slug string `json:"slug"`
|
||||||
Descripcion string `json:"descripcion"`
|
Descripcion string `json:"descripcion"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
Estado string `json:"estado"`
|
Estado string `json:"estado"`
|
||||||
Progreso int `json:"progreso"`
|
Progreso int `json:"progreso"`
|
||||||
}
|
}
|
||||||
var req Req
|
var req Req
|
||||||
if err := c.BodyParser(&req); err != nil {
|
if err := c.BodyParser(&req); err != nil {
|
||||||
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
||||||
}
|
}
|
||||||
|
clienteID := parseUintField(req.ClienteID)
|
||||||
p := &models.Proyecto{
|
p := &models.Proyecto{
|
||||||
ClienteID: req.ClienteID,
|
ClienteID: clienteID,
|
||||||
Nombre: req.Nombre,
|
Nombre: req.Nombre,
|
||||||
Slug: req.Slug,
|
Slug: req.Slug,
|
||||||
Descripcion: req.Descripcion,
|
Descripcion: req.Descripcion,
|
||||||
@@ -508,6 +510,18 @@ func slugify(s string) string {
|
|||||||
return strings.Trim(b.String(), "-")
|
return strings.Trim(b.String(), "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseUintField convierte interface{} (string o float64 del JSON) a uint de forma segura.
|
||||||
|
func parseUintField(v interface{}) uint {
|
||||||
|
switch val := v.(type) {
|
||||||
|
case float64:
|
||||||
|
return uint(val)
|
||||||
|
case string:
|
||||||
|
n, _ := strconv.ParseUint(strings.TrimSpace(val), 10, 64)
|
||||||
|
return uint(n)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func safeFilenameProyecto(name string) string {
|
func safeFilenameProyecto(name string) string {
|
||||||
base := filepath.Base(name)
|
base := filepath.Base(name)
|
||||||
ext := filepath.Ext(base)
|
ext := filepath.Ext(base)
|
||||||
|
|||||||
Reference in New Issue
Block a user