partial repository layer added

This commit is contained in:
Fran Jurmanović
2022-09-27 00:33:44 +02:00
parent 13ce0735d0
commit 82e97fc97f
73 changed files with 2686 additions and 1216 deletions

66
pkg/controller/api.go Normal file
View File

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

View File

@@ -0,0 +1,70 @@
package controller
import (
"fmt"
"go.uber.org/dig"
"strconv"
"strings"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
/*
InitializeControllers
Initializes Dependency Injection modules and registers controllers
Args:
*dig.Container: Dig Container
*/
func InitializeControllers(c *dig.Container) {
service.InitializeServices(c)
c.Invoke(NewApiController)
c.Invoke(NewUserController)
c.Invoke(NewWalletController)
c.Invoke(NewWalletHeaderController)
c.Invoke(NewTransactionController)
c.Invoke(NewTransactionStatusController)
c.Invoke(NewTransactionTypeController)
c.Invoke(NewSubscriptionController)
c.Invoke(NewSubscriptionTypeController)
}
/*
FilteredResponse
Gets query parameters and populates FilteredResponse model.
Args:
*gin.Context: Gin Application Context
Returns:
*model.FilteredResponse: Filtered response
*/
func FilteredResponse(c *gin.Context) *model.FilteredResponse {
filtered := new(model.FilteredResponse)
page, _ := c.GetQuery("page")
rpp, _ := c.GetQuery("rpp")
sortBy, _ := c.GetQuery("sortBy")
dividers := [5]string{"|", " ", ".", "/", ","}
for _, div := range dividers {
sortArr := strings.Split(sortBy, div)
if len(sortArr) >= 2 {
sortBy = fmt.Sprintf("%s %s", common.ToSnakeCase(sortArr[0]), strings.ToUpper(sortArr[1]))
}
}
filtered.Embed, _ = c.GetQuery("embed")
filtered.Page, _ = strconv.Atoi(page)
filtered.Rpp, _ = strconv.Atoi(rpp)
filtered.SortBy = sortBy
return filtered
}

View File

@@ -0,0 +1,169 @@
package controller
import (
"net/http"
"wallet-api/pkg/filter"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type SubscriptionController struct {
service *service.SubscriptionService
}
/*
NewSubscriptionController
Initializes SubscriptionController.
Args:
*services.SubscriptionService: Subscription service
*gin.RouterGroup: Gin Router Group
Returns:
*SubscriptionController: Controller for "subscription" route interactions
*/
func NewSubscriptionController(as *service.SubscriptionService, routeGroups *common.RouteGroups) *SubscriptionController {
wc := &SubscriptionController{
service: as,
}
routeGroups.Subscription.POST("", wc.New)
routeGroups.Subscription.PUT("/:id", wc.Edit)
routeGroups.Subscription.GET("/:id", wc.Get)
routeGroups.Subscription.GET("", wc.GetAll)
se := routeGroups.Subscription.Group("/end")
{
se.PUT("/:id", wc.End)
}
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /subscription)
func (wc *SubscriptionController) New(c *gin.Context) {
body := new(model.NewSubscriptionBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
Edit
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (PUT /subscription/:id)
func (wc *SubscriptionController) Edit(c *gin.Context) {
body := new(model.SubscriptionEdit)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id := c.Param("id")
wm, exception := wc.service.Edit(c, body, id)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
Get
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /subscription/:id)
func (wc *SubscriptionController) Get(c *gin.Context) {
body := new(model.Auth)
params := new(model.Params)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
id := c.Param("id")
embed, _ := c.GetQuery("embed")
params.Embed = embed
flt := filter.NewSubscriptionFilter(*params)
flt.Id = id
fr, exception := wc.service.Get(c, body, *flt)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}
// ROUTE (PUT /subscription/end/:id)
func (wc *SubscriptionController) End(c *gin.Context) {
body := new(model.Auth)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
end := new(model.SubscriptionEnd)
if err := c.ShouldBind(end); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id := c.Param("id")
fr, exception := wc.service.End(c, id)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /subscription)
func (wc *SubscriptionController) GetAll(c *gin.Context) {
auth := c.MustGet("auth")
fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId")
flt := filter.NewSubscriptionFilter(model.Params{})
flt.WalletId = wallet
flt.Id = auth.(*model.Auth).Id
exception := wc.service.GetAll(c, flt, fr)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}

View File

@@ -0,0 +1,74 @@
package controller
import (
"net/http"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type SubscriptionTypeController struct {
service *service.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 *service.SubscriptionTypeService, routeGroups *common.RouteGroups) *SubscriptionTypeController {
wc := &SubscriptionTypeController{
service: as,
}
routeGroups.SubscriptionType.POST("", wc.New)
routeGroups.SubscriptionType.GET("", wc.GetAll)
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /subscription-types)
func (wc *SubscriptionTypeController) New(c *gin.Context) {
body := new(model.NewSubscriptionTypeBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /subscription-types)
func (wc *SubscriptionTypeController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed")
wm, exception := wc.service.GetAll(c, embed)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}

View File

@@ -0,0 +1,189 @@
package controller
import (
"net/http"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type TransactionController struct {
service *service.TransactionService
}
/*
NewTransactionController
Initializes TransactionController.
Args:
*services.TransactionService: Transaction service
*gin.RouterGroup: Gin Router Group
Returns:
*TransactionController: Controller for "transaction" route interactions
*/
func NewTransactionController(as *service.TransactionService, routeGroups *common.RouteGroups) *TransactionController {
wc := &TransactionController{
service: as,
}
routeGroups.Transaction.POST("", wc.New)
routeGroups.Transaction.GET("", wc.GetAll)
routeGroups.Transaction.PUT("/:id", wc.Edit)
routeGroups.Transaction.GET("/:id", wc.Get)
bulkGroup := routeGroups.Transaction.Group("bulk")
{
bulkGroup.PUT("", wc.BulkEdit)
}
checkGroup := routeGroups.Transaction.Group("check")
{
checkGroup.GET("", wc.Check)
}
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /transactions)
func (wc *TransactionController) New(c *gin.Context) {
body := new(model.NewTransactionBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /transactions)
func (wc *TransactionController) GetAll(c *gin.Context) {
body := new(model.Auth)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId")
noPendingQry, _ := c.GetQuery("noPending")
noPending := noPendingQry != ""
exception := wc.service.GetAll(c, body, wallet, fr, noPending)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}
/*
Check
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /transactions)
func (wc *TransactionController) Check(c *gin.Context) {
body := new(model.Auth)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
fr := FilteredResponse(c)
wallet, _ := c.GetQuery("walletId")
exception := wc.service.Check(c, body, wallet, fr)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}
/*
Edit
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (PUT /transactions/:id)
func (wc *TransactionController) Edit(c *gin.Context) {
body := new(model.TransactionEdit)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id := c.Param("id")
wm, exception := wc.service.Edit(c, body, id)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
BulkEdit
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (PUT /transactions/:id)
func (wc *TransactionController) BulkEdit(c *gin.Context) {
body := new([]model.TransactionEdit)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.BulkEdit(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
Get
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /transactions/:id)
func (wc *TransactionController) Get(c *gin.Context) {
body := new(model.Auth)
params := new(model.Params)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
id := c.Param("id")
embed, _ := c.GetQuery("embed")
params.Embed = embed
fr, exception := wc.service.Get(c, body, id, params)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}

View File

@@ -0,0 +1,75 @@
package controller
import (
"net/http"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type TransactionStatusController struct {
service *service.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 *service.TransactionStatusService, routeGroups *common.RouteGroups) *TransactionStatusController {
wc := &TransactionStatusController{
service: as,
}
routeGroups.TransactionStatus.POST("", wc.New)
routeGroups.TransactionStatus.GET("", wc.GetAll)
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /transaction-status)
func (wc *TransactionStatusController) New(c *gin.Context) {
body := new(model.NewTransactionStatusBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /transaction-status)
func (wc *TransactionStatusController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed")
wm, exception := wc.service.GetAll(c, embed)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}

View File

@@ -0,0 +1,75 @@
package controller
import (
"net/http"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type TransactionTypeController struct {
service *service.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 *service.TransactionTypeService, routeGroups *common.RouteGroups) *TransactionTypeController {
wc := &TransactionTypeController{
service: as,
}
routeGroups.TransactionType.POST("", wc.New)
routeGroups.TransactionType.GET("", wc.GetAll)
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /transaction-types)
func (wc *TransactionTypeController) New(c *gin.Context) {
body := new(model.NewTransactionTypeBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /transaction-types)
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
embed, _ := c.GetQuery("embed")
wm, exception := wc.service.GetAll(c, embed)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}

129
pkg/controller/user.go Normal file
View File

@@ -0,0 +1,129 @@
package controller
import (
"net/http"
"wallet-api/pkg/middleware"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type UserController struct {
service *service.UserService
}
/*
NewUserController
Initializes UserController.
Args:
*services.UserService: User service
*gin.RouterGroup: Gin Router Group
Returns:
*UserController: Controller for "auth" interactions
*/
func NewUserController(rs *service.UserService, routeGroups *common.RouteGroups) *UserController {
rc := &UserController{
service: rs,
}
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
}
/*
PostLogin
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /auth/login).
func (rc *UserController) PostLogin(c *gin.Context) {
body := new(model.Login)
if err := c.ShouldBind(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
returnedUser, exceptionReturn := rc.service.Login(c, body)
if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
return
} else {
c.JSON(200, returnedUser)
}
}
/*
PostRegister
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /auth/register).
func (rc *UserController) PostRegister(c *gin.Context) {
body := new(model.User)
body.Init()
body.IsActive = true
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
returnedUser, exceptionReturn := rc.service.Create(c, body)
if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
return
} else {
c.JSON(200, returnedUser.Payload())
}
}
/*
Delete
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (DELETE /auth/deactivate).
func (rc *UserController) Delete(c *gin.Context) {
auth := new(model.Auth)
authGet := c.MustGet("auth")
auth.Id = authGet.(*model.Auth).Id
mr, er := rc.service.Deactivate(c, auth)
if er.Message != "" {
c.JSON(er.StatusCode, er)
return
} else {
c.JSON(200, mr)
}
}
/*
CheckToken
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /auth/check-token).
func (rc *UserController) CheckToken(c *gin.Context) {
token, _ := c.GetQuery("token")
re := new(model.CheckToken)
_, err := middleware.CheckToken(token)
if err != nil {
re.Valid = false
c.AbortWithStatusJSON(400, re)
return
}
re.Valid = true
c.JSON(200, re)
}

View File

@@ -0,0 +1,57 @@
package controller
import (
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type WalletHeaderController struct {
service *service.WalletService
}
/*
NewWalletHeaderController
Initializes WalletHeaderController.
Args:
*services.WalletService: Wallet service
*gin.RouterGroup: Gin Router Group
Returns:
*WalletHeaderController: Controller for "wallet/wallet-header" route interactions
*/
func NewWalletHeaderController(as *service.WalletService, routeGroups *common.RouteGroups) *WalletHeaderController {
wc := &WalletHeaderController{
service: as,
}
routeGroups.WalletHeader.GET("", wc.Get)
return wc
}
/*
Get
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /wallet/wallet-header)
func (wc *WalletHeaderController) Get(c *gin.Context) {
body := new(model.Auth)
walletId, _ := c.GetQuery("walletId")
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
wm, exception := wc.service.GetHeader(c, body, walletId)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}

131
pkg/controller/wallet.go Normal file
View File

@@ -0,0 +1,131 @@
package controller
import (
"net/http"
"wallet-api/pkg/model"
"wallet-api/pkg/service"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
type WalletController struct {
service *service.WalletService
}
/*
NewWalletController
Initializes WalletController.
Args:
*services.WalletService: Wallet service
*gin.RouterGroup: Gin Router Group
Returns:
*WalletController: Controller for "wallet" route interactions
*/
func NewWalletController(as *service.WalletService, routeGroups *common.RouteGroups) *WalletController {
wc := &WalletController{
service: as,
}
routeGroups.Wallet.POST("", wc.New)
routeGroups.Wallet.GET("", wc.GetAll)
routeGroups.Wallet.PUT("/:id", wc.Edit)
routeGroups.Wallet.GET("/:id", wc.Get)
return wc
}
/*
New
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (POST /wallet)
func (wc *WalletController) New(c *gin.Context) {
body := new(model.NewWalletBody)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
get := c.MustGet("auth")
body.UserID = get.(*model.Auth).Id
wm, exception := wc.service.New(c, body)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
GetAll
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /wallet)
func (wc *WalletController) GetAll(c *gin.Context) {
body := new(model.Auth)
auth := c.MustGet("auth")
body.Id = auth.(*model.Auth).Id
fr := FilteredResponse(c)
exception := wc.service.GetAll(c, body, fr)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}
/*
Edit
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (PUT /wallet/:id)
func (wc *WalletController) Edit(c *gin.Context) {
body := new(model.WalletEdit)
if err := c.ShouldBind(body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id := c.Param("id")
wm, exception := wc.service.Edit(c, body, id)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, wm)
}
/*
Get
Args:
*gin.Context: Gin Application Context
*/
// ROUTE (GET /wallet/:id)
func (wc *WalletController) Get(c *gin.Context) {
params := new(model.Params)
id := c.Param("id")
embed, _ := c.GetQuery("embed")
params.Embed = embed
fr, exception := wc.service.Get(c, id, params)
if exception != nil {
c.JSON(exception.StatusCode, exception)
return
}
c.JSON(200, fr)
}