aboutsummaryrefslogtreecommitdiff
path: root/pkg/database
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/database')
-rw-r--r--pkg/database/repository/media.go13
-rw-r--r--pkg/database/sql/media.go46
2 files changed, 50 insertions, 9 deletions
diff --git a/pkg/database/repository/media.go b/pkg/database/repository/media.go
index d6addbf..9915c90 100644
--- a/pkg/database/repository/media.go
+++ b/pkg/database/repository/media.go
@@ -35,7 +35,9 @@ type (
}
Album struct {
- ID uint
+ ID uint
+ Name string
+ Path string
}
MediaThumbnail struct {
@@ -43,9 +45,10 @@ type (
}
Pagination struct {
- Page int
- Size int
- Path string
+ Page int
+ Size int
+ AlbumID *uint
+ Path string
}
CreateMedia struct {
@@ -83,8 +86,10 @@ type (
CreateThumbnail(context.Context, uint, *MediaThumbnail) error
ListEmptyAlbums(context.Context, *Pagination) ([]*Media, error)
+ ListAlbums(context.Context, uint) ([]*Album, error)
ExistsAlbumByAbsolutePath(context.Context, string) (bool, error)
GetAlbumByAbsolutePath(context.Context, string) (*Album, error)
+ GetAlbum(context.Context, uint) (*Album, error)
CreateAlbum(context.Context, *CreateAlbum) (*Album, error)
CreateAlbumFile(context.Context, *CreateAlbumFile) error
}
diff --git a/pkg/database/sql/media.go b/pkg/database/sql/media.go
index 59e39ee..4b48608 100644
--- a/pkg/database/sql/media.go
+++ b/pkg/database/sql/media.go
@@ -23,7 +23,7 @@ type (
gorm.Model
Width *float64
Height *float64
- MediaID uint
+ MediaID uint `gorm:"not null"`
Media Media
Description *string
Camera *string
@@ -43,8 +43,8 @@ type (
MediaThumbnail struct {
gorm.Model
- Path string
- MediaID uint
+ Path string `gorm:"not null;unique"`
+ MediaID uint `gorm:"not null"`
Media Media
}
@@ -53,7 +53,7 @@ type (
ParentID *uint
Parent *MediaAlbum
Name string
- Path string
+ Path string `gorm:"not null; unique"`
}
MediaAlbumFile struct {
@@ -104,7 +104,9 @@ func (m *MediaEXIF) ToModel() *repository.MediaEXIF {
func (a *MediaAlbum) ToModel() *repository.Album {
return &repository.Album{
- ID: a.ID,
+ ID: a.ID,
+ Name: a.Name,
+ Path: a.Path,
}
}
@@ -407,6 +409,22 @@ func (r *MediaRepository) GetAlbumByAbsolutePath(ctx context.Context, path strin
return m.ToModel(), nil
}
+func (r *MediaRepository) GetAlbum(ctx context.Context, albumID uint) (*repository.Album, error) {
+ m := &MediaAlbum{}
+ result := r.db.
+ WithContext(ctx).
+ Model(&MediaAlbum{}).
+ Where("id = ?", albumID).
+ 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,
@@ -439,3 +457,21 @@ func (m *MediaRepository) CreateAlbumFile(ctx context.Context, createAlbumFile *
return nil
}
+
+func (m *MediaRepository) ListAlbums(ctx context.Context, albumID uint) ([]*repository.Album, error) {
+ albums := make([]*MediaAlbum, 0)
+
+ result := m.db.
+ WithContext(ctx).
+ Model(&MediaAlbum{}).
+ Where("parent_id = ?", albumID).
+ Find(&albums)
+
+ if result.Error != nil {
+ return nil, result.Error
+ }
+
+ return list.Map(albums, func(a *MediaAlbum) *repository.Album {
+ return a.ToModel()
+ }), nil
+}