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:
5
go.mod
5
go.mod
@@ -3,18 +3,19 @@ module wallet-api
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/gin-gonic/gin v1.7.1
|
||||
github.com/go-pg/pg/v10 v10.9.1
|
||||
github.com/go-playground/validator/v10 v10.5.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/uuid v1.2.0 // indirect
|
||||
github.com/google/uuid v1.2.0
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/json-iterator/go v1.1.11 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/ugorji/go v1.2.5 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf
|
||||
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
|
||||
5
go.sum
5
go.sum
@@ -4,6 +4,9 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
@@ -126,6 +129,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf h1:B2n+Zi5QeYRDAEodEu72OS36gmTWjgpXr2+cWcBW90o=
|
||||
golang.org/x/crypto v0.0.0-20210506145944-38f3c27a63bf/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
|
||||
@@ -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