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(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, AlbumID: &album.ID, }, 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) }