Files
Repuestera/back/controllers/clientes/CrearCliente.go
T

49 lines
1.1 KiB
Go

package cliente
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
dto "repuestera_back/dto/Clientes"
"repuestera_back/models"
r "repuestera_back/repositorios"
)
func CrearCliente(c *gin.Context) {
var data dto.CrearClienteDto
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
repo := r.ObtenerRepositorios(c).Cliente
if _, err := repo.BuscarPorCuit(data.Cuit); err == nil {
c.JSON(http.StatusConflict, gin.H{"error": "ya existe un cliente con ese CUIT"})
return
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
cliente := models.Cliente{
Nombre: data.Nombre,
Cuit: data.Cuit,
Email: data.Email,
}
if err := repo.Crear(cliente, c.GetUint("userID")); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
creado, err := repo.BuscarPorCuit(data.Cuit)
if err != nil {
c.Status(http.StatusCreated)
return
}
c.JSON(http.StatusCreated, creado)
}