mirror of
https://github.com/FJurmanovic/wallet-go-api.git
synced 2026-02-06 06:08:16 +00:00
register controller
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
|
||||
func Routes(s *gin.Engine, db *pg.DB) {
|
||||
apiService := services.ApiService{Db: db}
|
||||
registerService := services.RegisterService{Db: db}
|
||||
|
||||
apiController := controllers.ApiController{ApiService: &apiService}
|
||||
registerController := controllers.RegisterController{RegisterService: ®isterService}
|
||||
|
||||
apiController.Init(s)
|
||||
registerController.Init(s)
|
||||
}
|
||||
|
||||
41
pkg/controllers/register.go
Normal file
41
pkg/controllers/register.go
Normal file
@@ -0,0 +1,41 @@
|
||||
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 (rc *RegisterController) Init(s *gin.Engine) {
|
||||
apiGroup := s.Group("/register")
|
||||
apiGroup.POST("", rc.Post)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
7
pkg/middleware/auth.go
Normal file
7
pkg/middleware/auth.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func Auth(c *gin.Context) {
|
||||
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
|
||||
func Start(conn *pg.DB) {
|
||||
apiMigration := migrations.ApiMigration{Db: conn}
|
||||
usersMigration := migrations.UsersMigration{Db: conn}
|
||||
|
||||
apiMigration.Create()
|
||||
usersMigration.Create()
|
||||
}
|
||||
|
||||
31
pkg/migrate/migrations/users.go
Normal file
31
pkg/migrate/migrations/users.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
)
|
||||
|
||||
type UsersMigration struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (am *UsersMigration) Create() {
|
||||
models := []interface{}{
|
||||
(*models.UserModel)(nil),
|
||||
}
|
||||
|
||||
for _, model := range models {
|
||||
err := am.Db.Model(model).CreateTable(&orm.CreateTableOptions{
|
||||
IfNotExists: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Error Creating Table: %s", err)
|
||||
} else {
|
||||
fmt.Println("Table created successfully")
|
||||
}
|
||||
}
|
||||
}
|
||||
9
pkg/models/db.go
Normal file
9
pkg/models/db.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type CommonModel struct {
|
||||
Id string `json:"id" pg:"id"`
|
||||
DateCreated time.Time `json:"dateCreated" pg:"datecreated"`
|
||||
DateUpdated time.Time `json:"dateUpdated" pg:"dateupdated"`
|
||||
}
|
||||
7
pkg/models/exception.go
Normal file
7
pkg/models/exception.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
type ExceptionModel struct {
|
||||
ErrorCode string `json:"errorCode"`
|
||||
Message string `json:"message"`
|
||||
StatusCode int `json:"statusCode"`
|
||||
}
|
||||
25
pkg/models/register.go
Normal file
25
pkg/models/register.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package models
|
||||
|
||||
type UserModel struct {
|
||||
tableName struct{} `pg:"users,alias:users"`
|
||||
CommonModel
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type UserReturnInfoModel struct {
|
||||
tableName struct{} `pg:"users,alias:users"`
|
||||
CommonModel
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func (um *UserModel) Payload() UserReturnInfoModel {
|
||||
payload := UserReturnInfoModel{
|
||||
CommonModel: um.CommonModel,
|
||||
Username: um.Username,
|
||||
Email: um.Email,
|
||||
}
|
||||
return payload
|
||||
}
|
||||
41
pkg/services/register.go
Normal file
41
pkg/services/register.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"wallet-api/pkg/models"
|
||||
"wallet-api/pkg/utl/common"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
)
|
||||
|
||||
type RegisterService struct {
|
||||
Db *pg.DB
|
||||
}
|
||||
|
||||
func (rs *RegisterService) Create(registerBody *models.UserModel) (*models.UserModel, models.ExceptionModel) {
|
||||
var checkModel models.UserModel
|
||||
var exceptionReturn models.ExceptionModel
|
||||
|
||||
rs.Db.Model(&checkModel).Where("? = ?", pg.Ident("username"), registerBody.Username).WhereOr("? = ?", pg.Ident("email"), registerBody.Email).Select()
|
||||
if checkModel.Username != "" || checkModel.Email != "" {
|
||||
exceptionReturn.Message = "User already exists"
|
||||
exceptionReturn.ErrorCode = "400101"
|
||||
exceptionReturn.StatusCode = 400
|
||||
return &checkModel, exceptionReturn
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(registerBody.Password), bcrypt.DefaultCost)
|
||||
common.CheckError(err)
|
||||
|
||||
registerBody.Password = string(hashedPassword)
|
||||
_, err = rs.Db.Model(registerBody).Insert()
|
||||
|
||||
if err != nil {
|
||||
exceptionReturn.Message = "Error creating user"
|
||||
exceptionReturn.ErrorCode = "400102"
|
||||
exceptionReturn.StatusCode = 400
|
||||
}
|
||||
|
||||
return registerBody, exceptionReturn
|
||||
}
|
||||
@@ -1,9 +1,25 @@
|
||||
package common
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
"wallet-api/pkg/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func CheckError(err error) {
|
||||
if err != nil {
|
||||
log.Fatalf("Error occured. %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateDbModel() models.CommonModel {
|
||||
date := time.Now()
|
||||
dbModel := models.CommonModel{
|
||||
Id: uuid.NewString(),
|
||||
DateCreated: date,
|
||||
DateUpdated: date,
|
||||
}
|
||||
return dbModel
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user