mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
add DI
This commit is contained in:
@@ -4,57 +4,65 @@ import (
|
||||
"wallet-api/pkg/controllers"
|
||||
"wallet-api/pkg/middleware"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
"wallet-api/pkg/utl/configs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
/*
|
||||
Routes
|
||||
|
||||
Initializes web api controllers and its corresponding routes.
|
||||
|
||||
Args:
|
||||
*gin.Engine: Gin Engine
|
||||
*pg.DB: Postgres database client
|
||||
*/
|
||||
func Routes(s *gin.Engine, db *pg.DB) {
|
||||
c := dig.New()
|
||||
ver := s.Group(configs.Prefix)
|
||||
|
||||
api := ver.Group("api")
|
||||
auth := ver.Group("auth")
|
||||
wallet := ver.Group("wallet", middleware.Auth)
|
||||
walletHeader := ver.Group("wallet/wallet-header", middleware.Auth)
|
||||
transaction := ver.Group("transaction", middleware.Auth)
|
||||
transactionType := ver.Group("transaction-type", middleware.Auth)
|
||||
subscription := ver.Group("subscription", middleware.Auth)
|
||||
subscriptionType := ver.Group("subscription-type", middleware.Auth)
|
||||
transactionStatus := ver.Group("transaction-status", middleware.Auth)
|
||||
|
||||
routeGroups := &common.RouteGroups{
|
||||
Api: ver.Group("api"),
|
||||
Auth: ver.Group("auth"),
|
||||
Wallet: ver.Group("wallet", middleware.Auth),
|
||||
WalletHeader: ver.Group("wallet/wallet-header", middleware.Auth),
|
||||
Transaction: ver.Group("transaction", middleware.Auth),
|
||||
TransactionType: ver.Group("transaction-type", middleware.Auth),
|
||||
Subscription: ver.Group("subscription", middleware.Auth),
|
||||
SubscriptionType: ver.Group("subscription-type", middleware.Auth),
|
||||
TransactionStatus: ver.Group("transaction-status", middleware.Auth),
|
||||
}
|
||||
|
||||
s.NoRoute(func(c *gin.Context) {
|
||||
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
|
||||
})
|
||||
|
||||
|
||||
apiService := services.ApiService{Db: db}
|
||||
usersService := services.UsersService{Db: db}
|
||||
walletService := services.WalletService{Db: db}
|
||||
transactionService := services.TransactionService{Db: db}
|
||||
transactionTypeService := services.TransactionTypeService{Db: db}
|
||||
subscriptionService := services.SubscriptionService{Db: db}
|
||||
subscriptionTypeService := services.SubscriptionTypeService{Db: db}
|
||||
transactionStatusService := services.TransactionStatusService{Db: db}
|
||||
c.Provide(func() *common.RouteGroups {
|
||||
return routeGroups
|
||||
})
|
||||
c.Provide(func() *pg.DB {
|
||||
return db
|
||||
})
|
||||
c.Provide(services.NewApiService)
|
||||
c.Provide(services.NewSubscriptionService)
|
||||
c.Provide(services.NewSubscriptionTypeService)
|
||||
c.Provide(services.NewTransactionService)
|
||||
c.Provide(services.NewTransactionStatusService)
|
||||
c.Provide(services.NewTransactionTypeService)
|
||||
c.Provide(services.NewUsersService)
|
||||
c.Provide(services.NewWalletService)
|
||||
|
||||
walletService.Ss = &subscriptionService
|
||||
transactionService.Ss = &subscriptionService
|
||||
|
||||
controllers.NewApiController(&apiService, api)
|
||||
controllers.NewAuthController(&usersService, auth)
|
||||
controllers.NewWalletsController(&walletService, wallet)
|
||||
controllers.NewWalletsHeaderController(&walletService, walletHeader)
|
||||
controllers.NewTransactionController(&transactionService, transaction)
|
||||
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
||||
controllers.NewSubscriptionController(&subscriptionService, subscription)
|
||||
controllers.NewSubscriptionTypeController(&subscriptionTypeService, subscriptionType)
|
||||
controllers.NewTransactionStatusController(&transactionStatusService, transactionStatus)
|
||||
c.Invoke(controllers.NewApiController)
|
||||
c.Invoke(controllers.NewAuthController)
|
||||
c.Invoke(controllers.NewWalletsController)
|
||||
c.Invoke(controllers.NewWalletsHeaderController)
|
||||
c.Invoke(controllers.NewTransactionController)
|
||||
c.Invoke(controllers.NewTransactionStatusController)
|
||||
c.Invoke(controllers.NewTransactionTypeController)
|
||||
c.Invoke(controllers.NewSubscriptionController)
|
||||
c.Invoke(controllers.NewSubscriptionTypeController)
|
||||
}
|
||||
|
||||
@@ -3,30 +3,33 @@ package controllers
|
||||
import (
|
||||
"wallet-api/pkg/middleware"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApiController struct {
|
||||
ApiService *services.ApiService
|
||||
service *services.ApiService
|
||||
}
|
||||
|
||||
/*
|
||||
NewApiController
|
||||
|
||||
Initializes ApiController.
|
||||
|
||||
Args:
|
||||
*services.ApiService: API service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*ApiController: Controller for "api" interactions
|
||||
*/
|
||||
func NewApiController(as *services.ApiService, s *gin.RouterGroup) *ApiController {
|
||||
ac := new(ApiController)
|
||||
ac.ApiService = as
|
||||
func NewApiController(as *services.ApiService, routeGroups *common.RouteGroups) *ApiController {
|
||||
ac := &ApiController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.GET("", ac.getFirst)
|
||||
s.POST("migrate", middleware.SecretCode, ac.postMigrate)
|
||||
routeGroups.Api.GET("", ac.getFirst)
|
||||
routeGroups.Api.POST("migrate", middleware.SecretCode, ac.postMigrate)
|
||||
|
||||
return ac
|
||||
}
|
||||
@@ -38,7 +41,7 @@ getFirst
|
||||
*/
|
||||
// ROUTE (GET /api).
|
||||
func (ac *ApiController) getFirst(c *gin.Context) {
|
||||
apiModel := ac.ApiService.GetFirst(c)
|
||||
apiModel := ac.service.GetFirst(c)
|
||||
c.JSON(200, apiModel)
|
||||
}
|
||||
|
||||
@@ -53,7 +56,7 @@ Requires "SECRET_CODE", "VERSION" (optional) from body.
|
||||
func (ac *ApiController) postMigrate(c *gin.Context) {
|
||||
migrateModel := c.MustGet("migrate")
|
||||
version := migrateModel.(middleware.SecretCodeModel).Version
|
||||
mr, er := ac.ApiService.PostMigrate(c, version)
|
||||
mr, er := ac.service.PostMigrate(c, version)
|
||||
|
||||
if er.Message != "" {
|
||||
c.JSON(er.StatusCode, er)
|
||||
|
||||
@@ -5,32 +5,35 @@ import (
|
||||
"wallet-api/pkg/middleware"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type AuthController struct {
|
||||
UsersService *services.UsersService
|
||||
service *services.UsersService
|
||||
}
|
||||
|
||||
/*
|
||||
NewAuthController
|
||||
|
||||
Initializes AuthController.
|
||||
|
||||
Args:
|
||||
*services.UsersService: Users service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*AuthController: Controller for "auth" interactions
|
||||
*/
|
||||
func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthController {
|
||||
rc := new(AuthController)
|
||||
rc.UsersService = rs
|
||||
func NewAuthController(rs *services.UsersService, routeGroups *common.RouteGroups) *AuthController {
|
||||
rc := &AuthController{
|
||||
service: rs,
|
||||
}
|
||||
|
||||
s.POST("login", rc.PostLogin)
|
||||
s.POST("register", rc.PostRegister)
|
||||
s.DELETE("deactivate", middleware.Auth, rc.Delete)
|
||||
s.GET("check-token", rc.CheckToken)
|
||||
routeGroups.Auth.POST("login", rc.PostLogin)
|
||||
routeGroups.Auth.POST("register", rc.PostRegister)
|
||||
routeGroups.Auth.DELETE("deactivate", middleware.Auth, rc.Delete)
|
||||
routeGroups.Auth.GET("check-token", rc.CheckToken)
|
||||
|
||||
return rc
|
||||
}
|
||||
@@ -47,7 +50,7 @@ func (rc *AuthController) PostLogin(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
returnedUser, exceptionReturn := rc.UsersService.Login(c, body)
|
||||
returnedUser, exceptionReturn := rc.service.Login(c, body)
|
||||
|
||||
if exceptionReturn.Message != "" {
|
||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||
@@ -71,7 +74,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
returnedUser, exceptionReturn := rc.UsersService.Create(c, body)
|
||||
returnedUser, exceptionReturn := rc.service.Create(c, body)
|
||||
|
||||
if exceptionReturn.Message != "" {
|
||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||
@@ -92,7 +95,7 @@ func (rc *AuthController) Delete(c *gin.Context) {
|
||||
authGet := c.MustGet("auth")
|
||||
auth.Id = authGet.(*models.Auth).Id
|
||||
|
||||
mr, er := rc.UsersService.Deactivate(c, auth)
|
||||
mr, er := rc.service.Deactivate(c, auth)
|
||||
|
||||
if er.Message != "" {
|
||||
c.JSON(er.StatusCode, er)
|
||||
|
||||
@@ -4,30 +4,33 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SubscriptionTypeController struct {
|
||||
SubscriptionTypeService *services.SubscriptionTypeService
|
||||
service *services.SubscriptionTypeService
|
||||
}
|
||||
|
||||
/*
|
||||
NewSubscriptionTypeController
|
||||
|
||||
Initializes SubscriptionTypeController.
|
||||
|
||||
Args:
|
||||
*services.SubscriptionTypeService: Subscription type service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*SubscriptionTypeController: Controller for "subscription-types" route interactions
|
||||
*/
|
||||
func NewSubscriptionTypeController(as *services.SubscriptionTypeService, s *gin.RouterGroup) *SubscriptionTypeController {
|
||||
wc := new(SubscriptionTypeController)
|
||||
wc.SubscriptionTypeService = as
|
||||
func NewSubscriptionTypeController(as *services.SubscriptionTypeService, routeGroups *common.RouteGroups) *SubscriptionTypeController {
|
||||
wc := &SubscriptionTypeController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
routeGroups.SubscriptionType.POST("", wc.New)
|
||||
routeGroups.SubscriptionType.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -45,7 +48,7 @@ func (wc *SubscriptionTypeController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.SubscriptionTypeService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -62,7 +65,7 @@ GetAll
|
||||
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm, exception := wc.SubscriptionTypeService.GetAll(c, embed)
|
||||
wm, exception := wc.service.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -4,34 +4,37 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SubscriptionController struct {
|
||||
SubscriptionService *services.SubscriptionService
|
||||
service *services.SubscriptionService
|
||||
}
|
||||
|
||||
/*
|
||||
NewSubscriptionController
|
||||
|
||||
Initializes SubscriptionController.
|
||||
|
||||
Args:
|
||||
*services.SubscriptionService: Subscription service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*SubscriptionController: Controller for "subscription" route interactions
|
||||
*/
|
||||
func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGroup) *SubscriptionController {
|
||||
wc := new(SubscriptionController)
|
||||
wc.SubscriptionService = as
|
||||
func NewSubscriptionController(as *services.SubscriptionService, routeGroups *common.RouteGroups) *SubscriptionController {
|
||||
wc := &SubscriptionController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.PUT("/:id", wc.Edit)
|
||||
s.GET("/:id", wc.Get)
|
||||
s.GET("", wc.GetAll)
|
||||
routeGroups.Subscription.POST("", wc.New)
|
||||
routeGroups.Subscription.PUT("/:id", wc.Edit)
|
||||
routeGroups.Subscription.GET("/:id", wc.Get)
|
||||
routeGroups.Subscription.GET("", wc.GetAll)
|
||||
|
||||
se := s.Group("/end")
|
||||
se := routeGroups.Subscription.Group("/end")
|
||||
{
|
||||
se.PUT("/:id", wc.End)
|
||||
}
|
||||
@@ -52,7 +55,7 @@ func (wc *SubscriptionController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.SubscriptionService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
@@ -77,7 +80,7 @@ func (wc *SubscriptionController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm, exception := wc.SubscriptionService.Edit(c, body, id)
|
||||
wm, exception := wc.service.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -103,7 +106,7 @@ func (wc *SubscriptionController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr, exception := wc.SubscriptionService.Get(c, body, id, params)
|
||||
fr, exception := wc.service.Get(c, body, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -127,7 +130,7 @@ func (wc *SubscriptionController) End(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
fr, exception := wc.SubscriptionService.End(c, id)
|
||||
fr, exception := wc.service.End(c, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -150,7 +153,7 @@ func (wc *SubscriptionController) GetAll(c *gin.Context) {
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
exception := wc.SubscriptionService.GetAll(c, body, wallet, fr)
|
||||
exception := wc.service.GetAll(c, body, wallet, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -4,30 +4,33 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TransactionStatusController struct {
|
||||
TransactionStatusService *services.TransactionStatusService
|
||||
service *services.TransactionStatusService
|
||||
}
|
||||
|
||||
/*
|
||||
NewTransactionStatusController
|
||||
|
||||
Initializes TransactionStatusController.
|
||||
|
||||
Args:
|
||||
*services.TransactionStatusService: Transaction Staus service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*TransactionStatusController: Controller for "transaction-status" route interactions
|
||||
*/
|
||||
func NewTransactionStatusController(as *services.TransactionStatusService, s *gin.RouterGroup) *TransactionStatusController {
|
||||
wc := new(TransactionStatusController)
|
||||
wc.TransactionStatusService = as
|
||||
func NewTransactionStatusController(as *services.TransactionStatusService, routeGroups *common.RouteGroups) *TransactionStatusController {
|
||||
wc := &TransactionStatusController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
routeGroups.TransactionStatus.POST("", wc.New)
|
||||
routeGroups.TransactionStatus.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -45,7 +48,7 @@ func (wc *TransactionStatusController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.TransactionStatusService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -62,7 +65,7 @@ GetAll
|
||||
func (wc *TransactionStatusController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm, exception := wc.TransactionStatusService.GetAll(c, embed)
|
||||
wm, exception := wc.service.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -4,30 +4,33 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TransactionTypeController struct {
|
||||
TransactionTypeService *services.TransactionTypeService
|
||||
service *services.TransactionTypeService
|
||||
}
|
||||
|
||||
/*
|
||||
NewTransactionTypeController
|
||||
|
||||
Initializes TransactionTypeController.
|
||||
|
||||
Args:
|
||||
*services.TransactionTypeService: Transaction Type service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*TransactionTypeController: Controller for "transaction-types" route interactions
|
||||
*/
|
||||
func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.RouterGroup) *TransactionTypeController {
|
||||
wc := new(TransactionTypeController)
|
||||
wc.TransactionTypeService = as
|
||||
func NewTransactionTypeController(as *services.TransactionTypeService, routeGroups *common.RouteGroups) *TransactionTypeController {
|
||||
wc := &TransactionTypeController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
routeGroups.TransactionType.POST("", wc.New)
|
||||
routeGroups.TransactionType.GET("", wc.GetAll)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -45,7 +48,7 @@ func (wc *TransactionTypeController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.TransactionTypeService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -62,7 +65,7 @@ GetAll
|
||||
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
|
||||
wm, exception := wc.TransactionTypeService.GetAll(c, embed)
|
||||
wm, exception := wc.service.GetAll(c, embed)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -4,39 +4,42 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TransactionController struct {
|
||||
TransactionService *services.TransactionService
|
||||
service *services.TransactionService
|
||||
}
|
||||
|
||||
/*
|
||||
NewTransactionController
|
||||
|
||||
Initializes TransactionController.
|
||||
|
||||
Args:
|
||||
*services.TransactionService: Transaction service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*TransactionController: Controller for "transaction" route interactions
|
||||
*/
|
||||
func NewTransactionController(as *services.TransactionService, s *gin.RouterGroup) *TransactionController {
|
||||
wc := new(TransactionController)
|
||||
wc.TransactionService = as
|
||||
func NewTransactionController(as *services.TransactionService, routeGroups *common.RouteGroups) *TransactionController {
|
||||
wc := &TransactionController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
s.PUT("/:id", wc.Edit)
|
||||
s.GET("/:id", wc.Get)
|
||||
routeGroups.Transaction.POST("", wc.New)
|
||||
routeGroups.Transaction.GET("", wc.GetAll)
|
||||
routeGroups.Transaction.PUT("/:id", wc.Edit)
|
||||
routeGroups.Transaction.GET("/:id", wc.Get)
|
||||
|
||||
bulkGroup := s.Group("bulk")
|
||||
bulkGroup := routeGroups.Transaction.Group("bulk")
|
||||
{
|
||||
bulkGroup.PUT("", wc.BulkEdit)
|
||||
}
|
||||
|
||||
checkGroup := s.Group("check")
|
||||
checkGroup := routeGroups.Transaction.Group("check")
|
||||
{
|
||||
checkGroup.GET("", wc.Check)
|
||||
}
|
||||
@@ -57,7 +60,7 @@ func (wc *TransactionController) New(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.TransactionService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -82,7 +85,7 @@ func (wc *TransactionController) GetAll(c *gin.Context) {
|
||||
noPendingQry, _ := c.GetQuery("noPending")
|
||||
noPending := noPendingQry != ""
|
||||
|
||||
exception := wc.TransactionService.GetAll(c, body, wallet, fr, noPending)
|
||||
exception := wc.service.GetAll(c, body, wallet, fr, noPending)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -105,7 +108,7 @@ func (wc *TransactionController) Check(c *gin.Context) {
|
||||
fr := FilteredResponse(c)
|
||||
wallet, _ := c.GetQuery("walletId")
|
||||
|
||||
exception := wc.TransactionService.Check(c, body, wallet, fr)
|
||||
exception := wc.service.Check(c, body, wallet, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -129,7 +132,7 @@ func (wc *TransactionController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm, exception := wc.TransactionService.Edit(c, body, id)
|
||||
wm, exception := wc.service.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -150,7 +153,7 @@ func (wc *TransactionController) BulkEdit(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
wm, exception := wc.TransactionService.BulkEdit(c, body)
|
||||
wm, exception := wc.service.BulkEdit(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -176,7 +179,7 @@ func (wc *TransactionController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr, exception := wc.TransactionService.Get(c, body, id, params)
|
||||
fr, exception := wc.service.Get(c, body, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -3,29 +3,32 @@ package controllers
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type WalletsHeaderController struct {
|
||||
WalletService *services.WalletService
|
||||
service *services.WalletService
|
||||
}
|
||||
|
||||
/*
|
||||
NewWalletsHeaderController
|
||||
|
||||
Initializes WalletsHeaderController.
|
||||
|
||||
Args:
|
||||
*services.WalletService: Wallet service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*WalletsHeaderController: Controller for "wallet/wallet-header" route interactions
|
||||
*/
|
||||
func NewWalletsHeaderController(as *services.WalletService, s *gin.RouterGroup) *WalletsHeaderController {
|
||||
wc := new(WalletsHeaderController)
|
||||
wc.WalletService = as
|
||||
func NewWalletsHeaderController(as *services.WalletService, routeGroups *common.RouteGroups) *WalletsHeaderController {
|
||||
wc := &WalletsHeaderController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.GET("", wc.Get)
|
||||
routeGroups.WalletHeader.GET("", wc.Get)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -44,7 +47,7 @@ func (wc *WalletsHeaderController) Get(c *gin.Context) {
|
||||
auth := c.MustGet("auth")
|
||||
body.Id = auth.(*models.Auth).Id
|
||||
|
||||
wm, exception := wc.WalletService.GetHeader(c, body, walletId)
|
||||
wm, exception := wc.service.GetHeader(c, body, walletId)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -4,32 +4,35 @@ import (
|
||||
"net/http"
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/services"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type WalletsController struct {
|
||||
WalletService *services.WalletService
|
||||
service *services.WalletService
|
||||
}
|
||||
|
||||
/*
|
||||
NewWalletsController
|
||||
|
||||
Initializes WalletsController.
|
||||
|
||||
Args:
|
||||
*services.WalletService: Wallet service
|
||||
*gin.RouterGroup: Gin Router Group
|
||||
Returns:
|
||||
*WalletsController: Controller for "wallet" route interactions
|
||||
*/
|
||||
func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *WalletsController {
|
||||
wc := new(WalletsController)
|
||||
wc.WalletService = as
|
||||
func NewWalletsController(as *services.WalletService, routeGroups *common.RouteGroups) *WalletsController {
|
||||
wc := &WalletsController{
|
||||
service: as,
|
||||
}
|
||||
|
||||
s.POST("", wc.New)
|
||||
s.GET("", wc.GetAll)
|
||||
s.PUT("/:id", wc.Edit)
|
||||
s.GET("/:id", wc.Get)
|
||||
routeGroups.Wallet.POST("", wc.New)
|
||||
routeGroups.Wallet.GET("", wc.GetAll)
|
||||
routeGroups.Wallet.PUT("/:id", wc.Edit)
|
||||
routeGroups.Wallet.GET("/:id", wc.Get)
|
||||
|
||||
return wc
|
||||
}
|
||||
@@ -51,7 +54,7 @@ func (wc *WalletsController) New(c *gin.Context) {
|
||||
get := c.MustGet("auth")
|
||||
body.UserID = get.(*models.Auth).Id
|
||||
|
||||
wm, exception := wc.WalletService.New(c, body)
|
||||
wm, exception := wc.service.New(c, body)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -72,7 +75,7 @@ func (wc *WalletsController) GetAll(c *gin.Context) {
|
||||
|
||||
fr := FilteredResponse(c)
|
||||
|
||||
exception := wc.WalletService.GetAll(c, body, fr)
|
||||
exception := wc.service.GetAll(c, body, fr)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -96,7 +99,7 @@ func (wc *WalletsController) Edit(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
|
||||
wm, exception := wc.WalletService.Edit(c, body, id)
|
||||
wm, exception := wc.service.Edit(c, body, id)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
@@ -118,7 +121,7 @@ func (wc *WalletsController) Get(c *gin.Context) {
|
||||
embed, _ := c.GetQuery("embed")
|
||||
params.Embed = embed
|
||||
|
||||
fr, exception := wc.WalletService.Get(c, id, params)
|
||||
fr, exception := wc.service.Get(c, id, params)
|
||||
if exception != nil {
|
||||
c.JSON(exception.StatusCode, exception)
|
||||
return
|
||||
|
||||
@@ -9,21 +9,27 @@ import (
|
||||
)
|
||||
|
||||
type ApiService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewApiService(db *pg.DB) *ApiService {
|
||||
return &ApiService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
GetFirst
|
||||
|
||||
Gets first row from API table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
models.ApiModel: Api object from database.
|
||||
*/
|
||||
func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
|
||||
db := as.Db.WithContext(ctx)
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
Returns:
|
||||
models.ApiModel: Api object from database.
|
||||
*/
|
||||
func (as ApiService) GetFirst(ctx context.Context) models.ApiModel {
|
||||
db := as.db.WithContext(ctx)
|
||||
apiModel := models.ApiModel{Api: "Works"}
|
||||
db.Model(&apiModel).First()
|
||||
return apiModel
|
||||
@@ -33,15 +39,16 @@ func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
|
||||
PostMigrate
|
||||
|
||||
Starts database migration.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Migration version
|
||||
Returns:
|
||||
*models.MessageResponse: Message response object.
|
||||
*models.Exception: Exception response object.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Migration version
|
||||
Returns:
|
||||
*models.MessageResponse: Message response object.
|
||||
*models.Exception: Exception response object.
|
||||
*/
|
||||
func (as *ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
func (as ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
mr := new(models.MessageResponse)
|
||||
er := new(models.Exception)
|
||||
|
||||
@@ -10,22 +10,29 @@ import (
|
||||
)
|
||||
|
||||
type SubscriptionTypeService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewSubscriptionTypeService(db *pg.DB) *SubscriptionTypeService {
|
||||
return &SubscriptionTypeService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to subscription type table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewSubscriptionTypeBody: Values to create new row
|
||||
Returns:
|
||||
*models.SubscriptionType: Created row from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewSubscriptionTypeBody: Values to create new row
|
||||
Returns:
|
||||
*models.SubscriptionType: Created row from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) (*models.SubscriptionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(models.SubscriptionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -49,15 +56,16 @@ func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubs
|
||||
GetAll
|
||||
|
||||
Gets all rows from subscription type table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.SubscriptionType: List of subscription type objects.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) (*[]models.SubscriptionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.SubscriptionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
@@ -12,22 +12,29 @@ import (
|
||||
)
|
||||
|
||||
type SubscriptionService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewSubscriptionService(db *pg.DB) *SubscriptionService {
|
||||
return &SubscriptionService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to subscription table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewSubscriptionBody: Request body
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewSubscriptionBody: Request body
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(models.Subscription)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -69,17 +76,18 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
|
||||
Get
|
||||
|
||||
Gets row from subscription table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication model
|
||||
string: subscription id to search
|
||||
params: *models.Params
|
||||
Returns:
|
||||
*models.Subscription: Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication model
|
||||
string: subscription id to search
|
||||
params: *models.Params
|
||||
Returns:
|
||||
*models.Subscription: Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new(models.Subscription)
|
||||
@@ -106,16 +114,17 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
|
||||
GetAll
|
||||
|
||||
Gets filtered rows from subscription table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: Wallet id to search
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: Wallet id to search
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.Subscription)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -144,16 +153,17 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
|
||||
Edit
|
||||
|
||||
Updates row from subscription table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.SubscriptionEdit: Values to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Edited Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.SubscriptionEdit: Values to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Edited Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -188,15 +198,16 @@ End
|
||||
Updates row in subscription table by id.
|
||||
|
||||
Ends subscription with current date.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Subscription: Created Subscription row object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subscription, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Subscription)
|
||||
@@ -224,11 +235,12 @@ func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subs
|
||||
SubToTrans
|
||||
|
||||
Generates and Inserts new Transaction rows from the subscription model.
|
||||
Args:
|
||||
*models.Subscription: Subscription model to generate new transactions from
|
||||
*pg.Tx: Postgres query context
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
*models.Subscription: Subscription model to generate new transactions from
|
||||
*pg.Tx: Postgres query context
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *SubscriptionService) SubToTrans(subModel *models.Subscription, tx *pg.Tx) *models.Exception {
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
@@ -10,22 +10,29 @@ import (
|
||||
)
|
||||
|
||||
type TransactionStatusService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewTransactionStatusService(db *pg.DB) *TransactionStatusService {
|
||||
return &TransactionStatusService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionStatusBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) (*models.TransactionStatus, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -49,15 +56,16 @@ func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTra
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction status table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionStatus: List of Transaction status objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionStatus: List of Transaction status objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*[]models.TransactionStatus, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionStatus)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
@@ -10,22 +10,29 @@ import (
|
||||
)
|
||||
|
||||
type TransactionTypeService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewTransactionTypeService(db *pg.DB) *TransactionTypeService {
|
||||
return &TransactionTypeService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts new row to transaction type table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionTypeBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionTypeBody: object to create
|
||||
Returns:
|
||||
*models.TransactionType: Transaction Type object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) (*models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
tm := new(models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -49,15 +56,16 @@ func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTrans
|
||||
GetAll
|
||||
|
||||
Gets all rows from transaction type table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionType: List of Transaction type objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: Relations to embed
|
||||
Returns:
|
||||
*[]models.TransactionType: List of Transaction type objects from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]models.TransactionType, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.TransactionType)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
@@ -12,23 +12,31 @@ import (
|
||||
)
|
||||
|
||||
type TransactionService struct {
|
||||
Db *pg.DB
|
||||
Ss *SubscriptionService
|
||||
db *pg.DB
|
||||
subscriptionService *SubscriptionService
|
||||
}
|
||||
|
||||
func NewTransactionService(db *pg.DB, ss *SubscriptionService) *TransactionService {
|
||||
return &TransactionService{
|
||||
db: db,
|
||||
subscriptionService: ss,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New new row into transaction table
|
||||
New row into transaction table
|
||||
|
||||
Inserts
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*models.Transaction: Transaction object
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewTransactionBody: Transaction body object
|
||||
Returns:
|
||||
*models.Transaction: Transaction object
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
tm := new(models.Transaction)
|
||||
@@ -83,7 +91,7 @@ Gets all rows from subscription type table.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse, noPending bool) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new([]models.Transaction)
|
||||
@@ -132,7 +140,7 @@ Checks subscriptions and create transacitons.
|
||||
*/
|
||||
// Gets filtered rows from transaction table.
|
||||
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new([]models.Transaction)
|
||||
sm := new([]models.Subscription)
|
||||
@@ -157,7 +165,7 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
|
||||
|
||||
for _, sub := range *sm {
|
||||
if sub.HasNew() {
|
||||
as.Ss.SubToTrans(&sub, tx)
|
||||
as.subscriptionService.SubToTrans(&sub, tx)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,16 +191,17 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
|
||||
Edit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.TransactionEdit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.TransactionEdit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
amount, _ := body.Amount.Float64()
|
||||
|
||||
@@ -236,16 +245,17 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
|
||||
Bulk Edit
|
||||
|
||||
Updates row in transaction table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
?[]models.Transaction Bulk Edit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
?[]models.Transaction Bulk Edit: Object to edit
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) (*[]models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
tx, _ := db.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
@@ -284,17 +294,18 @@ func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.Trans
|
||||
Get
|
||||
|
||||
Gets row from transaction table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: id to search
|
||||
*model.Params: url query parameters
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: id to search
|
||||
*model.Params: url query parameters
|
||||
Returns:
|
||||
*models.Transaction: Transaction object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Transaction, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
wm := new(models.Transaction)
|
||||
|
||||
@@ -15,22 +15,29 @@ import (
|
||||
)
|
||||
|
||||
type UsersService struct {
|
||||
Db *pg.DB
|
||||
db *pg.DB
|
||||
}
|
||||
|
||||
func NewUsersService(db *pg.DB) *UsersService {
|
||||
return &UsersService{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Create
|
||||
|
||||
Inserts new row to users table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.User: User object to create
|
||||
Returns:
|
||||
*models.User: User object from database
|
||||
*models.Exception
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.User: User object to create
|
||||
Returns:
|
||||
*models.User: User object from database
|
||||
*models.Exception
|
||||
*/
|
||||
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *models.Exception) {
|
||||
db := us.Db.WithContext(ctx)
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
check := new(models.User)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -67,15 +74,16 @@ func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (
|
||||
Login
|
||||
|
||||
Gets row from users table by email and valid password.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Login: object to search
|
||||
Returns:
|
||||
*models.Token: new session token
|
||||
*models.Exception
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Login: object to search
|
||||
Returns:
|
||||
*models.Token: new session token
|
||||
*models.Exception
|
||||
*/
|
||||
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.Token, *models.Exception) {
|
||||
db := us.Db.WithContext(ctx)
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
check := new(models.User)
|
||||
exceptionReturn := new(models.Exception)
|
||||
@@ -123,15 +131,16 @@ Deactivate
|
||||
Updates row in users table.
|
||||
|
||||
IsActive column is set to false
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
Returns:
|
||||
*models.MessageResponse
|
||||
*models.Exception
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
Returns:
|
||||
*models.MessageResponse
|
||||
*models.Exception
|
||||
*/
|
||||
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*models.MessageResponse, *models.Exception) {
|
||||
db := us.Db.WithContext(ctx)
|
||||
db := us.db.WithContext(ctx)
|
||||
|
||||
mm := new(models.MessageResponse)
|
||||
me := new(models.Exception)
|
||||
@@ -171,12 +180,13 @@ CreateToken
|
||||
Generates new jwt token.
|
||||
|
||||
It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours.
|
||||
Args:
|
||||
*models.User: User object to encode
|
||||
bool: Should function generate longer lasting token (48hrs)
|
||||
Returns:
|
||||
string: Generated token
|
||||
error: Error that occured in the process
|
||||
|
||||
Args:
|
||||
*models.User: User object to encode
|
||||
bool: Should function generate longer lasting token (48hrs)
|
||||
Returns:
|
||||
string: Generated token
|
||||
error: Error that occured in the process
|
||||
*/
|
||||
func CreateToken(user *models.User, rememberMe bool) (string, error) {
|
||||
atClaims := jwt.MapClaims{}
|
||||
|
||||
@@ -11,23 +11,31 @@ import (
|
||||
)
|
||||
|
||||
type WalletService struct {
|
||||
Db *pg.DB
|
||||
Ss *SubscriptionService
|
||||
db *pg.DB
|
||||
subscriptionService *SubscriptionService
|
||||
}
|
||||
|
||||
func NewWalletService(db *pg.DB, ss *SubscriptionService) *WalletService {
|
||||
return &WalletService{
|
||||
db: db,
|
||||
subscriptionService: ss,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
New
|
||||
|
||||
Inserts row to wallets table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewWalletBody: Object to be inserted
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.NewWalletBody: Object to be inserted
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
walletModel := new(models.Wallet)
|
||||
@@ -48,16 +56,17 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*mo
|
||||
Edit
|
||||
|
||||
Updates row in wallets table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.WalletEdit: Object to be edited
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.WalletEdit: Object to be edited
|
||||
string: id to search
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database.
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
exceptionReturn := new(models.Exception)
|
||||
tm := new(models.Wallet)
|
||||
@@ -84,16 +93,17 @@ func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id s
|
||||
Get
|
||||
|
||||
Gets row in wallets table by id.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
*models.Params: url query parameters
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
string: id to search
|
||||
*models.Params: url query parameters
|
||||
Returns:
|
||||
*models.Wallet: Wallet object from database
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) (*models.Wallet, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
exceptionReturn := new(models.Exception)
|
||||
|
||||
wm := new(models.Wallet)
|
||||
@@ -120,16 +130,17 @@ func (as *WalletService) Get(ctx context.Context, id string, params *models.Para
|
||||
GetAll
|
||||
|
||||
Gets filtered rows from wallets table.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
*models.FilteredResponse: filter options
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) *models.Exception {
|
||||
exceptionReturn := new(models.Exception)
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
wm := new([]models.Wallet)
|
||||
|
||||
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||
@@ -149,16 +160,17 @@ GetHeader
|
||||
Gets row from wallets table.
|
||||
|
||||
Calculates previous month, current and next month totals.
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: wallet id to search
|
||||
Returns:
|
||||
*models.WalletHeader: generated wallet header object
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
context.Context: Application context
|
||||
*models.Auth: Authentication object
|
||||
string: wallet id to search
|
||||
Returns:
|
||||
*models.WalletHeader: generated wallet header object
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) (*models.WalletHeader, *models.Exception) {
|
||||
db := as.Db.WithContext(ctx)
|
||||
db := as.db.WithContext(ctx)
|
||||
|
||||
wm := new(models.WalletHeader)
|
||||
wallets := new([]models.WalletTransactions)
|
||||
@@ -286,12 +298,13 @@ addWhere
|
||||
Appends Transaction to the belonging walletId.
|
||||
|
||||
If missing, it creates the item list.
|
||||
Args:
|
||||
*[]models.WalletTransactions: list to append to
|
||||
string: wallet id to check
|
||||
models.Transaction: Transaction to append
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
|
||||
Args:
|
||||
*[]models.WalletTransactions: list to append to
|
||||
string: wallet id to check
|
||||
models.Transaction: Transaction to append
|
||||
Returns:
|
||||
*models.Exception: Exception payload.
|
||||
*/
|
||||
func addWhere(s *[]models.WalletTransactions, walletId string, e models.Transaction) {
|
||||
var exists bool
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
@@ -8,6 +9,18 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RouteGroups struct {
|
||||
Api *gin.RouterGroup
|
||||
Auth *gin.RouterGroup
|
||||
Wallet *gin.RouterGroup
|
||||
WalletHeader *gin.RouterGroup
|
||||
Transaction *gin.RouterGroup
|
||||
TransactionType *gin.RouterGroup
|
||||
Subscription *gin.RouterGroup
|
||||
SubscriptionType *gin.RouterGroup
|
||||
TransactionStatus *gin.RouterGroup
|
||||
}
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Printf("Error occured. %v", err)
|
||||
@@ -38,4 +51,4 @@ func GetIP() string {
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user