Skip to content

Commit

Permalink
代码整理
Browse files Browse the repository at this point in the history
  • Loading branch information
Away0x committed May 17, 2019
1 parent b775741 commit 30e313b
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 25 deletions.
File renamed without changes.
12 changes: 12 additions & 0 deletions app/controllers/api/image/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package image

import (
userModel "gin_bbs/app/models/user"

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

// Store 上传图片
func Store(c *gin.Context, currentUser *userModel.User, tokenString string) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// @Success 200 {object} controllers.Response "{}"
// @Router /api/users [post]
func Store(c *gin.Context) {
var req request.User
var req request.UserRegister
if err := c.ShouldBindJSON(&req); err != nil {
controllers.SendErrorResponse(c, errno.New(errno.ParamsError, err))
return
Expand Down
11 changes: 0 additions & 11 deletions app/controllers/page/page.go

This file was deleted.

26 changes: 26 additions & 0 deletions app/models/image/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package image

import (
"gin_bbs/app/models/user"
"gin_bbs/database"
)

// Get -
func Get(id int) (*Image, error) {
i := &Image{}
if err := database.DB.First(&i, id).Error; err != nil {
return nil, err
}

return i, nil
}

// User 获取 user
func (i *Image) User() (u *user.User, err error) {
u, err = user.Get(int(i.UserID))
if err != nil {
return nil, err
}

return u, err
}
25 changes: 25 additions & 0 deletions app/models/image/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package image

import (
"time"
)

var (
// Types image type
Types = []string{"avatar", "topic"}
)

// Image -
type Image struct {
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT;not null"`
Type string `gorm:"column:type;type:varchar(255);not null" sql:"index"` // 类型 (avatar,topic)
UserID uint `gorm:"column:user_id;not null" sql:"index"` // 用户 id
Path string `gorm:"column:path;type:varchar(255);not null"` // 图片路径
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}

// TableName 表名
func (Image) TableName() string {
return "images"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"gin_bbs/pkg/ginutils/validate"
)

// User -
type User struct {
// UserRegister -
type UserRegister struct {
validate.Validate
Name string `json:"name"`
Password string `json:"password"`
Expand All @@ -19,7 +19,7 @@ type User struct {
}

// RegisterValidators 注册验证器
func (u *User) RegisterValidators() validate.ValidatorMap {
func (u *UserRegister) RegisterValidators() validate.ValidatorMap {
return validate.ValidatorMap{
"name": {
validate.RequiredValidator(u.Name),
Expand All @@ -41,7 +41,7 @@ func (u *User) RegisterValidators() validate.ValidatorMap {
}

// RegisterMessages 注册错误信息
func (*User) RegisterMessages() validate.MessagesMap {
func (*UserRegister) RegisterMessages() validate.MessagesMap {
return validate.MessagesMap{
"name": {
"用户名不能为空",
Expand All @@ -57,7 +57,7 @@ func (*User) RegisterMessages() validate.MessagesMap {
}

// ValidateAndCreateUser -
func (u *User) ValidateAndCreateUser() *errno.Errno {
func (u *UserRegister) ValidateAndCreateUser() *errno.Errno {
ok, _, errMap := validate.Run(u)
if !ok {
return errno.New(errno.ParamsError, errMap)
Expand Down
4 changes: 4 additions & 0 deletions bootstrap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"errors"
"fmt"
"gin_bbs/app/models/category"
"gin_bbs/app/models/image"
"gin_bbs/app/models/notification"
passwordreset "gin_bbs/app/models/password_reset"
"gin_bbs/app/models/reply"
"gin_bbs/app/models/topic"
"gin_bbs/app/models/user"
"gin_bbs/config"
Expand All @@ -25,7 +27,9 @@ func SetupDB(needMock bool) (*gorm.DB, error) {
&passwordreset.PasswordReset{},
&category.Category{},
&topic.Topic{},
&reply.Reply{},
&notification.Notification{},
&image.Image{},
)

// 只有非 release 时才可 mock
Expand Down
7 changes: 0 additions & 7 deletions resources/views/pages/root.html

This file was deleted.

6 changes: 5 additions & 1 deletion routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"gin_bbs/app/controllers/api/authorization"
"gin_bbs/app/controllers/api/captcha"
"gin_bbs/app/controllers/api/image"
"gin_bbs/app/controllers/api/user"
vericode "gin_bbs/app/controllers/api/verification_code"

Expand Down Expand Up @@ -39,6 +40,7 @@ func registerAPI(r *router.MyRoute, middlewares ...gin.HandlerFunc) {
// 刷新 token
r.Register("PUT", "api.authorizations.update", "/authorizations/current",
middleware.TokenAuth(),
middleware.RateLimiter(1*time.Minute, 3), // 1 分钟 3 次
wrapper.GetToken(authorization.Update))
// 删除 token
r.Register("DELETE", "api.authorizations.destroy", "/authorizations/current",
Expand All @@ -49,7 +51,9 @@ func registerAPI(r *router.MyRoute, middlewares ...gin.HandlerFunc) {
// +++++++++++++++ 用户相关 +++++++++++++++
userRouter := r.Group("/user", middleware.TokenAuth())
{
// 获取用户信息
// 获取当前登录用户信息
userRouter.Register("GET", "api.user.show", "", wrapper.GetToken(user.Show))
// 图片资源
userRouter.Register("POST", "api.images.store", "", wrapper.GetToken(image.Store))
}
}

0 comments on commit 30e313b

Please sign in to comment.