mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
Merge branch 'feature/WA-6-User-Deactivation'
This commit is contained in:
@@ -14,8 +14,7 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
|||||||
ver := s.Group(configs.Prefix)
|
ver := s.Group(configs.Prefix)
|
||||||
|
|
||||||
api := ver.Group("api", middleware.Auth)
|
api := ver.Group("api", middleware.Auth)
|
||||||
register := ver.Group("register")
|
auth := ver.Group("auth")
|
||||||
login := ver.Group("login")
|
|
||||||
wallet := ver.Group("wallet", middleware.Auth)
|
wallet := ver.Group("wallet", middleware.Auth)
|
||||||
transaction := ver.Group("transaction", middleware.Auth)
|
transaction := ver.Group("transaction", middleware.Auth)
|
||||||
transactionType := ver.Group("transaction-type", middleware.Auth)
|
transactionType := ver.Group("transaction-type", middleware.Auth)
|
||||||
@@ -27,8 +26,7 @@ func Routes(s *gin.Engine, db *pg.DB) {
|
|||||||
transactionTypeService := services.TransactionTypeService{Db: db}
|
transactionTypeService := services.TransactionTypeService{Db: db}
|
||||||
|
|
||||||
controllers.NewApiController(&apiService, api)
|
controllers.NewApiController(&apiService, api)
|
||||||
controllers.NewRegisterController(&usersService, register)
|
controllers.NewAuthController(&usersService, auth)
|
||||||
controllers.NewLoginController(&usersService, login)
|
|
||||||
controllers.NewWalletsController(&walletService, wallet)
|
controllers.NewWalletsController(&walletService, wallet)
|
||||||
controllers.NewTransactionController(&transactionService, transaction)
|
controllers.NewTransactionController(&transactionService, transaction)
|
||||||
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
controllers.NewTransactionTypeController(&transactionTypeService, transactionType)
|
||||||
|
|||||||
70
pkg/controllers/auth.go
Normal file
70
pkg/controllers/auth.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"wallet-api/pkg/middleware"
|
||||||
|
"wallet-api/pkg/models"
|
||||||
|
"wallet-api/pkg/services"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthController struct {
|
||||||
|
UsersService *services.UsersService
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthController {
|
||||||
|
rc := new(AuthController)
|
||||||
|
rc.UsersService = rs
|
||||||
|
|
||||||
|
s.POST("login", rc.PostLogin)
|
||||||
|
s.POST("register", rc.PostRegister)
|
||||||
|
s.DELETE("deactivate", middleware.Auth, rc.Delete)
|
||||||
|
|
||||||
|
return rc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *AuthController) PostLogin(c *gin.Context) {
|
||||||
|
body := new(models.Login)
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returnedUser, exceptionReturn := rc.UsersService.Login(body)
|
||||||
|
|
||||||
|
if exceptionReturn.Message != "" {
|
||||||
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
|
} else {
|
||||||
|
c.JSON(200, returnedUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *AuthController) PostRegister(c *gin.Context) {
|
||||||
|
body := new(models.User)
|
||||||
|
body.Init()
|
||||||
|
body.IsActive = true
|
||||||
|
if err := c.ShouldBindJSON(body); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
returnedUser, exceptionReturn := rc.UsersService.Create(body)
|
||||||
|
|
||||||
|
if exceptionReturn.Message != "" {
|
||||||
|
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
||||||
|
} else {
|
||||||
|
c.JSON(200, returnedUser.Payload())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (rc *AuthController) Delete(c *gin.Context) {
|
||||||
|
auth := new(models.Auth)
|
||||||
|
authGet := c.MustGet("auth")
|
||||||
|
auth.Id = authGet.(*models.Auth).Id
|
||||||
|
|
||||||
|
mr, er := rc.UsersService.Deactivate(auth)
|
||||||
|
|
||||||
|
if er.Message != "" {
|
||||||
|
c.JSON(er.StatusCode, er)
|
||||||
|
} else {
|
||||||
|
c.JSON(200, mr)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
"wallet-api/pkg/services"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type LoginController struct {
|
|
||||||
UsersService *services.UsersService
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLoginController(rs *services.UsersService, s *gin.RouterGroup) *LoginController {
|
|
||||||
rc := new(LoginController)
|
|
||||||
rc.UsersService = rs
|
|
||||||
|
|
||||||
s.POST("", rc.Post)
|
|
||||||
|
|
||||||
return rc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rc *LoginController) Post(c *gin.Context) {
|
|
||||||
body := new(models.Login)
|
|
||||||
if err := c.ShouldBindJSON(&body); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
returnedUser, exceptionReturn := rc.UsersService.Login(body)
|
|
||||||
|
|
||||||
if exceptionReturn.Message != "" {
|
|
||||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
|
||||||
} else {
|
|
||||||
c.JSON(200, returnedUser)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
package controllers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"wallet-api/pkg/models"
|
|
||||||
"wallet-api/pkg/services"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RegisterController struct {
|
|
||||||
UsersService *services.UsersService
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *RegisterController {
|
|
||||||
rc := new(RegisterController)
|
|
||||||
rc.UsersService = rs
|
|
||||||
|
|
||||||
s.POST("", rc.Post)
|
|
||||||
|
|
||||||
return rc
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rc *RegisterController) Post(c *gin.Context) {
|
|
||||||
body := new(models.User)
|
|
||||||
body.Init()
|
|
||||||
if err := c.ShouldBindJSON(body); err != nil {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
returnedUser, exceptionReturn := rc.UsersService.Create(body)
|
|
||||||
|
|
||||||
if exceptionReturn.Message != "" {
|
|
||||||
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
|
|
||||||
} else {
|
|
||||||
c.JSON(200, returnedUser.Payload())
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -12,3 +12,7 @@ type FilteredResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ResponseFunc func(*gin.Context) *[]interface{}
|
type ResponseFunc func(*gin.Context) *[]interface{}
|
||||||
|
|
||||||
|
type MessageResponse struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package models
|
|||||||
type User struct {
|
type User struct {
|
||||||
tableName struct{} `pg:"users,alias:users"`
|
tableName struct{} `pg:"users,alias:users"`
|
||||||
BaseModel
|
BaseModel
|
||||||
|
IsActive bool `json:"isActive" pg:"is_active"`
|
||||||
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"`
|
||||||
|
|||||||
@@ -57,6 +57,13 @@ func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.E
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !check.IsActive {
|
||||||
|
exceptionReturn.Message = "Can't log in. User is deactivated."
|
||||||
|
exceptionReturn.ErrorCode = "400106"
|
||||||
|
exceptionReturn.StatusCode = 400
|
||||||
|
return tokenPayload, exceptionReturn
|
||||||
|
}
|
||||||
|
|
||||||
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
if bcrypt.CompareHashAndPassword([]byte(check.Password), []byte(loginBody.Password)) != nil {
|
||||||
exceptionReturn.Message = "Incorrect password"
|
exceptionReturn.Message = "Incorrect password"
|
||||||
exceptionReturn.ErrorCode = "400104"
|
exceptionReturn.ErrorCode = "400104"
|
||||||
@@ -72,6 +79,34 @@ func (us *UsersService) Login(loginBody *models.Login) (*models.Token, *models.E
|
|||||||
return tokenPayload, exceptionReturn
|
return tokenPayload, exceptionReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (us *UsersService) Deactivate(auth *models.Auth) (*models.MessageResponse, *models.Exception) {
|
||||||
|
mm := new(models.MessageResponse)
|
||||||
|
me := new(models.Exception)
|
||||||
|
um := new(models.User)
|
||||||
|
|
||||||
|
err := us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Select()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
me.ErrorCode = "404101"
|
||||||
|
me.Message = "User not found"
|
||||||
|
me.StatusCode = 404
|
||||||
|
return mm, me
|
||||||
|
}
|
||||||
|
um.IsActive = false
|
||||||
|
_, err = us.Db.Model(um).Where("? = ?", pg.Ident("id"), auth.Id).Update()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
me.ErrorCode = "400105"
|
||||||
|
me.Message = "Could not deactivate user"
|
||||||
|
me.StatusCode = 400
|
||||||
|
return mm, me
|
||||||
|
}
|
||||||
|
|
||||||
|
mm.Message = "User successfully deactivated."
|
||||||
|
|
||||||
|
return mm, me
|
||||||
|
}
|
||||||
|
|
||||||
func CreateToken(user *models.User) (string, error) {
|
func CreateToken(user *models.User) (string, error) {
|
||||||
atClaims := jwt.MapClaims{}
|
atClaims := jwt.MapClaims{}
|
||||||
atClaims["authorized"] = true
|
atClaims["authorized"] = true
|
||||||
|
|||||||
Reference in New Issue
Block a user