add b asic endpoint

This commit is contained in:
2022-05-20 00:52:07 +02:00
parent 19a642b4d4
commit c37d824191
2 changed files with 43 additions and 10 deletions

View File

@@ -5,7 +5,7 @@ import (
"nos-comptes/handler" "nos-comptes/handler"
"nos-comptes/internal/account" "nos-comptes/internal/account"
"nos-comptes/internal/expense" "nos-comptes/internal/expense"
sharedaccount "nos-comptes/internal/shared-account" "nos-comptes/internal/jointaccount"
"nos-comptes/internal/storage/dao/postgresql" "nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/user" "nos-comptes/internal/user"
"nos-comptes/middleware" "nos-comptes/middleware"
@@ -38,7 +38,7 @@ func NewRouter(config *handler.Config) *gin.Engine {
hc := handler.NewContext() hc := handler.NewContext()
uh := user.NewHandler(hc, db) uh := user.NewHandler(hc, db)
ah := account.NewHandler(hc, db) ah := account.NewHandler(hc, db)
sah := sharedaccount.NewHandler(hc, db) jah := jointaccount.NewHandler(hc, db)
eh := expense.NewHandler(hc, db) eh := expense.NewHandler(hc, db)
mv := middleware.NewValidator(hc, db) mv := middleware.NewValidator(hc, db)
public := router.Group("/") public := router.Group("/")
@@ -71,11 +71,17 @@ func NewRouter(config *handler.Config) *gin.Engine {
securedExistingExpenses := securedValidAccount.Group("/expenses/:expenseId") securedExistingExpenses := securedValidAccount.Group("/expenses/:expenseId")
securedExistingExpenses.Handle(http.MethodGet, "", eh.GetAnExpenses) securedExistingExpenses.Handle(http.MethodGet, "", eh.GetAnExpenses)
securedExistingExpenses.Handle(http.MethodDelete, "", eh.DeleteExpense) securedExistingExpenses.Handle(http.MethodDelete, "", eh.DeleteExpense)
//shared route
securedUserRoute.Handle(http.MethodPost, "/:userId/sharedaccounts/:accountId", sah.ShareAnAccount)
securedUserRoute.Handle(http.MethodDelete, "/:userId/sharedaccounts/:accountId", sah.DeleteSharedAccount)
securedUserRoute.Handle(http.MethodGet, "/:userId/sharedaccounts", sah.GetAllSharedAccountOfUser)
securedUserRoute.Handle(http.MethodGet, "/:userId/sharedaccounts/:sharedAccountId", sah.GetSpecificSharedAccountOfUser)
//account route
securedMatchingToken.Handle(http.MethodGet, "/jointaccounts", jah.GetAllJointaccountOfUser)
securedMatchingToken.Handle(http.MethodPost, "/jointaccounts", jah.CreateJointaccountOfUser)
securedValidJointAccount := securedMatchingToken.Group("/jointaccounts/:jointaccountId")
securedValidJointAccount.Use(mv.HasValidJointAccountId)
securedValidJointAccount.Use(mv.JointAccountExists)
securedValidJointAccount.Handle(http.MethodDelete, "", jah.DeleteJointaccountOfUser)
securedValidJointAccount.Handle(http.MethodGet, "", jah.GetSpecificJointaccountOfUser)
securedValidJointAccount.Handle(http.MethodPost, "/expenses", eh.CreateAnExpense)
securedValidJointAccount.Handle(http.MethodGet, "/expenses", eh.GetAllExpenses)
return router return router
} }

View File

@@ -3,6 +3,7 @@ package middleware
import ( import (
"nos-comptes/handler" "nos-comptes/handler"
"nos-comptes/internal/account" "nos-comptes/internal/account"
"nos-comptes/internal/jointaccount"
"nos-comptes/internal/storage/dao/postgresql" "nos-comptes/internal/storage/dao/postgresql"
"nos-comptes/internal/storage/model" "nos-comptes/internal/storage/model"
"nos-comptes/internal/storage/validators" "nos-comptes/internal/storage/validators"
@@ -13,13 +14,14 @@ import (
) )
type Validator struct { type Validator struct {
accountService *account.Service jointaccountService *jointaccount.Service
userService *user.Service accountService *account.Service
userService *user.Service
*handler.Context *handler.Context
} }
func NewValidator(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Validator { func NewValidator(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Validator {
return &Validator{accountService: account.NewService(account.NewDatabase(db)), userService: user.NewService(user.NewDatabase(db)), Context: ctx} return &Validator{accountService: account.NewService(account.NewDatabase(db)), userService: user.NewService(user.NewDatabase(db)), jointaccountService: jointaccount.NewService(jointaccount.NewDatabase(db)), Context: ctx}
} }
func (v Validator) HasValidUserId(gc *gin.Context) { func (v Validator) HasValidUserId(gc *gin.Context) {
userId := gc.Param("userId") userId := gc.Param("userId")
@@ -108,3 +110,28 @@ func (v Validator) AccountExists(gc *gin.Context) {
return return
} }
} }
func (v Validator) HasValidJointAccountId(gc *gin.Context) {
accountId := gc.Param("jointaccountId")
err := v.Validator.VarCtx(gc, accountId, "uuid4")
if err != nil {
utils.JSONError(gc.Writer, validators.NewDataValidationAPIError(err))
return
}
}
func (v Validator) JointAccountExists(gc *gin.Context) {
userId := gc.Param("userId")
accountId := gc.Param("jointaccountId")
_, err := v.jointaccountService.GetASpecificJointaccountForUser(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
}
}