Private
Public Access
refactor: separados handlers empresaaaaaa
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
4e7992ba2b
commit
ff21aa4264
5 files changed
+239
-204
No files matched your search
@@ -0,0 +1,71 @@
|
||||
package plataforma
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ActualizarEmpresa(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Nombre string `json:"nombre"`
|
||||
Slug string `json:"slug"`
|
||||
Cuit *string `json:"cuit"`
|
||||
Activa *bool `json:"activa"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios()
|
||||
empresa, err := repo.Empresa.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Empresa no encontrada"})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.Nombre) != "" {
|
||||
empresa.Nombre = strings.TrimSpace(req.Nombre)
|
||||
}
|
||||
if strings.TrimSpace(req.Slug) != "" {
|
||||
empresa.Slug = strings.TrimSpace(req.Slug)
|
||||
}
|
||||
if req.Cuit != nil {
|
||||
cuit := strings.TrimSpace(*req.Cuit)
|
||||
if cuit == "" {
|
||||
empresa.Cuit = nil
|
||||
} else {
|
||||
empresa.Cuit = &cuit
|
||||
}
|
||||
}
|
||||
if req.Activa != nil {
|
||||
empresa.Activa = *req.Activa
|
||||
}
|
||||
|
||||
if err := repo.Empresa.Guardar(empresa); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
miembros, _ := repo.Empresa.ContarMiembros(empresa.ID)
|
||||
owners, _ := repo.Empresa.ContarOwners(empresa.ID)
|
||||
|
||||
respuesta, err := dto_directors.NewEmpresaAdminDirector().DesdeEmpresa(empresa, miembros, owners)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, respuesta)
|
||||
}
|
||||
Reference in new issue
Block a user