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
+11 -7
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 {
+65 -7
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
}