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 {
|
||||
type Req struct {
|
||||
ClienteID uint `json:"cliente_id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Color string `json:"color"`
|
||||
Estado string `json:"estado"`
|
||||
ClienteID interface{} `json:"cliente_id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Color string `json:"color"`
|
||||
Estado string `json:"estado"`
|
||||
}
|
||||
var req Req
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
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"})
|
||||
}
|
||||
if req.Slug == "" {
|
||||
@@ -91,7 +92,7 @@ func CreateProyecto(c *fiber.Ctx) error {
|
||||
req.Estado = "activo"
|
||||
}
|
||||
p := &models.Proyecto{
|
||||
ClienteID: req.ClienteID,
|
||||
ClienteID: clienteID,
|
||||
Nombre: req.Nombre,
|
||||
Slug: req.Slug,
|
||||
Descripcion: req.Descripcion,
|
||||
@@ -110,20 +111,21 @@ func UpdateProyecto(c *fiber.Ctx) error {
|
||||
return c.Status(400).JSON(fiber.Map{"error": "ID inválido"})
|
||||
}
|
||||
type Req struct {
|
||||
ClienteID uint `json:"cliente_id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Color string `json:"color"`
|
||||
Estado string `json:"estado"`
|
||||
Progreso int `json:"progreso"`
|
||||
ClienteID interface{} `json:"cliente_id"`
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Descripcion string `json:"descripcion"`
|
||||
Color string `json:"color"`
|
||||
Estado string `json:"estado"`
|
||||
Progreso int `json:"progreso"`
|
||||
}
|
||||
var req Req
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
clienteID := parseUintField(req.ClienteID)
|
||||
p := &models.Proyecto{
|
||||
ClienteID: req.ClienteID,
|
||||
ClienteID: clienteID,
|
||||
Nombre: req.Nombre,
|
||||
Slug: req.Slug,
|
||||
Descripcion: req.Descripcion,
|
||||
@@ -508,6 +510,18 @@ func slugify(s string) 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 {
|
||||
base := filepath.Base(name)
|
||||
ext := filepath.Ext(base)
|
||||
|
||||
Reference in New Issue
Block a user