mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-11-11 07:41:10 +00:00
created login controller
This commit is contained in:
38
pkg/controllers/login.go
Normal file
38
pkg/controllers/login.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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) {
|
||||
loginBody := new(models.LoginModel)
|
||||
if err := c.ShouldBindJSON(&loginBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
returnedUser, returnException := rc.UsersService.Login(loginBody)
|
||||
|
||||
if returnException.Message != "" {
|
||||
c.JSON(returnException.StatusCode, returnException)
|
||||
} else {
|
||||
c.JSON(200, returnedUser)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,12 +10,12 @@ import (
|
||||
)
|
||||
|
||||
type RegisterController struct {
|
||||
RegisterService *services.RegisterService
|
||||
UsersService *services.UsersService
|
||||
}
|
||||
|
||||
func NewRegisterController(rs *services.RegisterService, s *gin.RouterGroup) *RegisterController {
|
||||
func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *RegisterController {
|
||||
rc := new(RegisterController)
|
||||
rc.RegisterService = rs
|
||||
rc.UsersService = rs
|
||||
|
||||
s.POST("", rc.Post)
|
||||
|
||||
@@ -23,12 +23,12 @@ func NewRegisterController(rs *services.RegisterService, s *gin.RouterGroup) *Re
|
||||
}
|
||||
|
||||
func (rc *RegisterController) Post(c *gin.Context) {
|
||||
registerBody := createModel()
|
||||
registerBody := createUserModel()
|
||||
if err := c.ShouldBindJSON(®isterBody); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
returnedUser, returnException := rc.RegisterService.Create(®isterBody)
|
||||
returnedUser, returnException := rc.UsersService.Create(®isterBody)
|
||||
|
||||
if returnException.Message != "" {
|
||||
c.JSON(returnException.StatusCode, returnException)
|
||||
@@ -38,7 +38,7 @@ func (rc *RegisterController) Post(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func createModel() models.UserModel {
|
||||
func createUserModel() models.UserModel {
|
||||
commonModel := common.CreateDbModel()
|
||||
userModel := models.UserModel{CommonModel: commonModel}
|
||||
return userModel
|
||||
|
||||
Reference in New Issue
Block a user