photoprism/query.go

47 lines
1,023 B
Go
Raw Normal View History

2018-08-07 18:17:14 +00:00
package photoprism
import (
"github.com/jinzhu/gorm"
)
type Search struct {
originalsPath string
db *gorm.DB
}
func NewQuery(originalsPath string, db *gorm.DB) *Search {
instance := &Search{
originalsPath: originalsPath,
db: db,
}
return instance
}
2018-09-06 12:47:32 +00:00
func (s *Search) FindPhotos(query string, count int, offset int) (photos []Photo) {
2018-08-09 21:10:05 +00:00
q := s.db.Preload("Tags").Preload("Files").Preload("Location").Preload("Albums")
if query != "" {
q = q.Joins("JOIN photo_tags ON photo_tags.photo_id=photos.id")
q = q.Joins("JOIN tags ON photo_tags.tag_id=tags.id")
2018-09-06 12:47:32 +00:00
q = q.Where("tags.label LIKE ?", "%"+query+"%")
2018-08-09 21:10:05 +00:00
}
q = q.Where(&Photo{Deleted: false}).Order("taken_at").Limit(count).Offset(offset)
q = q.Find(&photos)
2018-08-07 18:17:14 +00:00
return photos
}
2018-09-06 12:47:32 +00:00
func (s *Search) FindFiles(count int, offset int) (files []File) {
2018-08-07 18:17:14 +00:00
s.db.Where(&File{}).Limit(count).Offset(offset).Find(&files)
return files
}
2018-09-06 12:47:32 +00:00
func (s *Search) FindFile(id string) (file File) {
2018-08-07 18:17:14 +00:00
s.db.Where("id = ?", id).First(&file)
return file
2018-09-06 12:47:32 +00:00
}