diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-13 21:36:42 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-08-13 21:36:42 +0200 |
commit | c51fb8cc8b850b4915e083d0dd2c30d79f8b632e (patch) | |
tree | 527ba9e4f897bc2faf997ff648aeb85cc9c247eb /pkg/view | |
parent | f99f2bc94808d784c92ec4e58c660a8be3ed7fce (diff) | |
download | lens-c51fb8cc8b850b4915e083d0dd2c30d79f8b632e.tar.gz lens-c51fb8cc8b850b4915e083d0dd2c30d79f8b632e.tar.bz2 lens-c51fb8cc8b850b4915e083d0dd2c30d79f8b632e.zip |
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 :)
Diffstat (limited to 'pkg/view')
-rw-r--r-- | pkg/view/album.go | 102 | ||||
-rw-r--r-- | pkg/view/media.go | 26 |
2 files changed, 120 insertions, 8 deletions
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) |