Files
projectreshoot/internal/handler/movie.go

62 lines
1.6 KiB
Go

package handler
import (
"net/http"
"projectreshoot/internal/config"
"projectreshoot/internal/view/page"
"strconv"
"git.haelnorr.com/h/golib/hws"
"git.haelnorr.com/h/golib/tmdb"
)
func Movie(
server *hws.Server,
config *config.Config,
) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("movie_id")
movie_id, err := strconv.ParseInt(id, 10, 32)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusBadRequest,
Message: "Movie ID provided is not valid",
Error: err,
Level: hws.ErrorDEBUG,
RenderErrorPage: true,
})
if err != nil {
server.ThrowFatal(w, err)
}
return
}
movie, err := tmdb.GetMovie(int32(movie_id), config.TMDBToken)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusInternalServerError,
Message: "An error occured when trying to retrieve the requested movie",
Error: err,
})
if err != nil {
server.ThrowFatal(w, err)
}
return
}
credits, err := tmdb.GetCredits(int32(movie_id), config.TMDBToken)
if err != nil {
err := server.ThrowError(w, r, hws.HWSError{
StatusCode: http.StatusInternalServerError,
Message: "An error occured when trying to retrieve the credits for the requested movie",
Error: err,
})
if err != nil {
server.ThrowFatal(w, err)
}
return
}
page.Movie(movie, credits, &config.TMDBConfig.Image).Render(r.Context(), w)
},
)
}