mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
Merge branch 'feature/WA-10-transactions'
This commit is contained in:
@@ -17,13 +17,19 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
|||||||
register := ver.Group("register")
|
register := ver.Group("register")
|
||||||
login := ver.Group("login")
|
login := ver.Group("login")
|
||||||
wallet := ver.Group("wallet", middleware.Auth)
|
wallet := ver.Group("wallet", middleware.Auth)
|
||||||
|
transaction := ver.Group("transaction", middleware.Auth)
|
||||||
|
transactionType := ver.Group("transaction-type", middleware.Auth)
|
||||||
|
|
||||||
apiService := services.ApiService{Db: db}
|
apiService := services.ApiService{Db: db}
|
||||||
usersService := services.UsersService{Db: db}
|
usersService := services.UsersService{Db: db}
|
||||||
walletService := services.WalletService{Db: db}
|
walletService := services.WalletService{Db: db}
|
||||||
|
transactionService := services.TransactionService{Db: db}
|
||||||
|
transactionTypeService := services.TransactionTypeService{Db: db}
|
||||||
|
|
||||||
controllers.NewApiController(&apiService, api)
|
controllers.NewApiController(&apiService, api)
|
||||||
controllers.NewRegisterController(&usersService, register)
|
controllers.NewRegisterController(&usersService, register)
|
||||||
controllers.NewLoginController(&usersService, login)
|
controllers.NewLoginController(&usersService, login)
|
||||||
controllers.NewWalletsController(&walletService, wallet)
|
controllers.NewWalletsController(&walletService, wallet)
|
||||||
|
controllers.NewTransactionController(&transactionService, transaction)
|
||||||
|
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func NewLoginController(rs *services.UsersService, s *gin.RouterGroup) *LoginCon
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *LoginController) Post(c *gin.Context) {
|
func (rc *LoginController) Post(c *gin.Context) {
|
||||||
body := new(models.LoginModel)
|
body := new(models.Login)
|
||||||
if err := c.ShouldBindJSON(&body); err != nil {
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -22,9 +22,9 @@ func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *Regis
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RegisterController) Post(c *gin.Context) {
|
func (rc *RegisterController) Post(c *gin.Context) {
|
||||||
body := new(models.UserModel)
|
body := new(models.User)
|
||||||
body.Init()
|
body.Init()
|
||||||
if err := c.ShouldBindJSON(&body); err != nil {
|
if err := c.ShouldBindJSON(body); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
42
pkg/controllers/transactionTypes.go
Normal file
42
pkg/controllers/transactionTypes.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/services"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionTypeController struct {
|
||||||
|
TransactionTypeService *services.TransactionTypeService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.RouterGroup) *TransactionTypeController {
|
||||||
|
wc := new(TransactionTypeController)
|
||||||
|
wc.TransactionTypeService = as
|
||||||
|
|
||||||
|
s.POST("", wc.New)
|
||||||
|
s.GET("", wc.GetAll)
|
||||||
|
|
||||||
|
return wc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *TransactionTypeController) New(c *gin.Context) {
|
||||||
|
body := new(models.NewTransactionTypeBody)
|
||||||
|
if err := c.ShouldBindJSON(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wm := wc.TransactionTypeService.New(body)
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *TransactionTypeController) GetAll(c *gin.Context) {
|
||||||
|
embed, _ := c.GetQuery("embed")
|
||||||
|
|
||||||
|
wm := wc.TransactionTypeService.GetAll(embed)
|
||||||
|
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
43
pkg/controllers/transactions.go
Normal file
43
pkg/controllers/transactions.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/services"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionController struct {
|
||||||
|
TransactionService *services.TransactionService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTransactionController(as *services.TransactionService, s *gin.RouterGroup) *TransactionController {
|
||||||
|
wc := new(TransactionController)
|
||||||
|
wc.TransactionService = as
|
||||||
|
|
||||||
|
s.POST("", wc.New)
|
||||||
|
s.GET("", wc.GetAll)
|
||||||
|
|
||||||
|
return wc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *TransactionController) New(c *gin.Context) {
|
||||||
|
body := new(models.NewTransactionBody)
|
||||||
|
if err := c.ShouldBindJSON(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wm := wc.TransactionService.New(body)
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (wc *TransactionController) GetAll(c *gin.Context) {
|
||||||
|
embed, _ := c.GetQuery("embed")
|
||||||
|
wallet, _ := c.GetQuery("walletId")
|
||||||
|
|
||||||
|
wm := wc.TransactionService.GetAll(wallet, embed)
|
||||||
|
|
||||||
|
c.JSON(200, wm)
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"wallet-api/pkg/models"
|
"wallet-api/pkg/models"
|
||||||
"wallet-api/pkg/services"
|
"wallet-api/pkg/services"
|
||||||
|
|
||||||
@@ -22,21 +23,26 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WalletsController) New(c *gin.Context) {
|
func (wc *WalletsController) New(c *gin.Context) {
|
||||||
body := new(models.AuthModel)
|
body := new(models.NewWalletBody)
|
||||||
|
|
||||||
|
if err := c.ShouldBindJSON(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
get := c.MustGet("auth")
|
get := c.MustGet("auth")
|
||||||
body.Id = get.(*models.AuthModel).Id
|
body.UserID = get.(*models.Auth).Id
|
||||||
|
|
||||||
wm := wc.WalletService.New(body)
|
wm := wc.WalletService.New(body)
|
||||||
c.JSON(200, wm)
|
c.JSON(200, wm)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WalletsController) Get(c *gin.Context) {
|
func (wc *WalletsController) Get(c *gin.Context) {
|
||||||
body := new(models.AuthModel)
|
body := new(models.Auth)
|
||||||
|
|
||||||
embed, _ := c.GetQuery("embed")
|
embed, _ := c.GetQuery("embed")
|
||||||
auth := c.MustGet("auth")
|
auth := c.MustGet("auth")
|
||||||
body.Id = auth.(*models.AuthModel).Id
|
body.Id = auth.(*models.Auth).Id
|
||||||
|
|
||||||
wm := wc.WalletService.Get(body, embed)
|
wm := wc.WalletService.Get(body, embed)
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Auth(c *gin.Context) {
|
func Auth(c *gin.Context) {
|
||||||
exceptionReturn := new(models.ExceptionModel)
|
exceptionReturn := new(models.Exception)
|
||||||
tokenString := ExtractToken(c)
|
tokenString := ExtractToken(c)
|
||||||
secret := os.Getenv("ACCESS_SECRET")
|
secret := os.Getenv("ACCESS_SECRET")
|
||||||
if secret == "" {
|
if secret == "" {
|
||||||
@@ -37,7 +37,7 @@ func Auth(c *gin.Context) {
|
|||||||
if ok && token.Valid {
|
if ok && token.Valid {
|
||||||
userId, _ := claims["id"].(string)
|
userId, _ := claims["id"].(string)
|
||||||
|
|
||||||
authModel := new(models.AuthModel)
|
authModel := new(models.Auth)
|
||||||
authModel.Id = userId
|
authModel.Id = userId
|
||||||
|
|
||||||
c.Set("auth", authModel)
|
c.Set("auth", authModel)
|
||||||
|
|||||||
@@ -10,9 +10,12 @@ func Start(conn *pg.DB) {
|
|||||||
apiMigration := migrations.ApiMigration{Db: conn}
|
apiMigration := migrations.ApiMigration{Db: conn}
|
||||||
usersMigration := migrations.UsersMigration{Db: conn}
|
usersMigration := migrations.UsersMigration{Db: conn}
|
||||||
walletsMigration := migrations.WalletsMigration{Db: conn}
|
walletsMigration := migrations.WalletsMigration{Db: conn}
|
||||||
|
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
|
||||||
|
transactionsMigration := migrations.TransactionsMigration{Db: conn}
|
||||||
|
|
||||||
apiMigration.Create()
|
apiMigration.Create()
|
||||||
usersMigration.Create()
|
usersMigration.Create()
|
||||||
walletsMigration.Create()
|
walletsMigration.Create()
|
||||||
walletsMigration.PopulateTypes()
|
transactionTypesMigration.Create()
|
||||||
|
transactionsMigration.Create()
|
||||||
}
|
}
|
||||||
|
|||||||
32
pkg/migrate/migrations/transactionTypes.go
Normal file
32
pkg/migrate/migrations/transactionTypes.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionTypesMigration struct {
|
||||||
|
Db *pg.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *TransactionTypesMigration) Create() {
|
||||||
|
models := []interface{}{
|
||||||
|
(*models.TransactionType)(nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range models {
|
||||||
|
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
|
IfNotExists: false,
|
||||||
|
FKConstraints: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Creating Table: %s", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Table created successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
pkg/migrate/migrations/transactions.go
Normal file
32
pkg/migrate/migrations/transactions.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package migrations
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
"github.com/go-pg/pg/v10/orm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionsMigration struct {
|
||||||
|
Db *pg.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (am *TransactionsMigration) Create() {
|
||||||
|
models := []interface{}{
|
||||||
|
(*models.Transaction)(nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, model := range models {
|
||||||
|
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||||
|
IfNotExists: false,
|
||||||
|
FKConstraints: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error Creating Table: %s", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("Table created successfully")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ type UsersMigration struct {
|
|||||||
|
|
||||||
func (am *UsersMigration) Create() {
|
func (am *UsersMigration) Create() {
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.UserModel)(nil),
|
(*models.User)(nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ type WalletsMigration struct {
|
|||||||
|
|
||||||
func (am *WalletsMigration) Create() {
|
func (am *WalletsMigration) Create() {
|
||||||
models := []interface{}{
|
models := []interface{}{
|
||||||
(*models.WalletTypeModel)(nil),
|
(*models.Wallet)(nil),
|
||||||
(*models.WalletModel)(nil),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
@@ -31,10 +30,3 @@ func (am *WalletsMigration) Create() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (am *WalletsMigration) PopulateTypes() {
|
|
||||||
walletTypeModel := new(models.WalletTypeModel)
|
|
||||||
walletTypeModel.Init()
|
|
||||||
walletTypeModel.Name = "Test"
|
|
||||||
am.Db.Model(walletTypeModel).Insert()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type TokenModel struct {
|
type Token struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginModel struct {
|
type Login struct {
|
||||||
Email string
|
Email string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthModel struct {
|
type Auth struct {
|
||||||
Id string
|
Id string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CommonModel struct {
|
type BaseModel struct {
|
||||||
Id string `json:"id" pg:"id,pk"`
|
Id string `json:"id" pg:"id,pk"`
|
||||||
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
DateCreated time.Time `json:"dateCreated" pg:"date_created"`
|
||||||
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
DateUpdated time.Time `json:"dateUpdated" pg:"date_updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cm *CommonModel) Init() {
|
func (cm *BaseModel) Init() {
|
||||||
date := time.Now()
|
date := time.Now()
|
||||||
cm.Id = uuid.NewString()
|
cm.Id = uuid.NewString()
|
||||||
cm.DateCreated = date
|
cm.DateCreated = date
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type ExceptionModel struct {
|
type Exception struct {
|
||||||
ErrorCode string `json:"errorCode"`
|
ErrorCode string `json:"errorCode"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
StatusCode int `json:"statusCode"`
|
StatusCode int `json:"statusCode"`
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type UserModel struct {
|
type User struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
CommonModel
|
BaseModel
|
||||||
Username string `json:"username" pg:"username"`
|
Username string `json:"username" pg:"username"`
|
||||||
Password string `json:"password" pg:"password"`
|
Password string `json:"password" pg:"password"`
|
||||||
Email string `json:"email" pg:"email"`
|
Email string `json:"email" pg:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserReturnInfoModel struct {
|
type UserReturnInfo struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
CommonModel
|
BaseModel
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (um *UserModel) Payload() *UserReturnInfoModel {
|
func (um *User) Payload() *UserReturnInfo {
|
||||||
payload := new(UserReturnInfoModel)
|
payload := new(UserReturnInfo)
|
||||||
payload.CommonModel = um.CommonModel
|
payload.BaseModel = um.BaseModel
|
||||||
payload.Username = um.Username
|
payload.Username = um.Username
|
||||||
payload.Email = um.Email
|
payload.Email = um.Email
|
||||||
|
|
||||||
|
|||||||
13
pkg/models/transactionTypes.go
Normal file
13
pkg/models/transactionTypes.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type TransactionType struct {
|
||||||
|
tableName struct{} `pg:"transactionTypes,alias:transactionTypes"`
|
||||||
|
BaseModel
|
||||||
|
Name string `json:"name" pg:"name"`
|
||||||
|
Type string `json:"type" pg:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewTransactionTypeBody struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
21
pkg/models/transactions.go
Normal file
21
pkg/models/transactions.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type Transaction struct {
|
||||||
|
tableName struct{} `pg:"transactions,alias:transactions"`
|
||||||
|
BaseModel
|
||||||
|
Description string `json:"description" pg:"description"`
|
||||||
|
TransactionTypeID string `json:"transactionTypeId", pg:"transaction_type_id"`
|
||||||
|
TransactionType *TransactionType `json:"transactionType", pg:"rel:has-one, fk:transaction_type_id"`
|
||||||
|
WalletID string `json:"walletId", pg:"wallet_id"`
|
||||||
|
Wallet *Wallet `json:"wallet" pg:"rel:has-one, fk:wallet_id"`
|
||||||
|
TransactionDate time.Time `json:"transactionDate" pg:"transaction_date"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type NewTransactionBody struct {
|
||||||
|
WalletID string `json:"walletId"`
|
||||||
|
TransactionTypeID string `json:"transactionTypeId"`
|
||||||
|
TransactionDate time.Time `json:"transactionDate"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
}
|
||||||
@@ -1,16 +1,14 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type WalletModel struct {
|
type Wallet struct {
|
||||||
tableName struct{} `pg:"wallets,alias:wallets"`
|
tableName struct{} `pg:"wallets,alias:wallets"`
|
||||||
CommonModel
|
BaseModel
|
||||||
WalletTypeID string `json:"walletTypeId" pg:"wallet_type_id"`
|
Name string `json:"name" pg:"name"`
|
||||||
WalletType *WalletTypeModel `json:"walletType" pg:"rel:has-one,fk:wallet_type_id"`
|
|
||||||
UserID string `json:"userId" pg:"user_id"`
|
UserID string `json:"userId" pg:"user_id"`
|
||||||
User *UserReturnInfoModel `json:"user" pg:"rel:has-one,fk:user_id"`
|
User *UserReturnInfo `json:"user" pg:"rel:has-one,fk:user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type WalletTypeModel struct {
|
type NewWalletBody struct {
|
||||||
tableName struct{} `pg:"walletTypes,alias:walletTypes"`
|
|
||||||
CommonModel
|
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
UserID string `json:"userId"`
|
||||||
}
|
}
|
||||||
|
|||||||
33
pkg/services/transactionTypes.go
Normal file
33
pkg/services/transactionTypes.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionTypeService struct {
|
||||||
|
Db *pg.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *TransactionTypeService) New(body *models.NewTransactionTypeBody) *models.TransactionType {
|
||||||
|
tm := new(models.TransactionType)
|
||||||
|
|
||||||
|
tm.Init()
|
||||||
|
tm.Name = body.Name
|
||||||
|
tm.Type = body.Type
|
||||||
|
|
||||||
|
as.Db.Model(tm).Insert()
|
||||||
|
|
||||||
|
return tm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *TransactionTypeService) GetAll(embed string) *[]models.TransactionType {
|
||||||
|
wm := new([]models.TransactionType)
|
||||||
|
|
||||||
|
query := as.Db.Model(wm)
|
||||||
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
|
return wm
|
||||||
|
}
|
||||||
35
pkg/services/transactions.go
Normal file
35
pkg/services/transactions.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/utl/common"
|
||||||
|
|
||||||
|
"github.com/go-pg/pg/v10"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TransactionService struct {
|
||||||
|
Db *pg.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *TransactionService) New(body *models.NewTransactionBody) *models.Transaction {
|
||||||
|
tm := new(models.Transaction)
|
||||||
|
|
||||||
|
tm.Init()
|
||||||
|
tm.WalletID = body.WalletID
|
||||||
|
tm.TransactionTypeID = body.TransactionTypeID
|
||||||
|
tm.Description = body.Description
|
||||||
|
tm.TransactionDate = body.TransactionDate
|
||||||
|
|
||||||
|
as.Db.Model(tm).Insert()
|
||||||
|
|
||||||
|
return tm
|
||||||
|
}
|
||||||
|
|
||||||
|
func (as *TransactionService) GetAll(walletId string, embed string) *[]models.Transaction {
|
||||||
|
wm := new([]models.Transaction)
|
||||||
|
|
||||||
|
query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId)
|
||||||
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
|
return wm
|
||||||
|
}
|
||||||
@@ -17,9 +17,9 @@ type UsersService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, *models.ExceptionModel) {
|
func (us *UsersService) Create(registerBody *models.User) (*models.User, *models.Exception) {
|
||||||
check := new(models.UserModel)
|
check := new(models.User)
|
||||||
exceptionReturn := new(models.ExceptionModel)
|
exceptionReturn := new(models.Exception)
|
||||||
|
|
||||||
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||||
if check.Username != "" || check.Email != "" {
|
if check.Username != "" || check.Email != "" {
|
||||||
@@ -44,10 +44,10 @@ func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserMode
|
|||||||
return registerBody, exceptionReturn
|
return registerBody, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel, *models.ExceptionModel) {
|
func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.Exception) {
|
||||||
check := new(models.UserModel)
|
check := new(models.User)
|
||||||
exceptionReturn := new(models.ExceptionModel)
|
exceptionReturn := new(models.Exception)
|
||||||
tokenPayload := new(models.TokenModel)
|
tokenPayload := new(models.Token)
|
||||||
|
|
||||||
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
|
||||||
if check.Email == "" {
|
if check.Email == "" {
|
||||||
@@ -72,7 +72,7 @@ func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel,
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateToken(user *models.UserModel) (string, error) {
|
func CreateToken(user *models.User) (string, error) {
|
||||||
atClaims := jwt.MapClaims{}
|
atClaims := jwt.MapClaims{}
|
||||||
atClaims["authorized"] = true
|
atClaims["authorized"] = true
|
||||||
atClaims["id"] = user.Id
|
atClaims["id"] = user.Id
|
||||||
|
|||||||
@@ -11,30 +11,21 @@ type WalletService struct {
|
|||||||
Db *pg.DB
|
Db *pg.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) New(am *models.AuthModel) *models.WalletModel {
|
func (as *WalletService) New(am *models.NewWalletBody) *models.Wallet {
|
||||||
walletType := as.GetType()
|
|
||||||
|
|
||||||
walletModel := new(models.WalletModel)
|
walletModel := new(models.Wallet)
|
||||||
walletModel.Init()
|
walletModel.Init()
|
||||||
walletModel.UserID = am.Id
|
walletModel.UserID = am.UserID
|
||||||
walletModel.WalletTypeID = walletType.Id
|
walletModel.Name = am.Name
|
||||||
as.Db.Model(walletModel).Insert()
|
as.Db.Model(walletModel).Insert()
|
||||||
return walletModel
|
return walletModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) Get(am *models.AuthModel, embed string) *models.WalletModel {
|
func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet {
|
||||||
wm := new(models.WalletModel)
|
wm := new(models.Wallet)
|
||||||
|
|
||||||
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
|
||||||
common.GenerateEmbed(query, embed).Select()
|
common.GenerateEmbed(query, embed).Select()
|
||||||
|
|
||||||
return wm
|
return wm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *WalletService) GetType() *models.WalletTypeModel {
|
|
||||||
wt := new(models.WalletTypeModel)
|
|
||||||
|
|
||||||
as.Db.Model(wt).Select()
|
|
||||||
|
|
||||||
return wt
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user