Private
Public Access
checkeada parte del back sobre el abm de almacenes
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
3587a8dbc6
commit
12276acbd8
10 files changed
+294
-15
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package almacen
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
dto "repuestera_back/dto/Almacenes"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ActualizarAlmacen(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
did := uint(id)
|
||||
|
||||
repo := r.ObtenerRepositorios(c).Almacen
|
||||
almacen, err := repo.ObtenerPorID(did)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Almacén no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
var data dto.AlmacenDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
input, err := dto_directors.NewAlmacenDirector().Actualizar(data)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if existing, _ := repo.BuscarPorCodigo(input.Codigo); existing != nil && existing.ID != almacen.ID {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "ya existe un almacén con ese código"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := repo.ActualizarCompleto(almacen, *input, c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
actualizado, err := repo.ObtenerPorID(did)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, almacen)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, actualizado)
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package almacen
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func AltaAlmacen(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios(c).Almacen
|
||||
almacen, err := repo.ObtenerPorID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Almacén no encontrado"})
|
||||
return
|
||||
}
|
||||
|
||||
if !almacen.DeletedAt.Valid {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "El almacén no está eliminado"})
|
||||
return
|
||||
}
|
||||
|
||||
almacen.DeletedAt = gorm.DeletedAt{}
|
||||
if err := repo.Alta(almacen, *almacen, c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
@@ -4,23 +4,29 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
dto "repuestera_back/dto/Almacenes"
|
||||
dto_directors "repuestera_back/dto/Builders/Directors"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
dto "repuestera_back/dto/Almacenes"
|
||||
"repuestera_back/models"
|
||||
r "repuestera_back/repositorios"
|
||||
)
|
||||
|
||||
func CrearAlmacen(c *gin.Context) {
|
||||
var data dto.CrearAlmacenDto
|
||||
var data dto.AlmacenDto
|
||||
if err := c.ShouldBindJSON(&data); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
almacen, err := dto_directors.NewAlmacenDirector().Crear(data)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
repo := r.ObtenerRepositorios(c).Almacen
|
||||
if _, err := repo.BuscarPorCodigo(data.Codigo); err == nil {
|
||||
if _, err := repo.BuscarPorCodigo(almacen.Codigo); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "ya existe un almacén con ese código"})
|
||||
return
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -28,17 +34,16 @@ func CrearAlmacen(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
almacen := models.Almacen{Codigo: data.Codigo, Ubicacion: data.Ubicacion}
|
||||
if err := repo.Crear(almacen, c.GetUint("userID")); err != nil {
|
||||
if err := repo.Crear(*almacen, c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
creado, err := repo.BuscarPorCodigo(data.Codigo)
|
||||
creado, err := repo.BuscarPorCodigo(almacen.Codigo)
|
||||
if err != nil {
|
||||
c.Status(http.StatusCreated)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, creado)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package almacen
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func EliminarAlmacen(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "ID inválido"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := r.ObtenerRepositorios(c).Almacen.Baja(uint(id), c.GetUint("userID")); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package almacen
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ObtenerTodosLosAlmacenes(c *gin.Context) {
|
||||
almacenes, err := r.ObtenerRepositorios(c).Almacen.ObtenerTodos()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, almacenes)
|
||||
}
|
||||
Reference in new issue
Block a user