WA-9 - implemented wallet

This commit is contained in:
Fran Jurmanović
2021-05-16 17:43:45 +02:00
parent 32fd64716e
commit 55010c83d6
15 changed files with 64 additions and 68 deletions

View File

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

View File

@@ -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

View File

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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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()
} }

View File

@@ -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 {

View File

@@ -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()
}

View File

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

View File

@@ -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

View File

@@ -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"`

View File

@@ -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

View File

@@ -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 *UserReturnInfo `json:"user" pg:"rel:has-one,fk:user_id"`
User *UserReturnInfoModel `json:"user" pg:"rel:has-one,fk:user_id"`
} }
type WalletTypeModel struct { type NewWalletBody struct {
tableName struct{} `pg:"walletTypes,alias:walletTypes"` Name string `json:"name"`
CommonModel UserID string `json:"userId"`
Name string `json:"name"`
} }

View File

@@ -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

View File

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