2014-06-28 23:07:34 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/leanote/leanote/app/info"
|
2015-11-13 17:58:41 +08:00
|
|
|
// . "github.com/leanote/leanote/app/lea"
|
2014-06-28 23:07:34 +08:00
|
|
|
"github.com/leanote/leanote/app/db"
|
2014-09-02 15:45:44 +08:00
|
|
|
"gopkg.in/mgo.v2/bson"
|
2014-06-28 23:07:34 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const IMAGE_TYPE = 0
|
|
|
|
|
|
|
|
type AlbumService struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// add album
|
|
|
|
func (this *AlbumService) AddAlbum(album info.Album) bool {
|
|
|
|
album.CreatedTime = time.Now()
|
|
|
|
album.Type = IMAGE_TYPE
|
|
|
|
return db.Insert(db.Albums, album)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get albums
|
|
|
|
func (this *AlbumService) GetAlbums(userId string) []info.Album {
|
|
|
|
albums := []info.Album{}
|
|
|
|
db.ListByQ(db.Albums, bson.M{"UserId": bson.ObjectIdHex(userId)}, &albums)
|
|
|
|
return albums
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete album
|
|
|
|
// presupposition: has no images under this ablum
|
|
|
|
func (this *AlbumService) DeleteAlbum(userId, albumId string) (bool, string) {
|
2015-11-13 17:58:41 +08:00
|
|
|
if db.Count(db.Files, bson.M{"AlbumId": bson.ObjectIdHex(albumId),
|
2014-06-28 23:07:34 +08:00
|
|
|
"UserId": bson.ObjectIdHex(userId),
|
2015-11-13 17:58:41 +08:00
|
|
|
}) == 0 {
|
2014-06-28 23:07:34 +08:00
|
|
|
return db.DeleteByIdAndUserId(db.Albums, albumId, userId), ""
|
|
|
|
}
|
|
|
|
return false, "has images"
|
|
|
|
}
|
|
|
|
|
|
|
|
// update album name
|
|
|
|
func (this *AlbumService) UpdateAlbum(albumId, userId, name string) bool {
|
|
|
|
return db.UpdateByIdAndUserIdField(db.Albums, albumId, userId, "Name", name)
|
2015-11-13 17:58:41 +08:00
|
|
|
}
|