big refacto
This commit is contained in:
44
internal/account/validator.go
Normal file
44
internal/account/validator.go
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user