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 account
import (
"net/http"
"nos-comptes/handler"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/storage/model"
"nos-comptes/internal/storage/validators"
"nos-comptes/internal/user"
@@ -16,7 +14,7 @@ type Context struct {
service *Service
db *Database
userService *user.Service
*handler.Context
validator *Validator
}
func (c *Context) GetAllAccountOfUser(gc *gin.Context) {
@@ -92,9 +90,6 @@ func (c *Context) GetSpecificAccountOfUser(gc *gin.Context) {
utils.JSON(gc.Writer, http.StatusOK, account)
}
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
database := NewDatabase(db)
service := NewService(database)
userService := user.NewService(user.NewDatabase(db))
return &Context{service: service, db: database, userService: userService, Context: ctx}
func NewHandler(validator *Validator, database *Database, service *Service, userService *user.Service) *Context {
return &Context{service: service, db: database, userService: userService, validator: validator}
}

34
internal/account/setup.go Normal file
View File

@@ -0,0 +1,34 @@
package account
import (
"github.com/gin-gonic/gin"
"net/http"
"nos-comptes/internal/ginserver"
"nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/user"
"nos-comptes/internal/utils"
)
const ServiceInjectorKey = "ACCOUNT_SERVICE"
func Setup(injector *utils.Injector) {
pg := utils.Get[*postgresql.DatabasePostgreSQL](injector, postgresql.DatabaseKey)
userService := utils.Get[*user.Service](injector, user.ServiceInjectorKey)
database := NewDatabase(pg)
service := NewService(database)
validator := NewValidator(service)
handler := NewHandler(validator, database, service, userService)
securedRoute := utils.Get[*gin.RouterGroup](injector, ginserver.SecuredRouterInjectorKey)
securedUserRoute := securedRoute.Group("/:userId")
//account route
securedUserRoute.Handle(http.MethodGet, "/accounts", handler.GetAllAccountOfUser)
securedUserRoute.Handle(http.MethodPost, "/accounts", handler.CreateAccountOfUser)
securedValidAccount := securedUserRoute.Group("/accounts/:accountId")
securedValidAccount.Use(validator.HasValidAccountId)
securedValidAccount.Use(validator.AccountExists)
securedValidAccount.Handle(http.MethodDelete, "", handler.DeleteAccountOfUser)
securedValidAccount.Handle(http.MethodGet, "", handler.GetSpecificAccountOfUser)
injector.Set(ServiceInjectorKey, service)
}

View File

@@ -0,0 +1,44 @@
package account
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 {
accountService *Service
Validator validator.Validate
}
func NewValidator(service *Service) *Validator {
return &Validator{accountService: service}
}
func (v Validator) HasValidAccountId(gc *gin.Context) {
accountId := gc.Param("accountId")
err := v.Validator.VarCtx(gc, accountId, "uuid4")
if err != nil {
utils.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
return
}
}
func (v Validator) AccountExists(gc *gin.Context) {
userId := gc.Param("userId")
accountId := gc.Param("accountId")
_, err := v.accountService.GetASpecificAccountForUser(userId, accountId)
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
}
}