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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/view/media.go | 26 +++++++++----- 2 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 pkg/view/album.go (limited to 'pkg/view') 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) +} diff --git a/pkg/view/media.go b/pkg/view/media.go index c7d84ec..3124119 100644 --- a/pkg/view/media.go +++ b/pkg/view/media.go @@ -17,12 +17,14 @@ type ( } ) -func getPagination(w http.ResponseWriter, r *http.Request) *repository.Pagination { +func getPagination(r *http.Request) *repository.Pagination { var ( - size int - page int - sizeStr = r.FormValue("size") - pageStr = r.FormValue("page") + size int + page int + albumID *uint + sizeStr = r.FormValue("size") + pageStr = r.FormValue("page") + albumIDStr = r.FormValue("albumId") ) if sizeStr == "" { @@ -41,9 +43,17 @@ func getPagination(w http.ResponseWriter, r *http.Request) *repository.Paginatio page = p } + if albumIDStr == "" { + page = 0 + } else if p, err := strconv.Atoi(albumIDStr); err == nil { + id := uint(p) + albumID = &id + } + return &repository.Pagination{ - Page: page, - Size: size, + Page: page, + Size: size, + AlbumID: albumID, } } @@ -60,7 +70,7 @@ func NewMediaView( } func (self *MediaView) Index(w http.ResponseWriter, r *http.Request) error { - p := getPagination(w, r) + p := getPagination(r) token := ext.GetTokenFromCtx(w, r) userPath, err := self.userRepository.GetPathFromUserID(r.Context(), token.UserID) -- cgit v1.2.3