fix utils

This commit is contained in:
Jay 2018-04-24 17:49:58 +08:00
parent 1429c7fd85
commit 0a471443ce
8 changed files with 174 additions and 26 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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()

View File

@ -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()
}

View File

@ -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:\"(.+)\"`)

View File

@ -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
}

52
routers/album/album.go Normal file
View File

@ -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)
}

View File

@ -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))
}
}