Merge branch 'feature/WA-10-transactions'

This commit is contained in:
Fran Jurmanović
2021-05-16 17:44:14 +02:00
23 changed files with 315 additions and 68 deletions

View File

@@ -17,13 +17,19 @@ func Routes(s *gin.Engine, db *pg.DB) {
register := ver.Group("register")
login := ver.Group("login")
wallet := ver.Group("wallet", middleware.Auth)
transaction := ver.Group("transaction", middleware.Auth)
transactionType := ver.Group("transaction-type", middleware.Auth)
apiService := services.ApiService{Db: db}
usersService := services.UsersService{Db: db}
walletService := services.WalletService{Db: db}
transactionService := services.TransactionService{Db: db}
transactionTypeService := services.TransactionTypeService{Db: db}
controllers.NewApiController(&apiService, api)
controllers.NewRegisterController(&usersService, register)
controllers.NewLoginController(&usersService, login)
controllers.NewWalletsController(&walletService, wallet)
controllers.NewTransactionController(&transactionService, transaction)
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
}

View File

@@ -22,7 +22,7 @@ func NewLoginController(rs *services.UsersService, s *gin.RouterGroup) *LoginCon
}
func (rc *LoginController) Post(c *gin.Context) {
body := new(models.LoginModel)
body := new(models.Login)
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return

View File

@@ -22,9 +22,9 @@ func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *Regis
}
func (rc *RegisterController) Post(c *gin.Context) {
body := new(models.UserModel)
body := new(models.User)
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()})
return
}

View 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)
}

View 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)
}

View File

@@ -1,6 +1,7 @@
package controllers
import (
"net/http"
"wallet-api/pkg/models"
"wallet-api/pkg/services"
@@ -22,21 +23,26 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle
}
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")
body.Id = get.(*models.AuthModel).Id
body.UserID = get.(*models.Auth).Id
wm := wc.WalletService.New(body)
c.JSON(200, wm)
}
func (wc *WalletsController) Get(c *gin.Context) {
body := new(models.AuthModel)
body := new(models.Auth)
embed, _ := c.GetQuery("embed")
auth := c.MustGet("auth")
body.Id = auth.(*models.AuthModel).Id
body.Id = auth.(*models.Auth).Id
wm := wc.WalletService.Get(body, embed)

View File

@@ -13,7 +13,7 @@ import (
)
func Auth(c *gin.Context) {
exceptionReturn := new(models.ExceptionModel)
exceptionReturn := new(models.Exception)
tokenString := ExtractToken(c)
secret := os.Getenv("ACCESS_SECRET")
if secret == "" {
@@ -37,7 +37,7 @@ func Auth(c *gin.Context) {
if ok && token.Valid {
userId, _ := claims["id"].(string)
authModel := new(models.AuthModel)
authModel := new(models.Auth)
authModel.Id = userId
c.Set("auth", authModel)

View File

@@ -10,9 +10,12 @@ func Start(conn *pg.DB) {
apiMigration := migrations.ApiMigration{Db: conn}
usersMigration := migrations.UsersMigration{Db: conn}
walletsMigration := migrations.WalletsMigration{Db: conn}
transactionTypesMigration := migrations.TransactionTypesMigration{Db: conn}
transactionsMigration := migrations.TransactionsMigration{Db: conn}
apiMigration.Create()
usersMigration.Create()
walletsMigration.Create()
walletsMigration.PopulateTypes()
transactionTypesMigration.Create()
transactionsMigration.Create()
}

View 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")
}
}
}

View 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")
}
}
}

View File

@@ -15,7 +15,7 @@ type UsersMigration struct {
func (am *UsersMigration) Create() {
models := []interface{}{
(*models.UserModel)(nil),
(*models.User)(nil),
}
for _, model := range models {

View File

@@ -15,8 +15,7 @@ type WalletsMigration struct {
func (am *WalletsMigration) Create() {
models := []interface{}{
(*models.WalletTypeModel)(nil),
(*models.WalletModel)(nil),
(*models.Wallet)(nil),
}
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()
}

View File

@@ -1,14 +1,14 @@
package models
type TokenModel struct {
type Token struct {
Token string `json:"token"`
}
type LoginModel struct {
type Login struct {
Email string
Password string
}
type AuthModel struct {
type Auth struct {
Id string
}

View File

@@ -6,13 +6,13 @@ import (
"github.com/google/uuid"
)
type CommonModel struct {
type BaseModel struct {
Id string `json:"id" pg:"id,pk"`
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
DateCreated time.Time `json:"dateCreated" pg:"date_created"`
DateUpdated time.Time `json:"dateUpdated" pg:"date_updated"`
}
func (cm *CommonModel) Init() {
func (cm *BaseModel) Init() {
date := time.Now()
cm.Id = uuid.NewString()
cm.DateCreated = date

View File

@@ -1,6 +1,6 @@
package models
type ExceptionModel struct {
type Exception struct {
ErrorCode string `json:"errorCode"`
Message string `json:"message"`
StatusCode int `json:"statusCode"`

View File

@@ -1,23 +1,23 @@
package models
type UserModel struct {
type User struct {
tableName struct{} `pg:"users,alias:users"`
CommonModel
BaseModel
Username string `json:"username" pg:"username"`
Password string `json:"password" pg:"password"`
Email string `json:"email" pg:"email"`
}
type UserReturnInfoModel struct {
type UserReturnInfo struct {
tableName struct{} `pg:"users,alias:users"`
CommonModel
BaseModel
Username string `json:"username"`
Email string `json:"email"`
}
func (um *UserModel) Payload() *UserReturnInfoModel {
payload := new(UserReturnInfoModel)
payload.CommonModel = um.CommonModel
func (um *User) Payload() *UserReturnInfo {
payload := new(UserReturnInfo)
payload.BaseModel = um.BaseModel
payload.Username = um.Username
payload.Email = um.Email

View 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"`
}

View 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"`
}

View File

@@ -1,16 +1,14 @@
package models
type WalletModel struct {
type Wallet struct {
tableName struct{} `pg:"wallets,alias:wallets"`
CommonModel
WalletTypeID string `json:"walletTypeId" pg:"wallet_type_id"`
WalletType *WalletTypeModel `json:"walletType" pg:"rel:has-one,fk:wallet_type_id"`
BaseModel
Name string `json:"name" pg:"name"`
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 {
tableName struct{} `pg:"walletTypes,alias:walletTypes"`
CommonModel
type NewWalletBody struct {
Name string `json:"name"`
UserID string `json:"userId"`
}

View 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
}

View 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
}

View File

@@ -17,9 +17,9 @@ type UsersService struct {
Db *pg.DB
}
func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserModel, *models.ExceptionModel) {
check := new(models.UserModel)
exceptionReturn := new(models.ExceptionModel)
func (us *UsersService) Create(registerBody *models.User) (*models.User, *models.Exception) {
check := new(models.User)
exceptionReturn := new(models.Exception)
us.Db.Model(check).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
if check.Username != "" || check.Email != "" {
@@ -44,10 +44,10 @@ func (us *UsersService) Create(registerBody *models.UserModel) (*models.UserMode
return registerBody, exceptionReturn
}
func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel, *models.ExceptionModel) {
check := new(models.UserModel)
exceptionReturn := new(models.ExceptionModel)
tokenPayload := new(models.TokenModel)
func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.Exception) {
check := new(models.User)
exceptionReturn := new(models.Exception)
tokenPayload := new(models.Token)
us.Db.Model(check).Where("? = ?", pg.Ident("email"), loginBody.Email).Select()
if check.Email == "" {
@@ -72,7 +72,7 @@ func (us *UsersService) Login(loginBody *models.LoginModel) (*models.TokenModel,
return tokenPayload, exceptionReturn
}
func CreateToken(user *models.UserModel) (string, error) {
func CreateToken(user *models.User) (string, error) {
atClaims := jwt.MapClaims{}
atClaims["authorized"] = true
atClaims["id"] = user.Id

View File

@@ -11,30 +11,21 @@ type WalletService struct {
Db *pg.DB
}
func (as *WalletService) New(am *models.AuthModel) *models.WalletModel {
walletType := as.GetType()
func (as *WalletService) New(am *models.NewWalletBody) *models.Wallet {
walletModel := new(models.WalletModel)
walletModel := new(models.Wallet)
walletModel.Init()
walletModel.UserID = am.Id
walletModel.WalletTypeID = walletType.Id
walletModel.UserID = am.UserID
walletModel.Name = am.Name
as.Db.Model(walletModel).Insert()
return walletModel
}
func (as *WalletService) Get(am *models.AuthModel, embed string) *models.WalletModel {
wm := new(models.WalletModel)
func (as *WalletService) Get(am *models.Auth, embed string) *models.Wallet {
wm := new(models.Wallet)
query := as.Db.Model(wm).Where("? = ?", pg.Ident("user_id"), am.Id)
common.GenerateEmbed(query, embed).Select()
return wm
}
func (as *WalletService) GetType() *models.WalletTypeModel {
wt := new(models.WalletTypeModel)
as.Db.Model(wt).Select()
return wt
}