big refacto
This commit is contained in:
@@ -2,10 +2,9 @@ package expense
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
"net/http"
|
||||
"nos-comptes/handler"
|
||||
"nos-comptes/internal/account"
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
"nos-comptes/internal/storage/model"
|
||||
"nos-comptes/internal/storage/validators"
|
||||
"nos-comptes/internal/utils"
|
||||
@@ -18,7 +17,7 @@ type Context struct {
|
||||
service *Service
|
||||
db *Database
|
||||
accountService *account.Service
|
||||
*handler.Context
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func (c *Context) ImportExpenseFromCSV(gc *gin.Context) {
|
||||
@@ -124,9 +123,6 @@ func (c *Context) GetAnExpenses(context *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func NewHandler(ctx *handler.Context, db *postgresql.DatabasePostgreSQL) *Context {
|
||||
database := NewDatabase(db)
|
||||
service := NewService(database)
|
||||
accountService := account.NewService(account.NewDatabase(db))
|
||||
return &Context{service: service, db: database, accountService: accountService, Context: ctx}
|
||||
func NewHandler(validator *validator.Validate, database *Database, service *Service, accountService *account.Service) *Context {
|
||||
return &Context{service: service, db: database, accountService: accountService, validator: validator}
|
||||
}
|
||||
|
||||
36
internal/expense/setup.go
Normal file
36
internal/expense/setup.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package expense
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"gopkg.in/go-playground/validator.v9"
|
||||
"net/http"
|
||||
"nos-comptes/internal/account"
|
||||
"nos-comptes/internal/ginserver"
|
||||
"nos-comptes/internal/storage/dao/postgresql"
|
||||
"nos-comptes/internal/utils"
|
||||
validatorInternal "nos-comptes/internal/utils/validator"
|
||||
)
|
||||
|
||||
const ServiceInjectorKey = "EXPENSE_SERVICE"
|
||||
|
||||
func Setup(injector *utils.Injector) {
|
||||
pg := utils.Get[*postgresql.DatabasePostgreSQL](injector, postgresql.DatabaseKey)
|
||||
validate := utils.Get[*validator.Validate](injector, validatorInternal.ValidatorInjectorKey)
|
||||
accountService := utils.Get[*account.Service](injector, account.ServiceInjectorKey)
|
||||
database := NewDatabase(pg)
|
||||
service := NewService(database)
|
||||
handler := NewHandler(validate, database, service, accountService)
|
||||
securedRoute := utils.Get[*gin.RouterGroup](injector, ginserver.SecuredRouterInjectorKey)
|
||||
securedUserRoute := securedRoute.Group("/:userId")
|
||||
securedValidAccount := securedUserRoute.Group("/accounts/:accountId")
|
||||
|
||||
injector.Set(ServiceInjectorKey, service)
|
||||
|
||||
securedValidAccount.Handle(http.MethodPost, "/expenses", handler.CreateAnExpense)
|
||||
securedValidAccount.Handle(http.MethodGet, "/expenses", handler.GetAllExpenses)
|
||||
|
||||
securedExistingExpenses := securedValidAccount.Group("/expenses/:expenseId")
|
||||
securedExistingExpenses.Handle(http.MethodGet, "", handler.GetAnExpenses)
|
||||
securedExistingExpenses.Handle(http.MethodDelete, "", handler.DeleteExpense)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user