go-gallery/models/album.go

75 lines
1.6 KiB
Go

package models
import (
"database/sql"
"errors"
"time"
"git.trj.tw/golang/utils/dbtool"
)
// Album model
type Album struct {
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
}