diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-07-31 18:25:13 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-06 18:41:34 +0200 |
commit | 5f660b309bc695277c67223520499fcc13f3c59f (patch) | |
tree | ce30f46d8feebac6eb3f5145e9c772be1c32f4ad /pkg/database/sql | |
parent | 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc (diff) | |
download | lens-5f660b309bc695277c67223520499fcc13f3c59f.tar.gz lens-5f660b309bc695277c67223520499fcc13f3c59f.tar.bz2 lens-5f660b309bc695277c67223520499fcc13f3c59f.zip |
feat: Add album scanner
Diffstat (limited to 'pkg/database/sql')
-rw-r--r-- | pkg/database/sql/media.go | 122 | ||||
-rw-r--r-- | pkg/database/sql/migration.go | 2 |
2 files changed, 118 insertions, 6 deletions
diff --git a/pkg/database/sql/media.go b/pkg/database/sql/media.go index e5ba517..59e39ee 100644 --- a/pkg/database/sql/media.go +++ b/pkg/database/sql/media.go @@ -48,6 +48,22 @@ type ( Media Media } + MediaAlbum struct { + gorm.Model + ParentID *uint + Parent *MediaAlbum + Name string + Path string + } + + MediaAlbumFile struct { + gorm.Model + MediaID uint + Media Media + AlbumID uint + Album MediaAlbum + } + MediaRepository struct { db *gorm.DB } @@ -55,13 +71,13 @@ type ( var _ repository.MediaRepository = &MediaRepository{} -func (self *Media) ToModel() *repository.Media { +func (m *Media) ToModel() *repository.Media { return &repository.Media{ - ID: self.ID, - Path: self.Path, - PathHash: self.PathHash, - Name: self.Name, - MIMEType: self.MIMEType, + ID: m.ID, + Path: m.Path, + PathHash: m.PathHash, + Name: m.Name, + MIMEType: m.MIMEType, } } @@ -86,6 +102,12 @@ func (m *MediaEXIF) ToModel() *repository.MediaEXIF { } } +func (a *MediaAlbum) ToModel() *repository.Album { + return &repository.Album{ + ID: a.ID, + } +} + func (m *MediaThumbnail) ToModel() *repository.MediaThumbnail { return &repository.MediaThumbnail{ Path: m.Path, @@ -329,3 +351,91 @@ func (m *MediaRepository) CreateThumbnail(ctx context.Context, mediaID uint, thu return nil } + +func (r *MediaRepository) ListEmptyAlbums(ctx context.Context, pagination *repository.Pagination) ([]*repository.Media, error) { + medias := make([]*Media, 0) + result := r.db. + WithContext(ctx). + Model(&Media{}). + Joins("left join media_album_files on media.id = media_album_files.media_id"). + Where("media_album_files.media_id IS NULL"). + Offset(pagination.Page * pagination.Size). + Limit(pagination.Size). + Order("media.created_at DESC"). + Find(&medias) + + if result.Error != nil { + return nil, result.Error + } + + m := list.Map(medias, func(s *Media) *repository.Media { + return s.ToModel() + }) + + return m, nil +} + +func (m *MediaRepository) ExistsAlbumByAbsolutePath(ctx context.Context, path string) (bool, error) { + var exists bool + result := m.db. + WithContext(ctx). + Model(&MediaAlbum{}). + Select("count(id) > 0"). + Where("path = ?", path). + Find(&exists) + + if result.Error != nil { + return false, result.Error + } + + return exists, nil +} + +func (r *MediaRepository) GetAlbumByAbsolutePath(ctx context.Context, path string) (*repository.Album, error) { + m := &MediaAlbum{} + result := r.db. + WithContext(ctx). + Model(&MediaAlbum{}). + Where("path = ?", path). + Limit(1). + Take(m) + + if result.Error != nil { + return nil, result.Error + } + + return m.ToModel(), nil +} + +func (m *MediaRepository) CreateAlbum(ctx context.Context, createAlbum *repository.CreateAlbum) (*repository.Album, error) { + album := &MediaAlbum{ + ParentID: createAlbum.ParentID, + Name: createAlbum.Name, + Path: createAlbum.Path, + } + + result := m.db. + WithContext(ctx). + Create(album) + if result.Error != nil { + return nil, result.Error + } + + return album.ToModel(), nil +} + +func (m *MediaRepository) CreateAlbumFile(ctx context.Context, createAlbumFile *repository.CreateAlbumFile) error { + albumFile := &MediaAlbumFile{ + MediaID: createAlbumFile.MediaID, + AlbumID: createAlbumFile.AlbumID, + } + + result := m.db. + WithContext(ctx). + Create(albumFile) + if result.Error != nil { + return result.Error + } + + return nil +} diff --git a/pkg/database/sql/migration.go b/pkg/database/sql/migration.go index 076bf69..73e4297 100644 --- a/pkg/database/sql/migration.go +++ b/pkg/database/sql/migration.go @@ -9,6 +9,8 @@ func Migrate(db *gorm.DB) error { &Media{}, &MediaEXIF{}, &MediaThumbnail{}, + &MediaAlbum{}, + &MediaAlbumFile{}, } { if err := db.AutoMigrate(m); err != nil { return err |