From 7f7da8d097ffed3fe30a2ecbf4b612609603a44f Mon Sep 17 00:00:00 2001 From: Lucas BEE Date: Wed, 22 Mar 2017 10:02:59 +0000 Subject: [PATCH 1/2] Structs from db doesn't need to be exposed --- src/internal/backend/episode_torrents.go | 14 +++++++------- src/internal/backend/episodes.go | 22 +++++++++++----------- src/internal/backend/movie_torrents.go | 16 ++++++++-------- src/internal/backend/movie_wishlist.go | 24 ++++++++++++------------ src/internal/backend/movies.go | 16 ++++++++-------- src/internal/backend/show_wishlist.go | 22 +++++++++++----------- src/internal/backend/shows.go | 18 +++++++++--------- 7 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/internal/backend/episode_torrents.go b/src/internal/backend/episode_torrents.go index b8530bf..397b50c 100644 --- a/src/internal/backend/episode_torrents.go +++ b/src/internal/backend/episode_torrents.go @@ -25,8 +25,8 @@ const ( FROM episode_torrents WHERE imdb_id=$1 AND season=$2 AND episode=$3;` ) -// EpisodeTorrentDB represents the EpisodeTorrent in the DB -type EpisodeTorrentDB struct { +// episodeTorrentDB represents the EpisodeTorrent in the DB +type episodeTorrentDB struct { ID string `db:"id"` ImdbID string `db:"imdb_id"` URL string `db:"url"` @@ -43,7 +43,7 @@ type EpisodeTorrentDB struct { // NewTorrentFromEpisodeTorrentDB returns a polochon.Torrent from an // episodeTorrentDB -func NewTorrentFromEpisodeTorrentDB(eDB *EpisodeTorrentDB) *polochon.Torrent { +func NewTorrentFromEpisodeTorrentDB(eDB *episodeTorrentDB) *polochon.Torrent { q, _ := polochon.StringToQuality(eDB.Quality) return &polochon.Torrent{ Quality: *q, @@ -55,10 +55,10 @@ func NewTorrentFromEpisodeTorrentDB(eDB *EpisodeTorrentDB) *polochon.Torrent { } } -// NewEpisodeTorrentDB returns an EpisodeTorrentDB ready to be put in DB from a +// NewEpisodeTorrentDB returns an episodeTorrentDB ready to be put in DB from a // polochon.Torrent -func NewEpisodeTorrentDB(t *polochon.Torrent, imdbID string, season, episode int) *EpisodeTorrentDB { - return &EpisodeTorrentDB{ +func NewEpisodeTorrentDB(t *polochon.Torrent, imdbID string, season, episode int) *episodeTorrentDB { + return &episodeTorrentDB{ ImdbID: imdbID, Season: season, Episode: episode, @@ -73,7 +73,7 @@ func NewEpisodeTorrentDB(t *polochon.Torrent, imdbID string, season, episode int // GetEpisodeTorrents returns show episodes torrents from database func GetEpisodeTorrents(db *sqlx.DB, imdbID string, season, episode int) ([]polochon.Torrent, error) { - var torrentsDB = []*EpisodeTorrentDB{} + var torrentsDB = []*episodeTorrentDB{} err := db.Select(&torrentsDB, getEpisodeTorrentQuery, imdbID, season, episode) if err != nil { return nil, err diff --git a/src/internal/backend/episodes.go b/src/internal/backend/episodes.go index afa42a7..4142da7 100644 --- a/src/internal/backend/episodes.go +++ b/src/internal/backend/episodes.go @@ -28,8 +28,8 @@ const ( FROM episodes WHERE show_imdb_id=$1 AND season=$2 AND episode=$3;` ) -// EpisodeDB represents the Episode in the DB -type EpisodeDB struct { +// episodeDB represents the Episode in the DB +type episodeDB struct { ID string `db:"id"` TvdbID int `db:"tvdb_id"` ImdbID string `db:"imdb_id"` @@ -48,9 +48,9 @@ type EpisodeDB struct { Updated time.Time `db:"updated_at"` } -// NewEpisodeFromPolochon returns an EpisodeDB from a polochon ShowEpisode -func NewEpisodeFromPolochon(e *polochon.ShowEpisode) *EpisodeDB { - return &EpisodeDB{ +// NewEpisodeFromPolochon returns an episodeDB from a polochon ShowEpisode +func NewEpisodeFromPolochon(e *polochon.ShowEpisode) *episodeDB { + return &episodeDB{ TvdbID: e.TvdbID, ImdbID: e.EpisodeImdbID, ShowImdbID: e.ShowImdbID, @@ -67,15 +67,15 @@ func NewEpisodeFromPolochon(e *polochon.ShowEpisode) *EpisodeDB { } } -// NewEpisodeFromDB returns a new polochon ShowEpisode from an EpisodeDB -func NewEpisodeFromDB(eDB *EpisodeDB) *polochon.ShowEpisode { +// NewEpisodeFromDB returns a new polochon ShowEpisode from an episodeDB +func NewEpisodeFromDB(eDB *episodeDB) *polochon.ShowEpisode { pEpisode := polochon.ShowEpisode{} FillEpisodeFromDB(eDB, &pEpisode) return &pEpisode } -// FillEpisodeFromDB fills a ShowEpisode from an EpisodeDB -func FillEpisodeFromDB(eDB *EpisodeDB, pEpisode *polochon.ShowEpisode) { +// FillEpisodeFromDB fills a ShowEpisode from an episodeDB +func FillEpisodeFromDB(eDB *episodeDB, pEpisode *polochon.ShowEpisode) { pEpisode.TvdbID = eDB.TvdbID pEpisode.EpisodeImdbID = eDB.ImdbID pEpisode.ShowImdbID = eDB.ShowImdbID @@ -92,7 +92,7 @@ func FillEpisodeFromDB(eDB *EpisodeDB, pEpisode *polochon.ShowEpisode) { // GetEpisode gets an episode and fills the polochon episode func GetEpisode(db *sqlx.DB, pEpisode *polochon.ShowEpisode) error { - var episodeDB EpisodeDB + var episodeDB episodeDB err := db.QueryRowx(getEpisodeQuery, pEpisode.ShowImdbID, pEpisode.Season, pEpisode.Episode).StructScan(&episodeDB) if err != nil { return err @@ -106,7 +106,7 @@ func GetEpisode(db *sqlx.DB, pEpisode *polochon.ShowEpisode) error { // GetEpisodes gets show's episodes and fills the polochon show func GetEpisodes(db *sqlx.DB, pShow *polochon.Show, log *logrus.Entry) error { // Get the episodes - var episodesDB = []*EpisodeDB{} + var episodesDB = []*episodeDB{} err := db.Select(&episodesDB, getEpisodesQuery, pShow.ImdbID) if err != nil { return err diff --git a/src/internal/backend/movie_torrents.go b/src/internal/backend/movie_torrents.go index 071cd67..3ea90b3 100644 --- a/src/internal/backend/movie_torrents.go +++ b/src/internal/backend/movie_torrents.go @@ -24,8 +24,8 @@ const ( FROM movie_torrents WHERE imdb_id=$1;` ) -// MovieTorrentDB represents the MovieTorrent in the DB -type MovieTorrentDB struct { +// movieTorrentDB represents the MovieTorrent in the DB +type movieTorrentDB struct { ID string `db:"id"` ImdbID string `db:"imdb_id"` URL string `db:"url"` @@ -39,8 +39,8 @@ type MovieTorrentDB struct { } // NewTorrentFromMovieTorrentDB creates a new polochon.Torrent from a -// MovieTorrentDB -func NewTorrentFromMovieTorrentDB(mDB *MovieTorrentDB) *polochon.Torrent { +// movieTorrentDB +func NewTorrentFromMovieTorrentDB(mDB *movieTorrentDB) *polochon.Torrent { q, _ := polochon.StringToQuality(mDB.Quality) return &polochon.Torrent{ Quality: *q, @@ -54,8 +54,8 @@ func NewTorrentFromMovieTorrentDB(mDB *MovieTorrentDB) *polochon.Torrent { // NewMovieTorrentDB returns a MovieTorrent ready to be put in DB from a // Torrent -func NewMovieTorrentDB(t *polochon.Torrent, imdbID string) MovieTorrentDB { - return MovieTorrentDB{ +func NewMovieTorrentDB(t *polochon.Torrent, imdbID string) movieTorrentDB { + return movieTorrentDB{ ImdbID: imdbID, URL: t.URL, Source: t.Source, @@ -68,7 +68,7 @@ func NewMovieTorrentDB(t *polochon.Torrent, imdbID string) MovieTorrentDB { // GetMovieTorrents returns polochon.Torrents from the database func GetMovieTorrents(db *sqlx.DB, imdbID string) ([]polochon.Torrent, error) { - var torrentsDB = []*MovieTorrentDB{} + var torrentsDB = []*movieTorrentDB{} // Get the torrents from the DB err := db.Select(&torrentsDB, getMovieTorrentQueryByImdbID, imdbID) if err != nil { @@ -79,7 +79,7 @@ func GetMovieTorrents(db *sqlx.DB, imdbID string) ([]polochon.Torrent, error) { return nil, sql.ErrNoRows } - // Create polochon Torrents from the MovieTorrentDB + // Create polochon Torrents from the movieTorrentDB var torrents []polochon.Torrent for _, torrentDB := range torrentsDB { torrent := NewTorrentFromMovieTorrentDB(torrentDB) diff --git a/src/internal/backend/movie_wishlist.go b/src/internal/backend/movie_wishlist.go index 7def493..a52f75c 100644 --- a/src/internal/backend/movie_wishlist.go +++ b/src/internal/backend/movie_wishlist.go @@ -33,25 +33,25 @@ const ( deleteMovieWishlistedQueryByID = `DELETE FROM movies_tracked WHERE imdb_id=$1 AND user_id=$2;` ) -// MovieWishlist represents the list of movies wishlisted by a user -type MovieWishlist struct { +// movieWishlist represents the list of movies wishlisted by a user +type movieWishlist struct { movies map[string]struct{} } -// NewMovieWishlist returns a new MovieWishlist -func NewMovieWishlist() *MovieWishlist { - return &MovieWishlist{ +// NewMovieWishlist returns a new movieWishlist +func NewMovieWishlist() *movieWishlist { + return &movieWishlist{ movies: map[string]struct{}{}, } } -// add adds movie to the MovieWishlist -func (w *MovieWishlist) add(imdbID string) { +// add adds movie to the movieWishlist +func (w *movieWishlist) add(imdbID string) { w.movies[imdbID] = struct{}{} } // List return a slice of imdbID wishlisted -func (w *MovieWishlist) List() []string { +func (w *movieWishlist) List() []string { movies := make([]string, len(w.movies)) i := 0 @@ -63,15 +63,15 @@ func (w *MovieWishlist) List() []string { return movies } -// GetMovieWishlist returns a MovieWishlist obejct for a user -func GetMovieWishlist(db *sqlx.DB, userID string) (*MovieWishlist, error) { +// GetMovieWishlist returns a movieWishlist obejct for a user +func GetMovieWishlist(db *sqlx.DB, userID string) (*movieWishlist, error) { var movies = []string{} // Get the list of movies err := db.Select(&movies, getMovieWishlistQueryByUserID, userID) if err != nil { return nil, err } - // Create a new MovieWishlist + // Create a new movieWishlist wishlist := NewMovieWishlist() for _, imdbID := range movies { // add the movie to the wishlist object @@ -82,7 +82,7 @@ func GetMovieWishlist(db *sqlx.DB, userID string) (*MovieWishlist, error) { } // IsMovieInWishlist returns true if the movie is in the wishlist -func (w *MovieWishlist) IsMovieInWishlist(imdbID string) bool { +func (w *movieWishlist) IsMovieInWishlist(imdbID string) bool { _, ok := w.movies[imdbID] return ok } diff --git a/src/internal/backend/movies.go b/src/internal/backend/movies.go index a987328..ef5854d 100644 --- a/src/internal/backend/movies.go +++ b/src/internal/backend/movies.go @@ -29,8 +29,8 @@ const ( WHERE imdb_id=$1;` ) -// MovieDB represents the Movie in the DB -type MovieDB struct { +// movieDB represents the Movie in the DB +type movieDB struct { ID string `db:"id"` ImdbID string `db:"imdb_id"` TmdbID int `db:"tmdb_id"` @@ -51,7 +51,7 @@ type MovieDB struct { // GetMovie fills show details of a polochon.Movie func GetMovie(db *sqlx.DB, pMovie *polochon.Movie) error { - var mDB MovieDB + var mDB movieDB var err error if pMovie.ImdbID != "" { // Get the data from the DB @@ -63,13 +63,13 @@ func GetMovie(db *sqlx.DB, pMovie *polochon.Movie) error { return err } - // Fills the polochon.Movie from the MovieDB + // Fills the polochon.Movie from the movieDB FillFromDB(&mDB, pMovie) return nil } -// FillFromDB fills a Movie from a MovieDB extracted from the DB -func FillFromDB(mDB *MovieDB, pMovie *polochon.Movie) { +// FillFromDB fills a Movie from a movieDB extracted from the DB +func FillFromDB(mDB *movieDB, pMovie *polochon.Movie) { pMovie.ImdbID = mDB.ImdbID pMovie.Title = mDB.Title pMovie.Rating = mDB.Rating @@ -98,12 +98,12 @@ func UpsertMovie(db *sqlx.DB, pMovie *polochon.Movie) error { // NewMovieDB returns a Movie ready to be put in DB from a // polochon Movie -func NewMovieDB(m *polochon.Movie) MovieDB { +func NewMovieDB(m *polochon.Movie) movieDB { genres := []string{} if m.Genres != nil { genres = m.Genres } - return MovieDB{ + return movieDB{ ImdbID: m.ImdbID, Title: m.Title, Rating: m.Rating, diff --git a/src/internal/backend/show_wishlist.go b/src/internal/backend/show_wishlist.go index 33c9d8c..931a75d 100644 --- a/src/internal/backend/show_wishlist.go +++ b/src/internal/backend/show_wishlist.go @@ -33,8 +33,8 @@ const ( deleteShowWishlistedQueryByID = `DELETE FROM shows_tracked WHERE imdb_id=$1 AND user_id=$2;` ) -// ShowWishlist represents the show wishlist of a user -type ShowWishlist struct { +// showWishlist represents the show wishlist of a user +type showWishlist struct { shows map[string]*WishedShow } @@ -45,15 +45,15 @@ type WishedShow struct { Episode int } -// NewShowWishlist returns a new ShowWishlist -func NewShowWishlist() *ShowWishlist { - return &ShowWishlist{ +// NewShowWishlist returns a new showWishlist +func NewShowWishlist() *showWishlist { + return &showWishlist{ shows: map[string]*WishedShow{}, } } -// add adds a show to the ShowWishlist object -func (w *ShowWishlist) add(imdbID string, season, episode int) { +// add adds a show to the showWishlist object +func (w *showWishlist) add(imdbID string, season, episode int) { w.shows[imdbID] = &WishedShow{ ImdbID: imdbID, Season: season, @@ -62,7 +62,7 @@ func (w *ShowWishlist) add(imdbID string, season, episode int) { } // List return a slice of WishedShow -func (w *ShowWishlist) List() []*WishedShow { +func (w *showWishlist) List() []*WishedShow { shows := make([]*WishedShow, len(w.shows)) i := 0 @@ -78,8 +78,8 @@ func (w *ShowWishlist) List() []*WishedShow { return shows } -// GetShowWishlist returns a ShowWishlist for a user -func GetShowWishlist(db *sqlx.DB, userID string) (*ShowWishlist, error) { +// GetShowWishlist returns a showWishlist for a user +func GetShowWishlist(db *sqlx.DB, userID string) (*showWishlist, error) { var wishedShows = []*WishedShow{} // Get the wishlisted shows err := db.Select(&wishedShows, getShowWishlistQueryByUserID, userID) @@ -98,7 +98,7 @@ func GetShowWishlist(db *sqlx.DB, userID string) (*ShowWishlist, error) { // IsShowInWishlist returns true and the WishedShow if the show is in the // wishlist -func (w *ShowWishlist) IsShowInWishlist(imdbID string) (*WishedShow, bool) { +func (w *showWishlist) IsShowInWishlist(imdbID string) (*WishedShow, bool) { show, ok := w.shows[imdbID] return show, ok } diff --git a/src/internal/backend/shows.go b/src/internal/backend/shows.go index bb11213..0abe958 100644 --- a/src/internal/backend/shows.go +++ b/src/internal/backend/shows.go @@ -23,8 +23,8 @@ const ( FROM shows WHERE imdb_id=$1;` ) -// ShowDB represents the Show in the DB -type ShowDB struct { +// showDB represents the Show in the DB +type showDB struct { ID string `db:"id"` ImdbID string `db:"imdb_id"` TvdbID int `db:"tvdb_id"` @@ -59,9 +59,9 @@ func UpsertShow(db *sqlx.DB, s *polochon.Show) error { return nil } -// NewShowFromPolochon returns an ShowDB from a polochon Show -func NewShowFromPolochon(s *polochon.Show) *ShowDB { - sDB := ShowDB{ +// NewShowFromPolochon returns an showDB from a polochon Show +func NewShowFromPolochon(s *polochon.Show) *showDB { + sDB := showDB{ ImdbID: s.ImdbID, TvdbID: s.TvdbID, Title: s.Title, @@ -78,7 +78,7 @@ func NewShowFromPolochon(s *polochon.Show) *ShowDB { // GetShow fills a show from the DB func GetShow(db *sqlx.DB, pShow *polochon.Show) error { var err error - var sDB ShowDB + var sDB showDB if pShow.ImdbID != "" { err = db.QueryRowx(getShowQueryByImdbID, pShow.ImdbID).StructScan(&sDB) } else { @@ -88,13 +88,13 @@ func GetShow(db *sqlx.DB, pShow *polochon.Show) error { return err } - // Fill the show from the ShowDB + // Fill the show from the showDB FillShowFromDB(&sDB, pShow) return nil } -// FillShowFromDB returns a Show from a ShowDB extracted from the DB -func FillShowFromDB(sDB *ShowDB, pShow *polochon.Show) { +// FillShowFromDB returns a Show from a showDB extracted from the DB +func FillShowFromDB(sDB *showDB, pShow *polochon.Show) { pShow.ImdbID = sDB.ImdbID pShow.Title = sDB.Title pShow.Rating = sDB.Rating From f042987d10616cadd2510c9c91bc10c58272f8cf Mon Sep 17 00:00:00 2001 From: Lucas BEE Date: Wed, 22 Mar 2017 10:03:26 +0000 Subject: [PATCH 2/2] Add routes to show explorer's options --- src/internal/backend/explorer.go | 25 +++++++++++++++++- .../external_medias/external_medias.go | 26 +++++++++++++++++++ src/internal/external_medias/handlers.go | 22 ++++++++++++++++ src/routes.go | 2 ++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/internal/backend/explorer.go b/src/internal/backend/explorer.go index d59d9e7..c229649 100644 --- a/src/internal/backend/explorer.go +++ b/src/internal/backend/explorer.go @@ -15,7 +15,12 @@ const ( DO UPDATE SET type=:type, source=:source, category=:category, ids=:ids RETURNING id;` - getExternalMediaQuery = `SELECT * FROM external_medias WHERE type=$1 AND source=$2 AND category=$3 LIMIT 1;` + getExternalMediaQuery = `SELECT * FROM external_medias WHERE type=$1 AND source=$2 AND category=$3 LIMIT 1;` + getExternalMediaOptions = ` + SELECT + source, category + FROM external_medias + WHERE type=$1;` ) // Media represents an external media @@ -46,3 +51,21 @@ func (m *Media) Upsert(db *sqlx.DB) error { defer r.Close() return nil } + +func GetMediaOptions(db *sqlx.DB, mtype string) (map[string][]string, error) { + type mediaAvailable struct { + Source string `db:"source"` + Category string `db:"category"` + } + m := []*mediaAvailable{} + if err := db.Select(&m, getExternalMediaOptions, mtype); err != nil { + return nil, err + } + + availableMedia := map[string][]string{} + for _, p := range m { + availableMedia[p.Source] = append(availableMedia[p.Source], p.Category) + } + + return availableMedia, nil +} diff --git a/src/internal/external_medias/external_medias.go b/src/internal/external_medias/external_medias.go index 6d8bb5f..7bfa1a3 100644 --- a/src/internal/external_medias/external_medias.go +++ b/src/internal/external_medias/external_medias.go @@ -185,3 +185,29 @@ func Refresh(env *web.Env) { RefreshMovies(env) env.Log.Debugf("done refreshing movies") } + +// GetShowOptions will get show explorer options available +func GetShowOptions(env *web.Env) (map[string][]string, error) { + log := env.Log.WithFields(logrus.Fields{ + "function": "extmedias.GetShowOptions", + }) + log.Debugf("getting explorer show options") + e, err := backend.GetMediaOptions(env.Database, "show") + if err != nil { + return nil, err + } + return e, nil +} + +// GetMovieOptions will get movie explorer options available +func GetMovieOptions(env *web.Env) (map[string][]string, error) { + log := env.Log.WithFields(logrus.Fields{ + "function": "extmedias.GetMovieOptions", + }) + log.Debugf("getting explorer movie options") + e, err := backend.GetMediaOptions(env.Database, "movie") + if err != nil { + return nil, err + } + return e, nil +} diff --git a/src/internal/external_medias/handlers.go b/src/internal/external_medias/handlers.go index 76b5511..116b1d2 100644 --- a/src/internal/external_medias/handlers.go +++ b/src/internal/external_medias/handlers.go @@ -227,3 +227,25 @@ func ExploreShows(env *web.Env, w http.ResponseWriter, r *http.Request) error { return env.RenderJSON(w, shows) } + +// MovieExplorerOptions will return all the explore movie options available +func MovieExplorerOptions(env *web.Env, w http.ResponseWriter, r *http.Request) error { + // Get the list of movie explorer options available + movieOptions, err := GetMovieOptions(env) + if err != nil { + return env.RenderError(w, err) + } + + return env.RenderJSON(w, movieOptions) +} + +// ShowExplorerOptions will return all the explore show options available +func ShowExplorerOptions(env *web.Env, w http.ResponseWriter, r *http.Request) error { + // Get the list of show explorer options available + showOptions, err := GetShowOptions(env) + if err != nil { + return env.RenderError(w, err) + } + + return env.RenderJSON(w, showOptions) +} diff --git a/src/routes.go b/src/routes.go index ef5fc48..8df1a12 100644 --- a/src/routes.go +++ b/src/routes.go @@ -19,6 +19,7 @@ func setupRoutes(env *web.Env) { // Movies routes env.Handle("/movies/polochon", movies.PolochonMoviesHandler).WithRole(users.UserRole).Methods("GET") env.Handle("/movies/explore", extmedias.ExploreMovies).WithRole(users.UserRole).Methods("GET") + env.Handle("/movies/explore/options", extmedias.MovieExplorerOptions).WithRole(users.UserRole).Methods("GET") env.Handle("/movies/search", movies.SearchMovie).WithRole(users.UserRole).Methods("POST") env.Handle("/movies/{id:tt[0-9]+}", movies.PolochonDeleteHandler).WithRole(users.AdminRole).Methods("DELETE") env.Handle("/movies/{id:tt[0-9]+}/refresh", movies.RefreshMovieHandler).WithRole(users.UserRole).Methods("POST") @@ -27,6 +28,7 @@ func setupRoutes(env *web.Env) { // Shows routes env.Handle("/shows/polochon", shows.PolochonShowsHandler).WithRole(users.UserRole).Methods("GET") env.Handle("/shows/explore", extmedias.ExploreShows).WithRole(users.UserRole).Methods("GET") + env.Handle("/shows/explore/options", extmedias.ShowExplorerOptions).WithRole(users.UserRole).Methods("GET") env.Handle("/shows/search", shows.SearchShow).WithRole(users.UserRole).Methods("POST") env.Handle("/shows/{id:tt[0-9]+}", shows.GetDetailsHandler).WithRole(users.UserRole).Methods("GET") env.Handle("/shows/{id:tt[0-9]+}/refresh", shows.RefreshShowHandler).WithRole(users.UserRole).Methods("POST")