From 0a471443ce1681daa5d6c9d74a3c85919d1b9177 Mon Sep 17 00:00:00 2001 From: Jay Date: Tue, 24 Apr 2018 17:49:58 +0800 Subject: [PATCH] fix utils --- models/account.go | 18 ++++---- models/album.go | 72 ++++++++++++++++++++++++++++---- modules/memstore/redis.go | 10 +---- modules/middleware/middleware.go | 34 +++++++++++++++ modules/utils/utils.go | 5 ++- routers/account/account.go | 2 - routers/album/album.go | 52 +++++++++++++++++++++++ routers/routes/routes.go | 7 ++++ 8 files changed, 174 insertions(+), 26 deletions(-) create mode 100644 modules/middleware/middleware.go create mode 100644 routers/album/album.go diff --git a/models/account.go b/models/account.go index 0e0474e..eddf39c 100644 --- a/models/account.go +++ b/models/account.go @@ -9,13 +9,13 @@ import ( // Account - Account table struct type Account struct { - ID string `xorm:"id <-" cc:"id"` - Account string `xorm:"account" cc:"account"` - Password string `xorm:"password" cc:"-"` - Nick string `xorm:"nick" cc:"nick"` - Email string `xorm:"email" cc:"email"` - Ctime time.Time `xorm:"ctime created" cc:"ctime"` - Mtime time.Time `xorm:"mtime updated" cc:"mtime"` + ID string `sql:"id" cc:"id"` + Account string `sql:"account" cc:"account"` + Password string `sql:"password" cc:"-"` + Nick string `sql:"nick" cc:"nick"` + Email string `sql:"email" cc:"email"` + Ctime time.Time `sql:"ctime" cc:"ctime"` + Mtime time.Time `sql:"mtime" cc:"mtime"` } // GetAllAccount - all account @@ -25,6 +25,7 @@ func GetAllAccount() ([]Account, error) { if err != nil { return nil, err } + defer rows.Close() err = dbtool.ScanToStructAll(rows, &accs) return accs, nil } @@ -37,6 +38,7 @@ func GetAccount(account string) (acc *Account, err error) { if err != nil { return nil, err } + defer rows.Close() ok, err := dbtool.ScanToStruct(rows, acc) if err != nil || !ok { @@ -52,6 +54,7 @@ func (a *Account) Get() (bool, error) { if err != nil { return false, err } + defer rows.Close() ok, err := dbtool.ScanToStruct(rows, a) if err != nil { @@ -68,6 +71,7 @@ func (a *Account) Create() error { if err != nil { return err } + defer row.Close() ok, err := dbtool.ScanToStruct(row, a) if err != nil { diff --git a/models/album.go b/models/album.go index 5c0a601..c59f5f4 100644 --- a/models/album.go +++ b/models/album.go @@ -1,16 +1,74 @@ package models import ( + "database/sql" + "errors" "time" + + "git.trj.tw/golang/utils/dbtool" ) // Album model type Album struct { - ID string `xorm:"id"` - UID string `xorm:"uid"` - Name string `xorm:"name"` - Public bool `xorm:"public default false"` - Ctime time.Time `xorm:"ctime created"` - Mtime time.Time `xorm:"mtime updated"` - Photos []*Photo `xorm:"-"` + ID string `sql:"id" cc:"id"` + UID string `sql:"uid" cc:"uid"` + Name string `sql:"name" cc:"name"` + Public bool `sql:"public" cc:"public"` + Ctime time.Time `sql:"ctime" cc:"ctime"` + Mtime time.Time `sql:"mtime" cc:"mtime"` + Photos []*Photo `sql:"-" cc:"-"` +} + +// GetAllAlbums - +func GetAllAlbums(arg ...bool) (album []Album, err error) { + var rows *sql.Rows + if len(arg) > 0 { + rows, err = x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album" where "public" = $1`, arg[0]) + } else { + rows, err = x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album"`) + } + + if err != nil { + return nil, err + } + defer rows.Close() + + err = dbtool.ScanToStructAll(rows, &album) + if err != nil { + return nil, err + } + + return +} + +// GetUserAlbums - +func GetUserAlbums(uid string) (album []Album, err error) { + if len(uid) == 0 { + return nil, errors.New("user id empty") + } + rows, err := x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album" where "uid" = $1`, uid) + + err = dbtool.ScanToStructAll(rows, &album) + if err != nil { + return nil, err + } + return +} + +// GetAlbum - +func GetAlbum(id string) (album *Album, err error) { + rows, err := x.Query(`selecy "id", "uid", "name", "public", "ctime", "mtime" where "id" = $1 limit 1`, id) + if err != nil { + return + } + defer rows.Close() + ok, err := dbtool.ScanToStruct(rows, album) + if err != nil { + return + } + if !ok { + return nil, nil + } + + return } diff --git a/modules/memstore/redis.go b/modules/memstore/redis.go index 2aa5693..010c946 100644 --- a/modules/memstore/redis.go +++ b/modules/memstore/redis.go @@ -55,13 +55,9 @@ func RedisGet(namespace, key string) (string, error) { return "", errors.New("key empty") } - if len(n) > 0 { - n += ":" - } - n += key - val, err := redisStore.Get(key).Result() + val, err := redisStore.Get(n).Result() return val, err } @@ -77,10 +73,6 @@ func RedisDel(namespace, key string) error { return errors.New("key empty") } - if len(n) > 0 { - n += ":" - } - n += key err := redisStore.Del(n).Err() diff --git a/modules/middleware/middleware.go b/modules/middleware/middleware.go new file mode 100644 index 0000000..b570241 --- /dev/null +++ b/modules/middleware/middleware.go @@ -0,0 +1,34 @@ +package middleware + +import ( + "encoding/json" + + "git.trj.tw/golang/go-gallery/modules/context" + "git.trj.tw/golang/go-gallery/modules/memstore" +) + +// VerifyToken - +func VerifyToken(c *context.Context) { + token := c.GetHeader("X-Auth-Token") + + if len(token) == 0 { + c.CustomRes("Forbidden", nil) + return + } + + str, err := memstore.RedisGet("golang", token) + if err != nil || len(str) == 0 { + c.CustomRes("Forbidden", "token invaild") + return + } + + jsonData := make(map[string]interface{}) + err = json.Unmarshal([]byte(str), &jsonData) + if err != nil { + c.ServerError(nil) + } + + c.Set("token", jsonData) + + c.Next() +} diff --git a/modules/utils/utils.go b/modules/utils/utils.go index 15c9f56..c086a96 100644 --- a/modules/utils/utils.go +++ b/modules/utils/utils.go @@ -7,7 +7,10 @@ import ( // ToMap struct to map[string]interface{} func ToMap(ss interface{}) map[string]interface{} { - t := reflect.ValueOf(ss).Elem() + t := reflect.ValueOf(ss) + if t.Kind() == reflect.Ptr { + t = t.Elem() + } smap := make(map[string]interface{}) mtag := regexp.MustCompile(`cc:\"(.+)\"`) diff --git a/routers/account/account.go b/routers/account/account.go index e166af1..41e579e 100644 --- a/routers/account/account.go +++ b/routers/account/account.go @@ -5,7 +5,6 @@ import ( "crypto/sha512" "encoding/hex" "encoding/json" - "log" "reflect" "strings" @@ -40,7 +39,6 @@ func UserLogin(c *context.Context) { acc, err := models.GetAccount(loginArg.Account) if err != nil { - log.Println(err) c.ServerError(nil) return } diff --git a/routers/album/album.go b/routers/album/album.go new file mode 100644 index 0000000..35e70c2 --- /dev/null +++ b/routers/album/album.go @@ -0,0 +1,52 @@ +package album + +import ( + "log" + + "git.trj.tw/golang/go-gallery/modules/context" + "git.trj.tw/golang/go-gallery/modules/utils" + + "git.trj.tw/golang/go-gallery/models" +) + +// GetAllAlbums - +func GetAllAlbums(c *context.Context) { + val, ok := c.Get("token") + + if !ok { + c.CustomRes("Foridden", "User token data not found") + } + + albums, err := models.GetUserAlbums(val.(map[string]interface{})["user"].(map[string]interface{})["id"].(string)) + if err != nil { + c.ServerError(nil) + return + } + + data := make(map[string]interface{}) + data["albums"] = make([]map[string]interface{}, 0) + for _, v := range albums { + log.Println(v) + data["albums"] = append(data["albums"].([]map[string]interface{}), utils.ToMap(v)) + } + c.Success(data) +} + +// GetAlbum - +func GetAlbum(c *context.Context) { + id := c.Param("id") + + album, err := models.GetAlbum(id) + if err != nil { + c.ServerError(nil) + return + } + if album == nil { + c.NotFound("album not found") + return + } + + data := make(map[string]interface{}) + data["album"] = album + c.Success(data) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 44a23be..7a6a4bf 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -3,7 +3,9 @@ package routes import ( "git.trj.tw/golang/go-gallery/models" "git.trj.tw/golang/go-gallery/modules/context" + "git.trj.tw/golang/go-gallery/modules/middleware" "git.trj.tw/golang/go-gallery/routers/account" + "git.trj.tw/golang/go-gallery/routers/album" "github.com/gin-gonic/gin" ) @@ -43,4 +45,9 @@ func SetDefaultRoutes(r *gin.Engine) { accountAPI.POST("/logout", context.PatchContext(account.UserLogout)) accountAPI.POST("/signup", context.PatchContext(account.UserSignup)) } + api.GET("/albums", context.PatchContext(middleware.VerifyToken), context.PatchContext(album.GetAllAlbums)) + albumAPI := api.Group("/album", context.PatchContext(middleware.VerifyToken)) + { + albumAPI.GET("/:id", context.PatchContext(album.GetAlbum)) + } }