Private
Public Access
refactor: ahora los repos se acceden con un singleton en vez de inyecion
de dependencias
This commit is contained in:
1 parent
52fc779abd
commit
141715ee0a
22 files changed
+244
-211
No files matched your search
@@ -4,21 +4,13 @@ import (
|
||||
"net/http"
|
||||
|
||||
"repuestera_back/dto"
|
||||
repositorios "repuestera_back/repositorios"
|
||||
r "repuestera_back/repositorios"
|
||||
"repuestera_back/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var usuarioRepo repositorios.RepositorioUsuario
|
||||
var grupoRepo repositorios.RepositorioGrupo
|
||||
|
||||
func InitUsuarioRepository(repo repositorios.RepositorioUsuario, repog repositorios.RepositorioGrupo) {
|
||||
usuarioRepo = repo
|
||||
grupoRepo = repog
|
||||
}
|
||||
|
||||
func CambiarContraseña(c *gin.Context) {
|
||||
var req dto.CambiarContraseñaRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -46,7 +38,7 @@ func CambiarContraseña(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := usuarioRepo.ObtenerPorID(targetID)
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(targetID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "usuario no encontrado"})
|
||||
return
|
||||
@@ -69,7 +61,7 @@ func CambiarContraseña(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := usuarioRepo.ActualizarContraseña(user.ID, string(hashedPassword)); err != nil {
|
||||
if err := r.ObtenerRepositorios().Usuario.ActualizarContraseña(user.ID, string(hashedPassword)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error al actualizar la contraseña"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -22,7 +23,7 @@ func Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
email := strings.ToLower(strings.TrimSpace(req.Email))
|
||||
user, err := usuarioRepo.ObtenerPorEmail(email)
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorEmail(email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Mail Invalido"})
|
||||
return
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -50,7 +51,7 @@ func Me(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
userID, _ := strconv.ParseUint(sub, 10, 32)
|
||||
user, err := usuarioRepo.ObtenerPorID(uint(userID))
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(uint(userID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"repuestera_back/dto"
|
||||
"repuestera_back/jwtutils"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
@@ -42,7 +43,7 @@ func Refresh(c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid token claims"})
|
||||
return
|
||||
}
|
||||
user, err := usuarioRepo.ObtenerPorID(uint(sub))
|
||||
user, err := r.ObtenerRepositorios().Usuario.ObtenerPorID(uint(sub))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"repuestera_back/dto"
|
||||
models_seguridad "repuestera_back/models/seguridad"
|
||||
r "repuestera_back/repositorios"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -20,7 +21,7 @@ func Register(c *gin.Context) {
|
||||
|
||||
email := strings.ToLower(strings.TrimSpace(req.Email))
|
||||
|
||||
if _, err := usuarioRepo.ObtenerPorEmail(email); err == nil {
|
||||
if _, err := r.ObtenerRepositorios().Usuario.ObtenerPorEmail(email); err == nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "El email ya esta registrado"})
|
||||
return
|
||||
}
|
||||
@@ -39,9 +40,9 @@ func Register(c *gin.Context) {
|
||||
|
||||
//checkeo para ver si hay usuarios añadidos
|
||||
var cantUsuarios int64
|
||||
usuarioRepo.CantidadUsuarios(&cantUsuarios)
|
||||
r.ObtenerRepositorios().Usuario.CantidadUsuarios(&cantUsuarios)
|
||||
if (cantUsuarios < 1){
|
||||
admin, err := grupoRepo.ObtenerPorID(1)
|
||||
admin, err := r.ObtenerRepositorios().Grupo.ObtenerPorID(1)
|
||||
if err != nil{
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Hay un problema con la base de datos: Faltan Grupos?"})
|
||||
return
|
||||
@@ -49,7 +50,7 @@ func Register(c *gin.Context) {
|
||||
user.Grupos = append(user.Grupos, *admin)
|
||||
}
|
||||
|
||||
if err := usuarioRepo.Crear(&user); err != nil {
|
||||
if err := r.ObtenerRepositorios().Usuario.Crear(&user); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user