This commit is contained in:
Fran Jurmanović
2022-09-26 21:17:30 +02:00
parent 73a41f2b54
commit 13ce0735d0
19 changed files with 466 additions and 341 deletions

View File

@@ -4,57 +4,65 @@ import (
"wallet-api/pkg/controllers" "wallet-api/pkg/controllers"
"wallet-api/pkg/middleware" "wallet-api/pkg/middleware"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"wallet-api/pkg/utl/configs" "wallet-api/pkg/utl/configs"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10"
"go.uber.org/dig"
) )
/* /*
Routes Routes
Initializes web api controllers and its corresponding routes. Initializes web api controllers and its corresponding routes.
Args: Args:
*gin.Engine: Gin Engine *gin.Engine: Gin Engine
*pg.DB: Postgres database client *pg.DB: Postgres database client
*/ */
func Routes(s *gin.Engine, db *pg.DB) { func Routes(s *gin.Engine, db *pg.DB) {
c := dig.New()
ver := s.Group(configs.Prefix) ver := s.Group(configs.Prefix)
api := ver.Group("api") routeGroups := &common.RouteGroups{
auth := ver.Group("auth") Api: ver.Group("api"),
wallet := ver.Group("wallet", middleware.Auth) Auth: ver.Group("auth"),
walletHeader := ver.Group("wallet/wallet-header", middleware.Auth) Wallet: ver.Group("wallet", middleware.Auth),
transaction := ver.Group("transaction", middleware.Auth) WalletHeader: ver.Group("wallet/wallet-header", middleware.Auth),
transactionType := ver.Group("transaction-type", middleware.Auth) Transaction: ver.Group("transaction", middleware.Auth),
subscription := ver.Group("subscription", middleware.Auth) TransactionType: ver.Group("transaction-type", middleware.Auth),
subscriptionType := ver.Group("subscription-type", middleware.Auth) Subscription: ver.Group("subscription", middleware.Auth),
transactionStatus := ver.Group("transaction-status", middleware.Auth) SubscriptionType: ver.Group("subscription-type", middleware.Auth),
TransactionStatus: ver.Group("transaction-status", middleware.Auth),
}
s.NoRoute(func(c *gin.Context) { s.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"}) c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND", "message": "Page not found"})
}) })
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)
apiService := services.ApiService{Db: db} c.Invoke(controllers.NewApiController)
usersService := services.UsersService{Db: db} c.Invoke(controllers.NewAuthController)
walletService := services.WalletService{Db: db} c.Invoke(controllers.NewWalletsController)
transactionService := services.TransactionService{Db: db} c.Invoke(controllers.NewWalletsHeaderController)
transactionTypeService := services.TransactionTypeService{Db: db} c.Invoke(controllers.NewTransactionController)
subscriptionService := services.SubscriptionService{Db: db} c.Invoke(controllers.NewTransactionStatusController)
subscriptionTypeService := services.SubscriptionTypeService{Db: db} c.Invoke(controllers.NewTransactionTypeController)
transactionStatusService := services.TransactionStatusService{Db: db} c.Invoke(controllers.NewSubscriptionController)
c.Invoke(controllers.NewSubscriptionTypeController)
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)
} }

View File

@@ -3,30 +3,33 @@ package controllers
import ( import (
"wallet-api/pkg/middleware" "wallet-api/pkg/middleware"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type ApiController struct { type ApiController struct {
ApiService *services.ApiService service *services.ApiService
} }
/* /*
NewApiController NewApiController
Initializes ApiController. Initializes ApiController.
Args: Args:
*services.ApiService: API service *services.ApiService: API service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*ApiController: Controller for "api" interactions *ApiController: Controller for "api" interactions
*/ */
func NewApiController(as *services.ApiService, s *gin.RouterGroup) *ApiController { func NewApiController(as *services.ApiService, routeGroups *common.RouteGroups) *ApiController {
ac := new(ApiController) ac := &ApiController{
ac.ApiService = as service: as,
}
s.GET("", ac.getFirst) routeGroups.Api.GET("", ac.getFirst)
s.POST("migrate", middleware.SecretCode, ac.postMigrate) routeGroups.Api.POST("migrate", middleware.SecretCode, ac.postMigrate)
return ac return ac
} }
@@ -38,7 +41,7 @@ getFirst
*/ */
// ROUTE (GET /api). // ROUTE (GET /api).
func (ac *ApiController) getFirst(c *gin.Context) { func (ac *ApiController) getFirst(c *gin.Context) {
apiModel := ac.ApiService.GetFirst(c) apiModel := ac.service.GetFirst(c)
c.JSON(200, apiModel) c.JSON(200, apiModel)
} }
@@ -53,7 +56,7 @@ Requires "SECRET_CODE", "VERSION" (optional) from body.
func (ac *ApiController) postMigrate(c *gin.Context) { func (ac *ApiController) postMigrate(c *gin.Context) {
migrateModel := c.MustGet("migrate") migrateModel := c.MustGet("migrate")
version := migrateModel.(middleware.SecretCodeModel).Version version := migrateModel.(middleware.SecretCodeModel).Version
mr, er := ac.ApiService.PostMigrate(c, version) mr, er := ac.service.PostMigrate(c, version)
if er.Message != "" { if er.Message != "" {
c.JSON(er.StatusCode, er) c.JSON(er.StatusCode, er)

View File

@@ -5,32 +5,35 @@ import (
"wallet-api/pkg/middleware" "wallet-api/pkg/middleware"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type AuthController struct { type AuthController struct {
UsersService *services.UsersService service *services.UsersService
} }
/* /*
NewAuthController NewAuthController
Initializes AuthController. Initializes AuthController.
Args: Args:
*services.UsersService: Users service *services.UsersService: Users service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*AuthController: Controller for "auth" interactions *AuthController: Controller for "auth" interactions
*/ */
func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthController { func NewAuthController(rs *services.UsersService, routeGroups *common.RouteGroups) *AuthController {
rc := new(AuthController) rc := &AuthController{
rc.UsersService = rs service: rs,
}
s.POST("login", rc.PostLogin) routeGroups.Auth.POST("login", rc.PostLogin)
s.POST("register", rc.PostRegister) routeGroups.Auth.POST("register", rc.PostRegister)
s.DELETE("deactivate", middleware.Auth, rc.Delete) routeGroups.Auth.DELETE("deactivate", middleware.Auth, rc.Delete)
s.GET("check-token", rc.CheckToken) routeGroups.Auth.GET("check-token", rc.CheckToken)
return rc return rc
} }
@@ -47,7 +50,7 @@ func (rc *AuthController) PostLogin(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }
returnedUser, exceptionReturn := rc.UsersService.Login(c, body) returnedUser, exceptionReturn := rc.service.Login(c, body)
if exceptionReturn.Message != "" { if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn) 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()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return return
} }
returnedUser, exceptionReturn := rc.UsersService.Create(c, body) returnedUser, exceptionReturn := rc.service.Create(c, body)
if exceptionReturn.Message != "" { if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn) c.JSON(exceptionReturn.StatusCode, exceptionReturn)
@@ -92,7 +95,7 @@ func (rc *AuthController) Delete(c *gin.Context) {
authGet := c.MustGet("auth") authGet := c.MustGet("auth")
auth.Id = authGet.(*models.Auth).Id auth.Id = authGet.(*models.Auth).Id
mr, er := rc.UsersService.Deactivate(c, auth) mr, er := rc.service.Deactivate(c, auth)
if er.Message != "" { if er.Message != "" {
c.JSON(er.StatusCode, er) c.JSON(er.StatusCode, er)

View File

@@ -4,30 +4,33 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type SubscriptionTypeController struct { type SubscriptionTypeController struct {
SubscriptionTypeService *services.SubscriptionTypeService service *services.SubscriptionTypeService
} }
/* /*
NewSubscriptionTypeController NewSubscriptionTypeController
Initializes SubscriptionTypeController. Initializes SubscriptionTypeController.
Args: Args:
*services.SubscriptionTypeService: Subscription type service *services.SubscriptionTypeService: Subscription type service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*SubscriptionTypeController: Controller for "subscription-types" route interactions *SubscriptionTypeController: Controller for "subscription-types" route interactions
*/ */
func NewSubscriptionTypeController(as *services.SubscriptionTypeService, s *gin.RouterGroup) *SubscriptionTypeController { func NewSubscriptionTypeController(as *services.SubscriptionTypeService, routeGroups *common.RouteGroups) *SubscriptionTypeController {
wc := new(SubscriptionTypeController) wc := &SubscriptionTypeController{
wc.SubscriptionTypeService = as service: as,
}
s.POST("", wc.New) routeGroups.SubscriptionType.POST("", wc.New)
s.GET("", wc.GetAll) routeGroups.SubscriptionType.GET("", wc.GetAll)
return wc return wc
} }
@@ -45,7 +48,7 @@ func (wc *SubscriptionTypeController) New(c *gin.Context) {
return return
} }
wm, exception := wc.SubscriptionTypeService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -62,7 +65,7 @@ GetAll
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) { func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
wm, exception := wc.SubscriptionTypeService.GetAll(c, embed) wm, exception := wc.service.GetAll(c, embed)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -4,34 +4,37 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type SubscriptionController struct { type SubscriptionController struct {
SubscriptionService *services.SubscriptionService service *services.SubscriptionService
} }
/* /*
NewSubscriptionController NewSubscriptionController
Initializes SubscriptionController. Initializes SubscriptionController.
Args: Args:
*services.SubscriptionService: Subscription service *services.SubscriptionService: Subscription service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*SubscriptionController: Controller for "subscription" route interactions *SubscriptionController: Controller for "subscription" route interactions
*/ */
func NewSubscriptionController(as *services.SubscriptionService, s *gin.RouterGroup) *SubscriptionController { func NewSubscriptionController(as *services.SubscriptionService, routeGroups *common.RouteGroups) *SubscriptionController {
wc := new(SubscriptionController) wc := &SubscriptionController{
wc.SubscriptionService = as service: as,
}
s.POST("", wc.New) routeGroups.Subscription.POST("", wc.New)
s.PUT("/:id", wc.Edit) routeGroups.Subscription.PUT("/:id", wc.Edit)
s.GET("/:id", wc.Get) routeGroups.Subscription.GET("/:id", wc.Get)
s.GET("", wc.GetAll) routeGroups.Subscription.GET("", wc.GetAll)
se := s.Group("/end") se := routeGroups.Subscription.Group("/end")
{ {
se.PUT("/:id", wc.End) se.PUT("/:id", wc.End)
} }
@@ -52,7 +55,7 @@ func (wc *SubscriptionController) New(c *gin.Context) {
return return
} }
wm, exception := wc.SubscriptionService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
@@ -77,7 +80,7 @@ func (wc *SubscriptionController) Edit(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
wm, exception := wc.SubscriptionService.Edit(c, body, id) wm, exception := wc.service.Edit(c, body, id)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -103,7 +106,7 @@ func (wc *SubscriptionController) Get(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
params.Embed = 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 { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -127,7 +130,7 @@ func (wc *SubscriptionController) End(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
fr, exception := wc.SubscriptionService.End(c, id) fr, exception := wc.service.End(c, id)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -150,7 +153,7 @@ func (wc *SubscriptionController) GetAll(c *gin.Context) {
fr := FilteredResponse(c) fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId") wallet, _ := c.GetQuery("walletId")
exception := wc.SubscriptionService.GetAll(c, body, wallet, fr) exception := wc.service.GetAll(c, body, wallet, fr)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -4,30 +4,33 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type TransactionStatusController struct { type TransactionStatusController struct {
TransactionStatusService *services.TransactionStatusService service *services.TransactionStatusService
} }
/* /*
NewTransactionStatusController NewTransactionStatusController
Initializes TransactionStatusController. Initializes TransactionStatusController.
Args: Args:
*services.TransactionStatusService: Transaction Staus service *services.TransactionStatusService: Transaction Staus service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*TransactionStatusController: Controller for "transaction-status" route interactions *TransactionStatusController: Controller for "transaction-status" route interactions
*/ */
func NewTransactionStatusController(as *services.TransactionStatusService, s *gin.RouterGroup) *TransactionStatusController { func NewTransactionStatusController(as *services.TransactionStatusService, routeGroups *common.RouteGroups) *TransactionStatusController {
wc := new(TransactionStatusController) wc := &TransactionStatusController{
wc.TransactionStatusService = as service: as,
}
s.POST("", wc.New) routeGroups.TransactionStatus.POST("", wc.New)
s.GET("", wc.GetAll) routeGroups.TransactionStatus.GET("", wc.GetAll)
return wc return wc
} }
@@ -45,7 +48,7 @@ func (wc *TransactionStatusController) New(c *gin.Context) {
return return
} }
wm, exception := wc.TransactionStatusService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -62,7 +65,7 @@ GetAll
func (wc *TransactionStatusController) GetAll(c *gin.Context) { func (wc *TransactionStatusController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
wm, exception := wc.TransactionStatusService.GetAll(c, embed) wm, exception := wc.service.GetAll(c, embed)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -4,30 +4,33 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type TransactionTypeController struct { type TransactionTypeController struct {
TransactionTypeService *services.TransactionTypeService service *services.TransactionTypeService
} }
/* /*
NewTransactionTypeController NewTransactionTypeController
Initializes TransactionTypeController. Initializes TransactionTypeController.
Args: Args:
*services.TransactionTypeService: Transaction Type service *services.TransactionTypeService: Transaction Type service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*TransactionTypeController: Controller for "transaction-types" route interactions *TransactionTypeController: Controller for "transaction-types" route interactions
*/ */
func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.RouterGroup) *TransactionTypeController { func NewTransactionTypeController(as *services.TransactionTypeService, routeGroups *common.RouteGroups) *TransactionTypeController {
wc := new(TransactionTypeController) wc := &TransactionTypeController{
wc.TransactionTypeService = as service: as,
}
s.POST("", wc.New) routeGroups.TransactionType.POST("", wc.New)
s.GET("", wc.GetAll) routeGroups.TransactionType.GET("", wc.GetAll)
return wc return wc
} }
@@ -45,7 +48,7 @@ func (wc *TransactionTypeController) New(c *gin.Context) {
return return
} }
wm, exception := wc.TransactionTypeService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -62,7 +65,7 @@ GetAll
func (wc *TransactionTypeController) GetAll(c *gin.Context) { func (wc *TransactionTypeController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
wm, exception := wc.TransactionTypeService.GetAll(c, embed) wm, exception := wc.service.GetAll(c, embed)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -4,39 +4,42 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type TransactionController struct { type TransactionController struct {
TransactionService *services.TransactionService service *services.TransactionService
} }
/* /*
NewTransactionController NewTransactionController
Initializes TransactionController. Initializes TransactionController.
Args: Args:
*services.TransactionService: Transaction service *services.TransactionService: Transaction service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*TransactionController: Controller for "transaction" route interactions *TransactionController: Controller for "transaction" route interactions
*/ */
func NewTransactionController(as *services.TransactionService, s *gin.RouterGroup) *TransactionController { func NewTransactionController(as *services.TransactionService, routeGroups *common.RouteGroups) *TransactionController {
wc := new(TransactionController) wc := &TransactionController{
wc.TransactionService = as service: as,
}
s.POST("", wc.New) routeGroups.Transaction.POST("", wc.New)
s.GET("", wc.GetAll) routeGroups.Transaction.GET("", wc.GetAll)
s.PUT("/:id", wc.Edit) routeGroups.Transaction.PUT("/:id", wc.Edit)
s.GET("/:id", wc.Get) routeGroups.Transaction.GET("/:id", wc.Get)
bulkGroup := s.Group("bulk") bulkGroup := routeGroups.Transaction.Group("bulk")
{ {
bulkGroup.PUT("", wc.BulkEdit) bulkGroup.PUT("", wc.BulkEdit)
} }
checkGroup := s.Group("check") checkGroup := routeGroups.Transaction.Group("check")
{ {
checkGroup.GET("", wc.Check) checkGroup.GET("", wc.Check)
} }
@@ -57,7 +60,7 @@ func (wc *TransactionController) New(c *gin.Context) {
return return
} }
wm, exception := wc.TransactionService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -82,7 +85,7 @@ func (wc *TransactionController) GetAll(c *gin.Context) {
noPendingQry, _ := c.GetQuery("noPending") noPendingQry, _ := c.GetQuery("noPending")
noPending := noPendingQry != "" noPending := noPendingQry != ""
exception := wc.TransactionService.GetAll(c, body, wallet, fr, noPending) exception := wc.service.GetAll(c, body, wallet, fr, noPending)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -105,7 +108,7 @@ func (wc *TransactionController) Check(c *gin.Context) {
fr := FilteredResponse(c) fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId") wallet, _ := c.GetQuery("walletId")
exception := wc.TransactionService.Check(c, body, wallet, fr) exception := wc.service.Check(c, body, wallet, fr)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -129,7 +132,7 @@ func (wc *TransactionController) Edit(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
wm, exception := wc.TransactionService.Edit(c, body, id) wm, exception := wc.service.Edit(c, body, id)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -150,7 +153,7 @@ func (wc *TransactionController) BulkEdit(c *gin.Context) {
return return
} }
wm, exception := wc.TransactionService.BulkEdit(c, body) wm, exception := wc.service.BulkEdit(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -176,7 +179,7 @@ func (wc *TransactionController) Get(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
params.Embed = 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 { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -3,29 +3,32 @@ package controllers
import ( import (
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type WalletsHeaderController struct { type WalletsHeaderController struct {
WalletService *services.WalletService service *services.WalletService
} }
/* /*
NewWalletsHeaderController NewWalletsHeaderController
Initializes WalletsHeaderController. Initializes WalletsHeaderController.
Args: Args:
*services.WalletService: Wallet service *services.WalletService: Wallet service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*WalletsHeaderController: Controller for "wallet/wallet-header" route interactions *WalletsHeaderController: Controller for "wallet/wallet-header" route interactions
*/ */
func NewWalletsHeaderController(as *services.WalletService, s *gin.RouterGroup) *WalletsHeaderController { func NewWalletsHeaderController(as *services.WalletService, routeGroups *common.RouteGroups) *WalletsHeaderController {
wc := new(WalletsHeaderController) wc := &WalletsHeaderController{
wc.WalletService = as service: as,
}
s.GET("", wc.Get) routeGroups.WalletHeader.GET("", wc.Get)
return wc return wc
} }
@@ -44,7 +47,7 @@ func (wc *WalletsHeaderController) Get(c *gin.Context) {
auth := c.MustGet("auth") auth := c.MustGet("auth")
body.Id = auth.(*models.Auth).Id 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 { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -4,32 +4,35 @@ import (
"net/http" "net/http"
"wallet-api/pkg/models" "wallet-api/pkg/models"
"wallet-api/pkg/services" "wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type WalletsController struct { type WalletsController struct {
WalletService *services.WalletService service *services.WalletService
} }
/* /*
NewWalletsController NewWalletsController
Initializes WalletsController. Initializes WalletsController.
Args: Args:
*services.WalletService: Wallet service *services.WalletService: Wallet service
*gin.RouterGroup: Gin Router Group *gin.RouterGroup: Gin Router Group
Returns: Returns:
*WalletsController: Controller for "wallet" route interactions *WalletsController: Controller for "wallet" route interactions
*/ */
func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *WalletsController { func NewWalletsController(as *services.WalletService, routeGroups *common.RouteGroups) *WalletsController {
wc := new(WalletsController) wc := &WalletsController{
wc.WalletService = as service: as,
}
s.POST("", wc.New) routeGroups.Wallet.POST("", wc.New)
s.GET("", wc.GetAll) routeGroups.Wallet.GET("", wc.GetAll)
s.PUT("/:id", wc.Edit) routeGroups.Wallet.PUT("/:id", wc.Edit)
s.GET("/:id", wc.Get) routeGroups.Wallet.GET("/:id", wc.Get)
return wc return wc
} }
@@ -51,7 +54,7 @@ func (wc *WalletsController) New(c *gin.Context) {
get := c.MustGet("auth") get := c.MustGet("auth")
body.UserID = get.(*models.Auth).Id body.UserID = get.(*models.Auth).Id
wm, exception := wc.WalletService.New(c, body) wm, exception := wc.service.New(c, body)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -72,7 +75,7 @@ func (wc *WalletsController) GetAll(c *gin.Context) {
fr := FilteredResponse(c) fr := FilteredResponse(c)
exception := wc.WalletService.GetAll(c, body, fr) exception := wc.service.GetAll(c, body, fr)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -96,7 +99,7 @@ func (wc *WalletsController) Edit(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
wm, exception := wc.WalletService.Edit(c, body, id) wm, exception := wc.service.Edit(c, body, id)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return
@@ -118,7 +121,7 @@ func (wc *WalletsController) Get(c *gin.Context) {
embed, _ := c.GetQuery("embed") embed, _ := c.GetQuery("embed")
params.Embed = embed params.Embed = embed
fr, exception := wc.WalletService.Get(c, id, params) fr, exception := wc.service.Get(c, id, params)
if exception != nil { if exception != nil {
c.JSON(exception.StatusCode, exception) c.JSON(exception.StatusCode, exception)
return return

View File

@@ -9,21 +9,27 @@ import (
) )
type ApiService struct { type ApiService struct {
Db *pg.DB db *pg.DB
}
func NewApiService(db *pg.DB) *ApiService {
return &ApiService{
db: db,
}
} }
/* /*
GetFirst GetFirst
Gets first row from API table. Gets first row from API table.
Args: Args:
context.Context: Application context context.Context: Application context
Returns: Returns:
models.ApiModel: Api object from database. models.ApiModel: Api object from database.
*/ */
func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel { func (as ApiService) GetFirst(ctx context.Context) models.ApiModel {
db := as.Db.WithContext(ctx) db := as.db.WithContext(ctx)
apiModel := models.ApiModel{Api: "Works"} apiModel := models.ApiModel{Api: "Works"}
db.Model(&apiModel).First() db.Model(&apiModel).First()
return apiModel return apiModel
@@ -33,6 +39,7 @@ func (as *ApiService) GetFirst(ctx context.Context) models.ApiModel {
PostMigrate PostMigrate
Starts database migration. Starts database migration.
Args: Args:
context.Context: Application context context.Context: Application context
string: Migration version string: Migration version
@@ -40,8 +47,8 @@ Starts database migration.
*models.MessageResponse: Message response object. *models.MessageResponse: Message response object.
*models.Exception: Exception response object. *models.Exception: Exception response object.
*/ */
func (as *ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) { func (as ApiService) PostMigrate(ctx context.Context, version string) (*models.MessageResponse, *models.Exception) {
db := as.Db.WithContext(ctx) db := as.db.WithContext(ctx)
mr := new(models.MessageResponse) mr := new(models.MessageResponse)
er := new(models.Exception) er := new(models.Exception)

View File

@@ -10,13 +10,20 @@ import (
) )
type SubscriptionTypeService struct { type SubscriptionTypeService struct {
Db *pg.DB db *pg.DB
}
func NewSubscriptionTypeService(db *pg.DB) *SubscriptionTypeService {
return &SubscriptionTypeService{
db: db,
}
} }
/* /*
New New
Inserts new row to subscription type table. Inserts new row to subscription type table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewSubscriptionTypeBody: Values to create new row *models.NewSubscriptionTypeBody: Values to create new row
@@ -25,7 +32,7 @@ Inserts new row to subscription type table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubscriptionTypeBody) (*models.SubscriptionType, *models.Exception) { 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) tm := new(models.SubscriptionType)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -49,6 +56,7 @@ func (as *SubscriptionTypeService) New(ctx context.Context, body *models.NewSubs
GetAll GetAll
Gets all rows from subscription type table. Gets all rows from subscription type table.
Args: Args:
context.Context: Application context context.Context: Application context
string: Relations to embed string: Relations to embed
@@ -57,7 +65,7 @@ Gets all rows from subscription type table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionTypeService) GetAll(ctx context.Context, embed string) (*[]models.SubscriptionType, *models.Exception) { 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) wm := new([]models.SubscriptionType)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)

View File

@@ -12,13 +12,20 @@ import (
) )
type SubscriptionService struct { type SubscriptionService struct {
Db *pg.DB db *pg.DB
}
func NewSubscriptionService(db *pg.DB) *SubscriptionService {
return &SubscriptionService{
db: db,
}
} }
/* /*
New New
Inserts new row to subscription table. Inserts new row to subscription table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewSubscriptionBody: Request body *models.NewSubscriptionBody: Request body
@@ -27,7 +34,7 @@ Inserts new row to subscription table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscriptionBody) (*models.Subscription, *models.Exception) { 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) tm := new(models.Subscription)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -69,6 +76,7 @@ func (as *SubscriptionService) New(ctx context.Context, body *models.NewSubscrip
Get Get
Gets row from subscription table by id. Gets row from subscription table by id.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication model *models.Auth: Authentication model
@@ -79,7 +87,7 @@ Gets row from subscription table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Subscription, *models.Exception) { 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) exceptionReturn := new(models.Exception)
wm := new(models.Subscription) wm := new(models.Subscription)
@@ -106,6 +114,7 @@ func (as *SubscriptionService) Get(ctx context.Context, am *models.Auth, id stri
GetAll GetAll
Gets filtered rows from subscription table. Gets filtered rows from subscription table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication object *models.Auth: Authentication object
@@ -115,7 +124,7 @@ Gets filtered rows from subscription table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception { 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) wm := new([]models.Subscription)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -144,6 +153,7 @@ func (as *SubscriptionService) GetAll(ctx context.Context, am *models.Auth, wall
Edit Edit
Updates row from subscription table by id. Updates row from subscription table by id.
Args: Args:
context.Context: Application context context.Context: Application context
*models.SubscriptionEdit: Values to edit *models.SubscriptionEdit: Values to edit
@@ -153,7 +163,7 @@ Updates row from subscription table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionService) Edit(ctx context.Context, body *models.SubscriptionEdit, id string) (*models.Subscription, *models.Exception) { 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() amount, _ := body.Amount.Float64()
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -188,6 +198,7 @@ End
Updates row in subscription table by id. Updates row in subscription table by id.
Ends subscription with current date. Ends subscription with current date.
Args: Args:
context.Context: Application context context.Context: Application context
string: id to search string: id to search
@@ -196,7 +207,7 @@ Ends subscription with current date.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subscription, *models.Exception) { 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) exceptionReturn := new(models.Exception)
tm := new(models.Subscription) tm := new(models.Subscription)
@@ -224,6 +235,7 @@ func (as *SubscriptionService) End(ctx context.Context, id string) (*models.Subs
SubToTrans SubToTrans
Generates and Inserts new Transaction rows from the subscription model. Generates and Inserts new Transaction rows from the subscription model.
Args: Args:
*models.Subscription: Subscription model to generate new transactions from *models.Subscription: Subscription model to generate new transactions from
*pg.Tx: Postgres query context *pg.Tx: Postgres query context

View File

@@ -10,13 +10,20 @@ import (
) )
type TransactionStatusService struct { type TransactionStatusService struct {
Db *pg.DB db *pg.DB
}
func NewTransactionStatusService(db *pg.DB) *TransactionStatusService {
return &TransactionStatusService{
db: db,
}
} }
/* /*
New New
Inserts new row to transaction status table. Inserts new row to transaction status table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewTransactionStatusBody: object to create *models.NewTransactionStatusBody: object to create
@@ -25,7 +32,7 @@ Inserts new row to transaction status table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTransactionStatusBody) (*models.TransactionStatus, *models.Exception) { 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) tm := new(models.TransactionStatus)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -49,6 +56,7 @@ func (as *TransactionStatusService) New(ctx context.Context, body *models.NewTra
GetAll GetAll
Gets all rows from transaction status table. Gets all rows from transaction status table.
Args: Args:
context.Context: Application context context.Context: Application context
string: Relations to embed string: Relations to embed
@@ -57,7 +65,7 @@ Gets all rows from transaction status table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionStatusService) GetAll(ctx context.Context, embed string) (*[]models.TransactionStatus, *models.Exception) { 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) wm := new([]models.TransactionStatus)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)

View File

@@ -10,13 +10,20 @@ import (
) )
type TransactionTypeService struct { type TransactionTypeService struct {
Db *pg.DB db *pg.DB
}
func NewTransactionTypeService(db *pg.DB) *TransactionTypeService {
return &TransactionTypeService{
db: db,
}
} }
/* /*
New New
Inserts new row to transaction type table. Inserts new row to transaction type table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewTransactionTypeBody: object to create *models.NewTransactionTypeBody: object to create
@@ -25,7 +32,7 @@ Inserts new row to transaction type table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTransactionTypeBody) (*models.TransactionType, *models.Exception) { 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) tm := new(models.TransactionType)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -49,6 +56,7 @@ func (as *TransactionTypeService) New(ctx context.Context, body *models.NewTrans
GetAll GetAll
Gets all rows from transaction type table. Gets all rows from transaction type table.
Args: Args:
context.Context: Application context context.Context: Application context
string: Relations to embed string: Relations to embed
@@ -57,7 +65,7 @@ Gets all rows from transaction type table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionTypeService) GetAll(ctx context.Context, embed string) (*[]models.TransactionType, *models.Exception) { 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) wm := new([]models.TransactionType)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)

View File

@@ -12,14 +12,22 @@ import (
) )
type TransactionService struct { type TransactionService struct {
Db *pg.DB db *pg.DB
Ss *SubscriptionService 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 Inserts
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewTransactionBody: Transaction body object *models.NewTransactionBody: Transaction body object
@@ -28,7 +36,7 @@ Inserts
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionService) New(ctx context.Context, body *models.NewTransactionBody) (*models.Transaction, *models.Exception) { 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) exceptionReturn := new(models.Exception)
tm := new(models.Transaction) tm := new(models.Transaction)
@@ -83,7 +91,7 @@ Gets all rows from subscription type table.
*/ */
// Gets filtered rows from transaction 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 { 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) exceptionReturn := new(models.Exception)
wm := new([]models.Transaction) wm := new([]models.Transaction)
@@ -132,7 +140,7 @@ Checks subscriptions and create transacitons.
*/ */
// Gets filtered rows from transaction table. // Gets filtered rows from transaction table.
func (as *TransactionService) Check(ctx context.Context, am *models.Auth, walletId string, filtered *models.FilteredResponse) *models.Exception { 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) wm := new([]models.Transaction)
sm := new([]models.Subscription) sm := new([]models.Subscription)
@@ -157,7 +165,7 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
for _, sub := range *sm { for _, sub := range *sm {
if sub.HasNew() { if sub.HasNew() {
as.Ss.SubToTrans(&sub, tx) as.subscriptionService.SubToTrans(&sub, tx)
} }
} }
@@ -183,6 +191,7 @@ func (as *TransactionService) Check(ctx context.Context, am *models.Auth, wallet
Edit Edit
Updates row in transaction table by id. Updates row in transaction table by id.
Args: Args:
context.Context: Application context context.Context: Application context
*models.TransactionEdit: Object to edit *models.TransactionEdit: Object to edit
@@ -192,7 +201,7 @@ Updates row in transaction table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionService) Edit(ctx context.Context, body *models.TransactionEdit, id string) (*models.Transaction, *models.Exception) { 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() amount, _ := body.Amount.Float64()
@@ -236,6 +245,7 @@ func (as *TransactionService) Edit(ctx context.Context, body *models.Transaction
Bulk Edit Bulk Edit
Updates row in transaction table by id. Updates row in transaction table by id.
Args: Args:
context.Context: Application context context.Context: Application context
?[]models.Transaction Bulk Edit: Object to edit ?[]models.Transaction Bulk Edit: Object to edit
@@ -245,7 +255,7 @@ Updates row in transaction table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.TransactionEdit) (*[]models.Transaction, *models.Exception) { 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() tx, _ := db.Begin()
defer tx.Rollback() defer tx.Rollback()
@@ -284,6 +294,7 @@ func (as *TransactionService) BulkEdit(ctx context.Context, body *[]models.Trans
Get Get
Gets row from transaction table by id. Gets row from transaction table by id.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication object *models.Auth: Authentication object
@@ -294,7 +305,7 @@ Gets row from transaction table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *TransactionService) Get(ctx context.Context, am *models.Auth, id string, params *models.Params) (*models.Transaction, *models.Exception) { 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) exceptionReturn := new(models.Exception)
wm := new(models.Transaction) wm := new(models.Transaction)

View File

@@ -15,13 +15,20 @@ import (
) )
type UsersService struct { type UsersService struct {
Db *pg.DB db *pg.DB
}
func NewUsersService(db *pg.DB) *UsersService {
return &UsersService{
db: db,
}
} }
/* /*
Create Create
Inserts new row to users table. Inserts new row to users table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.User: User object to create *models.User: User object to create
@@ -30,7 +37,7 @@ Inserts new row to users table.
*models.Exception *models.Exception
*/ */
func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (*models.User, *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) check := new(models.User)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -67,6 +74,7 @@ func (us *UsersService) Create(ctx context.Context, registerBody *models.User) (
Login Login
Gets row from users table by email and valid password. Gets row from users table by email and valid password.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Login: object to search *models.Login: object to search
@@ -75,7 +83,7 @@ Gets row from users table by email and valid password.
*models.Exception *models.Exception
*/ */
func (us *UsersService) Login(ctx context.Context, loginBody *models.Login) (*models.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) check := new(models.User)
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
@@ -123,6 +131,7 @@ Deactivate
Updates row in users table. Updates row in users table.
IsActive column is set to false IsActive column is set to false
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication object *models.Auth: Authentication object
@@ -131,7 +140,7 @@ IsActive column is set to false
*models.Exception *models.Exception
*/ */
func (us *UsersService) Deactivate(ctx context.Context, auth *models.Auth) (*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) mm := new(models.MessageResponse)
me := new(models.Exception) me := new(models.Exception)
@@ -171,6 +180,7 @@ CreateToken
Generates new jwt token. Generates new jwt token.
It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours. It encodes the user id. Based on rememberMe it is valid through 48hours or 2hours.
Args: Args:
*models.User: User object to encode *models.User: User object to encode
bool: Should function generate longer lasting token (48hrs) bool: Should function generate longer lasting token (48hrs)

View File

@@ -11,14 +11,22 @@ import (
) )
type WalletService struct { type WalletService struct {
Db *pg.DB db *pg.DB
Ss *SubscriptionService subscriptionService *SubscriptionService
}
func NewWalletService(db *pg.DB, ss *SubscriptionService) *WalletService {
return &WalletService{
db: db,
subscriptionService: ss,
}
} }
/* /*
New New
Inserts row to wallets table. Inserts row to wallets table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.NewWalletBody: Object to be inserted *models.NewWalletBody: Object to be inserted
@@ -27,7 +35,7 @@ Inserts row to wallets table.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*models.Wallet, *models.Exception) { 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) exceptionReturn := new(models.Exception)
walletModel := new(models.Wallet) walletModel := new(models.Wallet)
@@ -48,6 +56,7 @@ func (as *WalletService) New(ctx context.Context, am *models.NewWalletBody) (*mo
Edit Edit
Updates row in wallets table by id. Updates row in wallets table by id.
Args: Args:
context.Context: Application context context.Context: Application context
*models.WalletEdit: Object to be edited *models.WalletEdit: Object to be edited
@@ -57,7 +66,7 @@ Updates row in wallets table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id string) (*models.Wallet, *models.Exception) { 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) exceptionReturn := new(models.Exception)
tm := new(models.Wallet) tm := new(models.Wallet)
@@ -84,6 +93,7 @@ func (as *WalletService) Edit(ctx context.Context, body *models.WalletEdit, id s
Get Get
Gets row in wallets table by id. Gets row in wallets table by id.
Args: Args:
context.Context: Application context context.Context: Application context
string: id to search string: id to search
@@ -93,7 +103,7 @@ Gets row in wallets table by id.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *WalletService) Get(ctx context.Context, id string, params *models.Params) (*models.Wallet, *models.Exception) { 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) exceptionReturn := new(models.Exception)
wm := new(models.Wallet) wm := new(models.Wallet)
@@ -120,6 +130,7 @@ func (as *WalletService) Get(ctx context.Context, id string, params *models.Para
GetAll GetAll
Gets filtered rows from wallets table. Gets filtered rows from wallets table.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication object *models.Auth: Authentication object
@@ -129,7 +140,7 @@ Gets filtered rows from wallets table.
*/ */
func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) *models.Exception { func (as *WalletService) GetAll(ctx context.Context, am *models.Auth, filtered *models.FilteredResponse) *models.Exception {
exceptionReturn := new(models.Exception) exceptionReturn := new(models.Exception)
db := as.Db.WithContext(ctx) db := as.db.WithContext(ctx)
wm := new([]models.Wallet) wm := new([]models.Wallet)
query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id) query := db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
@@ -149,6 +160,7 @@ GetHeader
Gets row from wallets table. Gets row from wallets table.
Calculates previous month, current and next month totals. Calculates previous month, current and next month totals.
Args: Args:
context.Context: Application context context.Context: Application context
*models.Auth: Authentication object *models.Auth: Authentication object
@@ -158,7 +170,7 @@ Calculates previous month, current and next month totals.
*models.Exception: Exception payload. *models.Exception: Exception payload.
*/ */
func (as *WalletService) GetHeader(ctx context.Context, am *models.Auth, walletId string) (*models.WalletHeader, *models.Exception) { 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) wm := new(models.WalletHeader)
wallets := new([]models.WalletTransactions) wallets := new([]models.WalletTransactions)
@@ -286,6 +298,7 @@ addWhere
Appends Transaction to the belonging walletId. Appends Transaction to the belonging walletId.
If missing, it creates the item list. If missing, it creates the item list.
Args: Args:
*[]models.WalletTransactions: list to append to *[]models.WalletTransactions: list to append to
string: wallet id to check string: wallet id to check

View File

@@ -1,6 +1,7 @@
package common package common
import ( import (
"github.com/gin-gonic/gin"
"log" "log"
"net" "net"
"os" "os"
@@ -8,6 +9,18 @@ import (
"strings" "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) { func CheckError(err error) {
if err != nil { if err != nil {
log.Printf("Error occured. %v", err) log.Printf("Error occured. %v", err)