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

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

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

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

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