fix utils
This commit is contained in:
		
							parent
							
								
									1429c7fd85
								
							
						
					
					
						commit
						0a471443ce
					
				@ -9,13 +9,13 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Account - Account table struct
 | 
					// Account - Account table struct
 | 
				
			||||||
type Account struct {
 | 
					type Account struct {
 | 
				
			||||||
	ID       string    `xorm:"id <-" cc:"id"`
 | 
						ID       string    `sql:"id" cc:"id"`
 | 
				
			||||||
	Account  string    `xorm:"account" cc:"account"`
 | 
						Account  string    `sql:"account" cc:"account"`
 | 
				
			||||||
	Password string    `xorm:"password" cc:"-"`
 | 
						Password string    `sql:"password" cc:"-"`
 | 
				
			||||||
	Nick     string    `xorm:"nick" cc:"nick"`
 | 
						Nick     string    `sql:"nick" cc:"nick"`
 | 
				
			||||||
	Email    string    `xorm:"email" cc:"email"`
 | 
						Email    string    `sql:"email" cc:"email"`
 | 
				
			||||||
	Ctime    time.Time `xorm:"ctime created" cc:"ctime"`
 | 
						Ctime    time.Time `sql:"ctime" cc:"ctime"`
 | 
				
			||||||
	Mtime    time.Time `xorm:"mtime updated" cc:"mtime"`
 | 
						Mtime    time.Time `sql:"mtime" cc:"mtime"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// GetAllAccount - all account
 | 
					// GetAllAccount - all account
 | 
				
			||||||
@ -25,6 +25,7 @@ func GetAllAccount() ([]Account, error) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer rows.Close()
 | 
				
			||||||
	err = dbtool.ScanToStructAll(rows, &accs)
 | 
						err = dbtool.ScanToStructAll(rows, &accs)
 | 
				
			||||||
	return accs, nil
 | 
						return accs, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -37,6 +38,7 @@ func GetAccount(account string) (acc *Account, err error) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer rows.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ok, err := dbtool.ScanToStruct(rows, acc)
 | 
						ok, err := dbtool.ScanToStruct(rows, acc)
 | 
				
			||||||
	if err != nil || !ok {
 | 
						if err != nil || !ok {
 | 
				
			||||||
@ -52,6 +54,7 @@ func (a *Account) Get() (bool, error) {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return false, err
 | 
							return false, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer rows.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ok, err := dbtool.ScanToStruct(rows, a)
 | 
						ok, err := dbtool.ScanToStruct(rows, a)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
@ -68,6 +71,7 @@ func (a *Account) Create() error {
 | 
				
			|||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						defer row.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ok, err := dbtool.ScanToStruct(row, a)
 | 
						ok, err := dbtool.ScanToStruct(row, a)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,16 +1,74 @@
 | 
				
			|||||||
package models
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"database/sql"
 | 
				
			||||||
 | 
						"errors"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.trj.tw/golang/utils/dbtool"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Album model
 | 
					// Album model
 | 
				
			||||||
type Album struct {
 | 
					type Album struct {
 | 
				
			||||||
	ID     string    `xorm:"id"`
 | 
						ID     string    `sql:"id" cc:"id"`
 | 
				
			||||||
	UID    string    `xorm:"uid"`
 | 
						UID    string    `sql:"uid" cc:"uid"`
 | 
				
			||||||
	Name   string    `xorm:"name"`
 | 
						Name   string    `sql:"name" cc:"name"`
 | 
				
			||||||
	Public bool      `xorm:"public default false"`
 | 
						Public bool      `sql:"public" cc:"public"`
 | 
				
			||||||
	Ctime  time.Time `xorm:"ctime created"`
 | 
						Ctime  time.Time `sql:"ctime" cc:"ctime"`
 | 
				
			||||||
	Mtime  time.Time `xorm:"mtime updated"`
 | 
						Mtime  time.Time `sql:"mtime" cc:"mtime"`
 | 
				
			||||||
	Photos []*Photo  `xorm:"-"`
 | 
						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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -55,13 +55,9 @@ func RedisGet(namespace, key string) (string, error) {
 | 
				
			|||||||
		return "", errors.New("key empty")
 | 
							return "", errors.New("key empty")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(n) > 0 {
 | 
					 | 
				
			||||||
		n += ":"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	n += key
 | 
						n += key
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	val, err := redisStore.Get(key).Result()
 | 
						val, err := redisStore.Get(n).Result()
 | 
				
			||||||
	return val, err
 | 
						return val, err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -77,10 +73,6 @@ func RedisDel(namespace, key string) error {
 | 
				
			|||||||
		return errors.New("key empty")
 | 
							return errors.New("key empty")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if len(n) > 0 {
 | 
					 | 
				
			||||||
		n += ":"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	n += key
 | 
						n += key
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := redisStore.Del(n).Err()
 | 
						err := redisStore.Del(n).Err()
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										34
									
								
								modules/middleware/middleware.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								modules/middleware/middleware.go
									
									
									
									
									
										Normal 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()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -7,7 +7,10 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// ToMap struct to map[string]interface{}
 | 
					// ToMap struct to map[string]interface{}
 | 
				
			||||||
func ToMap(ss interface{}) 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{})
 | 
						smap := make(map[string]interface{})
 | 
				
			||||||
	mtag := regexp.MustCompile(`cc:\"(.+)\"`)
 | 
						mtag := regexp.MustCompile(`cc:\"(.+)\"`)
 | 
				
			||||||
 | 
				
			|||||||
@ -5,7 +5,6 @@ import (
 | 
				
			|||||||
	"crypto/sha512"
 | 
						"crypto/sha512"
 | 
				
			||||||
	"encoding/hex"
 | 
						"encoding/hex"
 | 
				
			||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"log"
 | 
					 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -40,7 +39,6 @@ func UserLogin(c *context.Context) {
 | 
				
			|||||||
	acc, err := models.GetAccount(loginArg.Account)
 | 
						acc, err := models.GetAccount(loginArg.Account)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		log.Println(err)
 | 
					 | 
				
			||||||
		c.ServerError(nil)
 | 
							c.ServerError(nil)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										52
									
								
								routers/album/album.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								routers/album/album.go
									
									
									
									
									
										Normal 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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -3,7 +3,9 @@ package routes
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"git.trj.tw/golang/go-gallery/models"
 | 
						"git.trj.tw/golang/go-gallery/models"
 | 
				
			||||||
	"git.trj.tw/golang/go-gallery/modules/context"
 | 
						"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/account"
 | 
				
			||||||
 | 
						"git.trj.tw/golang/go-gallery/routers/album"
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -43,4 +45,9 @@ func SetDefaultRoutes(r *gin.Engine) {
 | 
				
			|||||||
		accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
 | 
							accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
 | 
				
			||||||
		accountAPI.POST("/signup", context.PatchContext(account.UserSignup))
 | 
							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))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user