123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- package pages
- import (
- "encoding/json"
- "fmt"
- "html/template"
- "net/http"
- "strings"
- "gogs.gildas.ch/gildas/movies"
- "gogs.gildas.ch/gildas/movies/omdb"
- "gopkg.in/russross/blackfriday.v2"
- )
- func Router(c *movies.Collection) http.HandlerFunc {
- homeHandler := Home(c)
- movieHandler := Movie(c)
- listHandler := List(c)
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- path := r.URL.Path
- switch {
- case path == "/":
- homeHandler(w, r)
- return
- case path == "/style.css":
- http.ServeFile(w, r, "templates/style.css")
- return
- case strings.HasPrefix(path, "/l/"):
- listHandler(w, r)
- return
- case strings.HasPrefix(path, "/tt"):
- movieHandler(w, r)
- return
- }
- })
- }
- func Home(c *movies.Collection) http.HandlerFunc {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- t, err := template.ParseFiles("templates/home.html")
- if err != nil {
- fmt.Println(err)
- return
- }
- var errs []error
- if r.Method == "POST" {
- if r.FormValue("omdb_json") != "" {
- m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
- if err != nil {
- w.WriteHeader(http.StatusBadRequest)
- errs = append(errs, err)
- } else {
- c.Add(m)
- }
- }
- if r.FormValue("list-id") != "" {
- if err := c.AddList(r.FormValue("list-id"), r.FormValue("list-title")); err != nil {
- errs = append(errs, err)
- }
- }
- }
- var omdbString string
- if imdbID := r.URL.Query().Get("imdb_id"); imdbID != "" {
- b, err := omdb.Get(imdbID)
- if err != nil {
- errs = append(errs, err)
- } else {
- omdbString = string(b)
- }
- }
- t.Execute(w, map[string]interface{}{
- "Collection": c,
- "IMDBID": r.URL.Query().Get("imdb_id"),
- "OMDBString": omdbString,
- "Errors": errs,
- })
- })
- }
- func Movie(c *movies.Collection) http.HandlerFunc {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- t, err := template.ParseFiles("templates/movie.html")
- if err != nil {
- fmt.Println(err)
- return
- }
- var errs []error
- splitted := strings.Split(r.URL.Path, "/")
- if len(splitted) < 2 {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- imdbID := splitted[1]
- m, ok := c.Get(imdbID)
- if !ok {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- if r.Method == "POST" {
- updated, err := movies.Unmarshal([]byte(r.FormValue("movie_json")))
- if err != nil {
- errs = append(errs, err)
- } else if imdbID != updated.IMDBID {
- errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
- } else {
- m = updated
- c.Update(updated)
- }
- }
- b, err := json.MarshalIndent(m, "", " ")
- if err != nil {
- errs = append(errs, err)
- }
- t.Execute(w, map[string]interface{}{
- "Movie": m,
- "MovieJSON": string(b),
- "Errors": errs,
- })
- })
- }
- func List(c *movies.Collection) http.HandlerFunc {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- t, err := template.ParseFiles("templates/list.html")
- if err != nil {
- fmt.Println(err)
- return
- }
- var errs []error
- splitted := strings.Split(r.URL.Path, "/")
- if len(splitted) < 3 {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- listID := splitted[2]
- l, ok := c.GetList(listID)
- if !ok {
- w.WriteHeader(http.StatusNotFound)
- return
- }
- if r.Method == "POST" {
- if r.FormValue("title") != "" || r.FormValue("description") != "" {
- l.Title = r.FormValue("title")
- l.Description = r.FormValue("description")
- c.UpdateList(l)
- } else if r.FormValue("imdb_id") != "" {
- l.Movies = append(l.Movies, r.FormValue("imdb_id"))
- c.UpdateList(l)
- }
- }
- b, err := json.MarshalIndent(l, "", " ")
- if err != nil {
- errs = append(errs, err)
- }
- var ms []*movies.Movie
- for _, imdbID := range l.Movies {
- if m, ok := c.Get(imdbID); ok {
- ms = append(ms, m)
- }
- }
- t.Execute(w, map[string]interface{}{
- "List": l,
- "Description": template.HTML(
- blackfriday.Run(
- []byte(strings.ReplaceAll(l.Description, "\r\n", "\n")),
- blackfriday.WithExtensions(blackfriday.CommonExtensions))),
- "Movies": ms,
- "ListJSON": string(b),
- "Errors": errs,
- })
- })
- }
|