Private
Public Access
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package cliente
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"repuestera_back/models"
|
|
r "repuestera_back/repositorios"
|
|
|
|
dto "repuestera_back/dto/Clientes"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func ActualizarCliente(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
|
|
}
|
|
if id < 1 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "ID no puede ser menor que 1"})
|
|
return
|
|
}
|
|
|
|
repo := r.ObtenerRepositorios(c).Cliente
|
|
cliente, err := repo.ObtenerPorID(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Cliente no encontrado"})
|
|
return
|
|
}
|
|
|
|
var data dto.CrearClienteDto
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if existing, _ := repo.BuscarPorCuit(data.Cuit); existing != nil && existing.ID != cliente.ID {
|
|
c.JSON(http.StatusConflict, gin.H{"error": "ya existe un cliente con ese CUIT"})
|
|
return
|
|
}
|
|
|
|
input := models.Cliente{
|
|
Nombre: data.Nombre,
|
|
Cuit: data.Cuit,
|
|
Email: data.Email,
|
|
}
|
|
uid := c.GetUint("userID")
|
|
if err := repo.Actualizar(cliente, input, uid); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
actualizado, err := repo.ObtenerPorID(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, cliente)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, actualizado)
|
|
}
|