Private
Public Access
version pre testeo de descuentos
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
a9ec514687
commit
ea009e276f
25 files changed
+1100
-13
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package descuento
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
dto "repuestera_back/dto/Descuentos"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func CrearDescuento(c *gin.Context) {
|
||||
var data dto.DescuentoDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
descuento, err := dto_directors.NewDescuentoDirector().Crear(data)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios(c).Descuento
|
||||
if _, err := repo.ObtenerPorNombre(descuento.Nombre); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "ya existe un descuento con ese nombre"})
|
||||
return
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.Crear(*descuento, c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
creado, err := repo.ObtenerPorNombre(descuento.Nombre)
|
||||
if err != nil {
|
||||
c.Status(http.StatusCreated)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, creado)
|
||||
}
|
||||
Reference in new issue
Block a user