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/view/album.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 pkg/view/album.go (limited to 'pkg/view/album.go') diff --git a/pkg/view/album.go b/pkg/view/album.go new file mode 100644 index 0000000..a96b9bd --- /dev/null +++ b/pkg/view/album.go @@ -0,0 +1,102 @@ +package view + +import ( + "net/http" + + "git.sr.ht/~gabrielgio/img/pkg/database/repository" + "git.sr.ht/~gabrielgio/img/pkg/ext" + "git.sr.ht/~gabrielgio/img/templates" +) + +type ( + AlbumView struct { + mediaRepository repository.MediaRepository + userRepository repository.UserRepository + settingsRepository repository.SettingsRepository + } +) + +func NewAlbumView( + mediaRepository repository.MediaRepository, + userRepository repository.UserRepository, + settingsRepository repository.SettingsRepository, +) *AlbumView { + return &AlbumView{ + mediaRepository: mediaRepository, + userRepository: userRepository, + settingsRepository: settingsRepository, + } +} + +func (self *AlbumView) Index(w http.ResponseWriter, r *http.Request) error { + p := getPagination(r) + token := ext.GetTokenFromCtx(w, r) + + // TODO: optmize call, GetPathFromUserID may no be necessary + userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID) + if err != nil { + return err + } + + var albums []*repository.Album + var album *repository.Album + + if p.AlbumID == nil { + // use user path as default value + p.Path = userPath + + album, err = self.mediaRepository.GetAlbumByAbsolutePath(r.Context(), p.Path) + if err != nil { + return err + } + + albums, err = self.mediaRepository.ListAlbums(r.Context(), album.ID) + if err != nil { + return err + } + } else { + album, err = self.mediaRepository.GetAlbum(r.Context(), *p.AlbumID) + if err != nil { + return err + } + + // TODO: User can enter a album out of its bounderies + p.Path = album.Path + + albums, err = self.mediaRepository.ListAlbums(r.Context(), *p.AlbumID) + if err != nil { + return err + } + + } + + medias, err := self.mediaRepository.List(r.Context(), p) + if err != nil { + return err + } + + settings, err := self.settingsRepository.Load(r.Context()) + if err != nil { + return err + } + + page := &templates.AlbumPage{ + Medias: medias, + Albums: albums, + Name: album.Name, + Next: &repository.Pagination{ + Size: p.Size, + Page: p.Page + 1, + }, + Settings: settings, + } + + templates.WritePageTemplate(w, page) + + return nil +} + +func (self *AlbumView) SetMyselfIn(r *ext.Router) { + r.GET("/album/", self.Index) + r.POST("/album/", self.Index) +} -- cgit v1.2.3