Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
Signed-off-by: Toby Yan <me@tobyan.com>
  • Loading branch information
toby1991 committed Apr 17, 2019
1 parent 13aa9ad commit 117c2d6
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 420 deletions.
120 changes: 60 additions & 60 deletions app/http/controllers/register.go
@@ -1,77 +1,77 @@
package controllers

import (
"net/http"
"net/http"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"

"github.com/totoval/framework/config"
"github.com/totoval/framework/helpers"
"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/http/controller"
"github.com/totoval/framework/utils/crypt"
"github.com/totoval/framework/utils/jwt"
"totoval/app/http/requests"
"totoval/app/models"
"github.com/totoval/framework/config"
"github.com/totoval/framework/helpers"
"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/http/controller"
"github.com/totoval/framework/utils/crypt"
"github.com/totoval/framework/utils/jwt"
"totoval/app/http/requests"
"totoval/app/models"
)

type Register struct {
controller.BaseController
controller.BaseController
}

func (r *Register) Register(c *gin.Context) {
// validate and assign requestData
var requestData requests.UserRegister
if !r.Validate(c, &requestData, true) {
return
}
// validate and assign requestData
var requestData requests.UserRegister
if !r.Validate(c, &requestData, true) {
return
}

// determine if exist
user := models.User{
Email: &requestData.Email,
}
if m.H().Exist(&user, true) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_existed")})
return
}
// determine if exist
user := models.User{
Email: &requestData.Email,
}
if m.H().Exist(&user, true) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_existed")})
return
}

// create user
// encrypt password //@todo move to model setter later
encryptedPassword := crypt.Bcrypt(requestData.Password)
user.Password = &encryptedPassword
if err := m.H().Create(&user); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")})
return
}
// create user
// encrypt password //@todo move to model setter later
encryptedPassword := crypt.Bcrypt(requestData.Password)
user.Password = &encryptedPassword
if err := m.H().Create(&user); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")})
return
}

// add user affiliation
if config.GetBool("user_affiliation.enable") {
uaffPtr := &models.UserAffiliation{
UserID: user.ID,
}
var err error
if requestData.AffiliationFromCode != nil {
err = uaffPtr.InsertNode(&user, *requestData.AffiliationFromCode)
} else {
err = uaffPtr.InsertNode(&user)
}
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")})
return
}
}
// add user affiliation
if config.GetBool("user_affiliation.enable") {
uaffPtr := &models.UserAffiliation{
UserID: user.ID,
}
var err error
if requestData.AffiliationFromCode != nil {
err = uaffPtr.InsertNode(&user, *requestData.AffiliationFromCode)
} else {
err = uaffPtr.InsertNode(&user)
}
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_system_error")})
return
}
}

// create jwt
newJwt := jwt.NewJWT(config.GetString("auth.sign_key"))
username := ""
if user.Name != nil {
username = *user.Name
}
if token, err := newJwt.CreateToken(string(*user.ID), username); err == nil {
c.JSON(http.StatusOK, gin.H{"token": token})
return
}
// create jwt
newJwt := jwt.NewJWT(config.GetString("auth.sign_key"))
username := ""
if user.Name != nil {
username = *user.Name
}
if token, err := newJwt.CreateToken(string(*user.ID), username); err == nil {
c.JSON(http.StatusOK, gin.H{"token": token})
return
}

c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_token_generate_error")})
return
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.register.failed_token_generate_error")})
return
}
20 changes: 10 additions & 10 deletions app/http/controllers/user_affiliation.go
@@ -1,23 +1,23 @@
package controllers

import (
"net/http"
"net/http"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin"

"github.com/totoval/framework/http/controller"
"totoval/app/models"
"github.com/totoval/framework/http/controller"
"totoval/app/models"
)

type UserAffiliation struct {
controller.BaseController
controller.BaseController
}

func (uaff *UserAffiliation) RenderAll(c *gin.Context) {
var u models.UserAffiliation
c.HTML(http.StatusOK, "user_affiliation.nodes", gin.H{
"data": u.All(),
})
var u models.UserAffiliation
c.HTML(http.StatusOK, "user_affiliation.nodes", gin.H{
"data": u.All(),
})

return
return
}
8 changes: 4 additions & 4 deletions app/http/requests/register.go
@@ -1,8 +1,8 @@
package requests

type UserRegister struct {
Email string `json:"email" binding:"required,email"`
AffiliationFromCode *string `json:"affiliation_code" binding:"omitempty,len=6"`
Password string `json:"password" binding:"required,min=8,max=24"`
PasswordConfirmation string `json:"password_confirmation" binding:"required,eqfield=Password"`
Email string `json:"email" binding:"required,email"`
AffiliationFromCode *string `json:"affiliation_code" binding:"omitempty,len=6"`
Password string `json:"password" binding:"required,min=8,max=24"`
PasswordConfirmation string `json:"password_confirmation" binding:"required,eqfield=Password"`
}

0 comments on commit 117c2d6

Please sign in to comment.