big refacto

This commit is contained in:
2023-10-03 00:40:01 +02:00
parent aa722718f7
commit 932f423faf
35 changed files with 354 additions and 934 deletions

View File

@@ -2,8 +2,6 @@ package user
import (
"net/http"
"nos-comptes/handler"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/storage/model"
"nos-comptes/internal/utils"
"strings"
@@ -13,15 +11,9 @@ import (
)
type Context struct {
service *Service
db *Database
*handler.Context
}
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
database := NewDatabase(db)
service := NewService(database)
return &Context{service: service, db: database, Context: ctx}
service *Service
db *Database
validator *Validator
}
func (uc *Context) GetAllUsers(c *gin.Context) {
@@ -130,3 +122,7 @@ func (hc *Context) GetUser(c *gin.Context) {
user, _ := hc.service.GetUserById(userID)
utils.JSON(c.Writer, http.StatusOK, user)
}
func NewHandler(validator *Validator, database *Database, service *Service) *Context {
return &Context{service: service, db: database, validator: validator}
}

30
internal/user/setup.go Normal file
View File

@@ -0,0 +1,30 @@
package user
import (
"github.com/gin-gonic/gin"
"net/http"
"nos-comptes/internal/ginserver"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/utils"
)
const ServiceInjectorKey = "USER_SERVICE"
func Setup(injector *utils.Injector) {
pg := utils.Get[*postgresql.DatabasePostgreSQL](injector, postgresql.DatabaseKey)
database := NewDatabase(pg)
service := NewService(database)
validator := NewValidator(service)
handler := NewHandler(validator, database, service)
securedRoute := utils.Get[*gin.RouterGroup](injector, ginserver.SecuredRouterInjectorKey)
//TODO add secure auth
securedRoute.Handle(http.MethodGet, "/:userId", handler.GetUser)
securedUserRoute := securedRoute.Group("/:userId")
securedUserRoute.Use(validator.HasValidUserId)
securedUserRoute.Use(validator.UserdIdMatchOAuthToken)
injector.Set(ServiceInjectorKey, service)
}
func SetupRoute(injector *utils.Injector) {
}

View File

@@ -0,0 +1,82 @@
package user
import (
"gopkg.in/go-playground/validator.v9"
"nos-comptes/internal/storage/model"
"nos-comptes/internal/storage/validators"
"nos-comptes/internal/utils"
"github.com/gin-gonic/gin"
)
type Validator struct {
userService *Service
Validator validator.Validate
}
func NewValidator(service *Service) *Validator {
return &Validator{userService: service}
}
func (v Validator) HasValidUserId(gc *gin.Context) {
userId := gc.Param("userId")
err := v.Validator.VarCtx(gc, userId, "uuid4")
if err != nil {
utils.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
return
}
}
func (v Validator) UserExists(gc *gin.Context) {
userId := gc.Param("userId")
user, err := v.userService.GetUserById(userId)
if e, ok := err.(*model.APIError); ok {
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
if user == nil {
utils.JSONErrorWithMessage(gc.Writer, model.ErrNotFound, "User not found")
return
}
}
func (v Validator) UserdIdMatchOAuthToken(gc *gin.Context) {
userId := gc.Param("userId")
usrParam, err := v.userService.GetUserById(userId)
if e, ok := err.(*model.APIError); ok {
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
googleUserId, exists := gc.Get("googleUserId")
if exists == false {
utils.GetLoggerFromCtx(gc).Error("error while getting google user id")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
usr, err := v.userService.GetUserFromGoogleID(googleUserId.(string))
if e, ok := err.(*model.APIError); ok {
utils.GetLogger().Info(err)
utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUserFromGoogleID: get user from google user id")
utils.JSONErrorWithMessage(gc.Writer, *e, e.Description)
return
} else if err != nil {
utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user from google user id")
utils.JSONError(gc.Writer, model.ErrInternalServer)
return
}
if usr == nil || usr.ID != usrParam.ID {
utils.GetLoggerFromCtx(gc).WithError(err).Error("User in path doesn't match authenticated user")
utils.JSONError(gc.Writer, model.ErrBadRequestFormat)
return
}
}