From c51fb8cc8b850b4915e083d0dd2c30d79f8b632e Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 13 Aug 2023 21:36:42 +0200 Subject: feat: Add (yet again) crude album implementation This is a initial UI album implementation. This should cover the most basic album navigation. This is still plenty to do :) --- pkg/database/sql/media.go | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'pkg/database/sql/media.go') 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 +} -- cgit v1.2.3