This commit is contained in:
Jay
2018-04-25 15:50:45 +08:00
parent 8c83ceb9ea
commit 2b17fa04be
5 changed files with 152 additions and 18 deletions
+50 -3
View File
@@ -16,7 +16,6 @@ type Album struct {
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 -
@@ -56,8 +55,8 @@ func GetUserAlbums(uid string) (album []Album, err error) {
}
// 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)
func GetAlbum(id, uid string) (album *Album, err error) {
rows, err := x.Query(`selecy "id", "uid", "name", "public", "ctime", "mtime" where "id" = $1 and "uid" = $2 limit 1`, id, uid)
if err != nil {
return
}
@@ -72,3 +71,51 @@ func GetAlbum(id string) (album *Album, err error) {
return
}
// Get -
func (a *Album) Get() (err error) {
rows, err := x.Query(`select "id", "uid", "name", "public", "ctime", "mtime" from "storage"."album" where "id" = $1`, a.ID)
if err != nil {
return err
}
ok, err := dbtool.ScanToStruct(rows, a)
if err != nil {
return err
}
if !ok {
return errors.New("album data not found")
}
return
}
// Create -
func (a *Album) Create() (err error) {
if len(a.UID) == 0 || len(a.Name) == 0 {
return errors.New("album data empty")
}
rows, err := x.Query(`insert into "storage"."album" ("uid", "name", "public") values ($1, $2, $3) returning *`, a.UID, a.Name, a.Public)
if err != nil {
return err
}
defer rows.Close()
_, err = dbtool.ScanToStruct(rows, a)
if err != nil {
return err
}
return
}
// Update -
func (a *Album) Update() (err error) {
_, err = x.Exec(`update "storage"."album" set
"name" = $1,
"public" = $2
where
"id" = $3`, a.Name, a.Public, a.ID)
return
}
+6 -6
View File
@@ -4,10 +4,10 @@ import "time"
// Photo model
type Photo struct {
ID string `xorm:"id"`
Album string `xorm:"album"`
Name string `xorm:"name"`
Thumbnail string `xorm:"thumbnail"`
Ctime time.Time `xorm:"ctime"`
Mtime time.Time `xorm:"mtime"`
ID string `sql:"id" cc:"id"`
Album string `sql:"album" cc:"album"`
Name string `sql:"name" cc:"name"`
Thumbnail string `sql:"thumbnail" cc:"thumbnail"`
Ctime time.Time `sql:"ctime" cc:"ctime"`
Mtime time.Time `sql:"mtime" cc:"mtime"`
}