Skip to content

Commit

Permalink
upgrade framework
Browse files Browse the repository at this point in the history
Signed-off-by: Toby Yan <me@tobyan.com>
  • Loading branch information
toby1991 committed Jul 10, 2019
1 parent 8eef1b4 commit ed255a7
Show file tree
Hide file tree
Showing 23 changed files with 215 additions and 227 deletions.
10 changes: 5 additions & 5 deletions .env.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"APP_DEBUG": true,
"APP_PORT": 8080,
"APP_LOCALE": "en",
"APP_KEY": "ajwklefjklwjelkflkwefwef",
"APP_KEY": "YOUR-APP-KEY",

"DB_CONNECTION": "mysql",
"DB_HOST": "127.0.0.1",
"DB_PORT": "3306",
"DB_DATABASE": "wallet",
"DB_USERNAME": "root",
"DB_PASSWORD": "eznOiMjtLXWugdDl9MLm",
"DB_DATABASE": "YOUR-DATABASE-NAME",
"DB_USERNAME": "YOUR-DATABASE-USER",
"DB_PASSWORD": "YOUR-DATABASE-USER-PASSWORD",
"DB_PREFIX": "prefix_",

"CACHE_DRIVER": "memory",
Expand All @@ -21,7 +21,7 @@
"REDIS_DB": 0,
"REDIS_CACHE_DB": 1,

"AUTH_SIGN_KEY": "klasdjflksdjfklsdkljf",
"AUTH_SIGN_KEY": "YOUR-AUTH-SIGN-KEY",

"QUEUE_CONNECTION": "nsq",
"QUEUE_NSQD_TCP_HOST": "127.0.0.1",
Expand Down
14 changes: 7 additions & 7 deletions app/http/controllers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package controllers
import (
"net/http"

"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/helpers/toto"
"github.com/totoval/framework/http/controller"
"github.com/totoval/framework/request"
"github.com/totoval/framework/utils/crypt"
"github.com/totoval/framework/utils/jwt"

Expand All @@ -20,7 +20,7 @@ type Login struct {
controller.BaseController
}

func (l *Login) Login(c *gin.Context) {
func (l *Login) Login(c *request.Context) {
// validate and assign requestData
var requestData requests.UserLogin
if !l.Validate(c, &requestData, true) {
Expand All @@ -31,12 +31,12 @@ func (l *Login) Login(c *gin.Context) {
Email: &requestData.Email,
}
if err := m.H().First(&user, false); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.login.failed_not_exist")})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": helpers.L(c, "auth.login.failed_not_exist")})
return
}

if !crypt.BcryptCheck(*user.Password, requestData.Password) {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": helpers.L(c, "auth.login.failed_wrong_password")})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": helpers.L(c, "auth.login.failed_wrong_password")})
return
}

Expand All @@ -47,10 +47,10 @@ func (l *Login) Login(c *gin.Context) {
username = *user.Name
}
if token, err := newJwt.CreateToken(string(*user.ID), username); err == nil {
c.JSON(http.StatusOK, gin.H{"token": token})
c.JSON(http.StatusOK, toto.V{"token": token})
return
}

c.JSON(http.StatusOK, gin.H{"error": helpers.L(c, "auth.login.failed_token_generate_error")})
c.JSON(http.StatusOK, toto.V{"error": helpers.L(c, "auth.login.failed_token_generate_error")})
return
}
13 changes: 6 additions & 7 deletions app/http/controllers/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"net/http"

"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/logs"

"github.com/gin-gonic/gin"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/request"

"github.com/totoval/framework/hub"

Expand All @@ -29,7 +28,7 @@ type Register struct {
controller.BaseController
}

func (r *Register) Register(c *gin.Context) {
func (r *Register) Register(c *request.Context) {
// validate and assign requestData
var requestData requests.UserRegister
if !r.Validate(c, &requestData, true) {
Expand All @@ -40,7 +39,7 @@ func (r *Register) Register(c *gin.Context) {
if err := recover(); err != nil {
responseErr, ok := err.(error)
if ok {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": responseErr.Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": responseErr.Error()})
return
}
panic(err)
Expand Down Expand Up @@ -92,9 +91,9 @@ func (r *Register) Register(c *gin.Context) {
}
ur.SetParam(param)
if errs := hub.Emit(&ur); errs != nil {
log.Info("user registered event emit failed", logs.Field{"event": ur, "errors": errs})
log.Info("user registered event emit failed", toto.V{"event": ur, "errors": errs})
}

c.JSON(http.StatusOK, gin.H{"token": token})
c.JSON(http.StatusOK, toto.V{"token": token})
return
}
66 changes: 33 additions & 33 deletions app/http/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,78 @@ package controllers
import (
"net/http"

"github.com/gin-gonic/gin"

"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/helpers/ptr"
"github.com/totoval/framework/helpers/toto"
"github.com/totoval/framework/http/controller"
"github.com/totoval/framework/http/middleware"
"github.com/totoval/framework/model"
"github.com/totoval/framework/policy"
"github.com/totoval/framework/request"

"totoval/app/models"
"totoval/app/policies"
)

type User struct {
controller.BaseController
}

func (*User) LogOut(c *gin.Context) {
func (*User) LogOut(c *request.Context) {
if err := middleware.Revoke(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{})
c.JSON(http.StatusOK, toto.V{})
return
}

func (*User) Info(c *gin.Context) {
userID, isAbort := middleware.AuthClaimsID(c)
if isAbort {
func (u *User) Info(c *request.Context) {
if u.Scan(c) {
return
}
user := models.User{
ID: &userID,
}
user := u.User().Value().(*models.User)

if err := m.H().First(&user, false); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
if permit, _ := u.Authorize(c, policies.NewUserPolicy(), policy.ActionView); !permit {
c.JSON(http.StatusForbidden, toto.V{"error": policy.UserNotPermitError{}.Error()})
return
}

user.Password = ptr.String("") // remove password value for response rendering
c.JSON(http.StatusOK, gin.H{"data": user})
c.JSON(http.StatusOK, toto.V{"data": user})
return
}

func (*User) AllUser(c *gin.Context) {
func (*User) AllUser(c *request.Context) {
user := &models.User{}
outArr, err := user.ObjArr([]model.Filter{}, []model.Sort{}, 0, false)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": outArr.([]models.User)})
c.JSON(http.StatusOK, toto.V{"data": outArr.([]models.User)})
return
}

func (*User) PaginateUser(c *gin.Context) {
func (*User) PaginateUser(c *request.Context) {
user := &models.User{}
pagination, err := user.ObjArrPaginate(c, 25, []model.Filter{}, []model.Sort{}, 0, false)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"data": gin.H{"item": pagination.ItemArr(), "totalPage": pagination.LastPage(), "currentPage": pagination.CurrentPage(), "count": pagination.Count(), "total": pagination.Total()}})
c.JSON(http.StatusOK, toto.V{"data": toto.V{"item": pagination.ItemArr(), "totalPage": pagination.LastPage(), "currentPage": pagination.CurrentPage(), "count": pagination.Count(), "total": pagination.Total()}})
return
}

func (*User) Update(c *gin.Context) {
func (*User) Update(c *request.Context) {
var id uint
id = 14
user := models.User{
ID: &id,
}
if err := m.H().First(&user, false); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err.Error()})
return
}

Expand All @@ -83,34 +83,34 @@ func (*User) Update(c *gin.Context) {
Name: &name,
}
if err := m.H().Save(&user, modifyUser); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"data": user})
c.JSON(http.StatusOK, toto.V{"data": user})
return

// m.Transaction(func() {
// fmt.Println(id)
// panic(123)
// }, 3)
}
func (*User) Delete(c *gin.Context) {
func (*User) Delete(c *request.Context) {
var id uint
id = 14
user := models.User{
ID: &id,
}
if err := m.H().Delete(&user, false); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"data": true})
c.JSON(http.StatusOK, toto.V{"data": true})
return
}
func (*User) DeleteTransaction(c *gin.Context) {
func (*User) DeleteTransaction(c *request.Context) {
defer func() { // handle transaction error
if err := recover(); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err.(error).Error()})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err.(error).Error()})
return
}
}()
Expand All @@ -127,20 +127,20 @@ func (*User) DeleteTransaction(c *gin.Context) {
}
}, 1)

c.JSON(http.StatusOK, gin.H{"data": true})
c.JSON(http.StatusOK, toto.V{"data": true})
return
}
func (*User) Restore(c *gin.Context) {
func (*User) Restore(c *request.Context) {
var id uint
id = 14
modifyUser := models.User{
ID: &id,
}

if err := m.H().Restore(&modifyUser); err != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{"error": err})
c.JSON(http.StatusUnprocessableEntity, toto.V{"error": err})
return
}
c.JSON(http.StatusOK, gin.H{"data": true})
c.JSON(http.StatusOK, toto.V{"data": true})
return
}
8 changes: 4 additions & 4 deletions app/http/controllers/user_affiliation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ package controllers
import (
"net/http"

"github.com/gin-gonic/gin"

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

type UserAffiliation struct {
controller.BaseController
}

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

Expand Down
12 changes: 4 additions & 8 deletions app/http/middleware/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package middleware

import (
"github.com/totoval/framework/helpers/log"
"github.com/totoval/framework/logs"

"github.com/gin-gonic/gin"

"github.com/totoval/framework/helpers/zone"
)

func Example() gin.HandlerFunc {
return func(c *gin.Context) {
func Example() request.HandlerFunc {
return func(c *request.Context) {
t := zone.Now()

// Set example variable
Expand All @@ -22,10 +18,10 @@ func Example() gin.HandlerFunc {

// after request
latency := zone.Since(t)
log.Info("latency", logs.Field{"latency": latency})
log.Info("latency", toto.V{"latency": latency})

// access the status we are sending
status := c.Writer.Status()
log.Info("status", logs.Field{"status": status})
log.Info("status", toto.V{"status": status})
}
}
5 changes: 2 additions & 3 deletions app/models/failed_queue.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package models

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

"github.com/totoval/framework/helpers/zone"
"github.com/totoval/framework/request"

"github.com/totoval/framework/helpers/m"
"github.com/totoval/framework/helpers/pb"
Expand Down Expand Up @@ -123,7 +122,7 @@ func (fq *FailedQueue) ObjArr(filterArr []model.Filter, sortArr []model.Sort, li
}
return outArr, nil
}
func (fq *FailedQueue) ObjArrPaginate(c *gin.Context, perPage uint, filterArr []model.Filter, sortArr []model.Sort, limit int, withTrashed bool) (pagination model.Pagination, err error) {
func (fq *FailedQueue) ObjArrPaginate(c *request.Context, perPage uint, filterArr []model.Filter, sortArr []model.Sort, limit int, withTrashed bool) (pagination model.Pagination, err error) {
var outArr []FailedQueue
filter := model.Model(*m.H().Q(filterArr, sortArr, limit, withTrashed))
return filter.Paginate(&outArr, c, perPage)
Expand Down
Loading

0 comments on commit ed255a7

Please sign in to comment.