fixes on structuring

This commit is contained in:
Fran Jurmanović
2021-05-15 22:23:30 +02:00
parent e7a80796f7
commit cc98d0cf49
10 changed files with 125 additions and 60 deletions

View File

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

View File

@@ -4,7 +4,6 @@ import (
"net/http"
"wallet-api/pkg/models"
"wallet-api/pkg/services"
"wallet-api/pkg/utl/common"
"github.com/gin-gonic/gin"
)
@@ -23,23 +22,18 @@ func NewRegisterController(rs *services.UsersService, s *gin.RouterGroup) *Regis
}
func (rc *RegisterController) Post(c *gin.Context) {
registerBody := createUserModel()
if err := c.ShouldBindJSON(&registerBody); err != nil {
body := new(models.UserModel)
body.Init()
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
returnedUser, returnException := rc.UsersService.Create(&registerBody)
returnedUser, exceptionReturn := rc.UsersService.Create(body)
if returnException.Message != "" {
c.JSON(returnException.StatusCode, returnException)
if exceptionReturn.Message != "" {
c.JSON(exceptionReturn.StatusCode, exceptionReturn)
} else {
c.JSON(200, returnedUser.Payload())
}
}
func createUserModel() models.UserModel {
commonModel := common.CreateDbModel()
userModel := models.UserModel{CommonModel: commonModel}
return userModel
}