mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
46 lines
1015 B
Go
46 lines
1015 B
Go
package controllers
|
|
|
|
import (
|
|
"net/http"
|
|
"wallet-api/pkg/models"
|
|
"wallet-api/pkg/services"
|
|
"wallet-api/pkg/utl/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type RegisterController struct {
|
|
RegisterService *services.RegisterService
|
|
}
|
|
|
|
func NewRegisterController(rs *services.RegisterService, s *gin.RouterGroup) *RegisterController {
|
|
rc := new(RegisterController)
|
|
rc.RegisterService = rs
|
|
|
|
s.POST("", rc.Post)
|
|
|
|
return rc
|
|
}
|
|
|
|
func (rc *RegisterController) Post(c *gin.Context) {
|
|
registerBody := createModel()
|
|
if err := c.ShouldBindJSON(®isterBody); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
returnedUser, returnException := rc.RegisterService.Create(®isterBody)
|
|
|
|
if returnException.Message != "" {
|
|
c.JSON(returnException.StatusCode, returnException)
|
|
} else {
|
|
c.JSON(200, returnedUser.Payload())
|
|
}
|
|
|
|
}
|
|
|
|
func createModel() models.UserModel {
|
|
commonModel := common.CreateDbModel()
|
|
userModel := models.UserModel{CommonModel: commonModel}
|
|
return userModel
|
|
}
|