diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-07-13 19:45:40 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-13 19:45:40 +0530 |
| commit | 5d4eaf85434658f6fed621016e0bc2bae59e88d1 (patch) | |
| tree | 93ec34c177b6a48d90d5024313c3a10f5b7c5d2b /internal/service | |
| parent | f41b0b92046e1bee512b3e4143ed9dc9a9ba9713 (diff) | |
| parent | 8e8f0bd38f0cc0ee435ae2ed392a206e1ea73a32 (diff) | |
| download | galleryd-5d4eaf85434658f6fed621016e0bc2bae59e88d1.tar.xz galleryd-5d4eaf85434658f6fed621016e0bc2bae59e88d1.zip | |
Merge pull request #8 from RanXom/feat/api
Feat/api: expose gallery HTTP API
Diffstat (limited to 'internal/service')
| -rw-r--r-- | internal/service/gallery.go | 2 | ||||
| -rw-r--r-- | internal/service/gallery_service.go | 14 | ||||
| -rw-r--r-- | internal/service/index.go | 9 | ||||
| -rw-r--r-- | internal/service/load.go | 33 | ||||
| -rw-r--r-- | internal/service/photo.go | 29 | ||||
| -rw-r--r-- | internal/service/service.go | 7 |
6 files changed, 90 insertions, 4 deletions
diff --git a/internal/service/gallery.go b/internal/service/gallery.go index f78e5df..30abefe 100644 --- a/internal/service/gallery.go +++ b/internal/service/gallery.go @@ -9,4 +9,6 @@ import ( type galleryService struct { scanner *scanner.Scanner builder *gallery.Builder + + index galleryIndex } diff --git a/internal/service/gallery_service.go b/internal/service/gallery_service.go index 90e3c09..e500bec 100644 --- a/internal/service/gallery_service.go +++ b/internal/service/gallery_service.go @@ -2,16 +2,22 @@ package service import ( "context" + "errors" "github.com/RanXom/galleryd/internal/gallery" ) // Gallery scans the configured roots and builds the gallery. func (s *galleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) { - files, err := s.scanner.Scan(ctx) - if err != nil { - return nil, err + select { + case <-ctx.Done(): + return nil, ctx.Err() + default: } - return s.builder.Build(ctx, files) + if s.index.byID == nil { + return nil, errors.New("gallery not loaded") + } + + return s.index.photos, nil } diff --git a/internal/service/index.go b/internal/service/index.go new file mode 100644 index 0000000..dd39169 --- /dev/null +++ b/internal/service/index.go @@ -0,0 +1,9 @@ +package service + +import "github.com/RanXom/galleryd/internal/gallery" + +// galleryIndex stores an in-memory snapshot of the gallery. +type galleryIndex struct { + photos []gallery.Photo + byID map[string]gallery.Photo +} diff --git a/internal/service/load.go b/internal/service/load.go new file mode 100644 index 0000000..09bbc20 --- /dev/null +++ b/internal/service/load.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + + "github.com/RanXom/galleryd/internal/gallery" +) + +// Load scans the configured roots and rebuilds the in-memory gallery. +func (s *galleryService) Load(ctx context.Context) error { + files, err := s.scanner.Scan(ctx) + if err != nil { + return err + } + + photos, err := s.builder.Build(ctx, files) + if err != nil { + return err + } + + index := galleryIndex{ + photos: photos, + byID: make(map[string]gallery.Photo, len(photos)), + } + + for _, photo := range photos { + index.byID[photo.ID] = photo + } + + s.index = index + + return nil +} diff --git a/internal/service/photo.go b/internal/service/photo.go new file mode 100644 index 0000000..018ba6b --- /dev/null +++ b/internal/service/photo.go @@ -0,0 +1,29 @@ +package service + +import ( + "context" + "errors" + + "github.com/RanXom/galleryd/internal/gallery" +) + +var ErrPhotoNotFound = errors.New("photo not found") + +// Photo returns a single photo by its ID. +func (s *galleryService) Photo( + ctx context.Context, + id string, +) (gallery.Photo, error) { + select { + case <-ctx.Done(): + return gallery.Photo{}, ctx.Err() + default: + } + + photo, ok := s.index.byID[id] + if !ok { + return gallery.Photo{}, ErrPhotoNotFound + } + + return photo, nil +} diff --git a/internal/service/service.go b/internal/service/service.go index 6183e26..68a764e 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -8,5 +8,12 @@ import ( // GalleryService provides gallery operations. type GalleryService interface { + // Load scans the configured roots and builds the in-memory gallery. + Load(ctx context.Context) error + + // Gallery returns the currently loaded gallery. Gallery(ctx context.Context) ([]gallery.Photo, error) + + // Photo returns a single photo by its ID. + Photo(ctx context.Context, id string) (gallery.Photo, error) } |