pages.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package pages
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "strings"
  8. "gogs.gildas.ch/gildas/movies"
  9. "gogs.gildas.ch/gildas/movies/omdb"
  10. )
  11. func Router(c *movies.Collection) http.HandlerFunc {
  12. homeHandler := Home(c)
  13. movieHandler := Movie(c)
  14. listHandler := List(c)
  15. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  16. path := r.URL.Path
  17. switch {
  18. case path == "/":
  19. homeHandler(w, r)
  20. return
  21. case path == "/style.css":
  22. http.ServeFile(w, r, "templates/style.css")
  23. return
  24. case strings.HasPrefix(path, "/l/"):
  25. listHandler(w, r)
  26. return
  27. case strings.HasPrefix(path, "/tt"):
  28. movieHandler(w, r)
  29. return
  30. }
  31. })
  32. }
  33. func Home(c *movies.Collection) http.HandlerFunc {
  34. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  35. t, err := template.ParseFiles("templates/home.html")
  36. if err != nil {
  37. fmt.Println(err)
  38. return
  39. }
  40. var errs []error
  41. if r.Method == "POST" {
  42. if r.FormValue("omdb_json") != "" {
  43. m, err := movies.Unmarshal([]byte(r.FormValue("omdb_json")))
  44. if err != nil {
  45. w.WriteHeader(http.StatusBadRequest)
  46. errs = append(errs, err)
  47. } else {
  48. c.Add(m)
  49. }
  50. }
  51. if r.FormValue("list-id") != "" {
  52. if err := c.AddList(r.FormValue("list-id"), r.FormValue("list-title")); err != nil {
  53. errs = append(errs, err)
  54. }
  55. }
  56. }
  57. var omdbString string
  58. if imdbID := r.URL.Query().Get("imdb_id"); imdbID != "" {
  59. b, err := omdb.Get(imdbID)
  60. if err != nil {
  61. errs = append(errs, err)
  62. } else {
  63. omdbString = string(b)
  64. }
  65. }
  66. t.Execute(w, map[string]interface{}{
  67. "Collection": c,
  68. "IMDBID": r.URL.Query().Get("imdb_id"),
  69. "OMDBString": omdbString,
  70. "Errors": errs,
  71. })
  72. })
  73. }
  74. func Movie(c *movies.Collection) http.HandlerFunc {
  75. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  76. t, err := template.ParseFiles("templates/movie.html")
  77. if err != nil {
  78. fmt.Println(err)
  79. return
  80. }
  81. var errs []error
  82. splitted := strings.Split(r.URL.Path, "/")
  83. if len(splitted) < 2 {
  84. w.WriteHeader(http.StatusNotFound)
  85. return
  86. }
  87. imdbID := splitted[1]
  88. m, ok := c.Get(imdbID)
  89. if !ok {
  90. w.WriteHeader(http.StatusNotFound)
  91. return
  92. }
  93. if r.Method == "POST" {
  94. updated, err := movies.Unmarshal([]byte(r.FormValue("movie_json")))
  95. if err != nil {
  96. errs = append(errs, err)
  97. } else if imdbID != updated.IMDBID {
  98. errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
  99. } else {
  100. m = updated
  101. c.Update(updated)
  102. }
  103. }
  104. b, err := json.MarshalIndent(m, "", " ")
  105. if err != nil {
  106. errs = append(errs, err)
  107. }
  108. t.Execute(w, map[string]interface{}{
  109. "Movie": m,
  110. "MovieJSON": string(b),
  111. "Errors": errs,
  112. })
  113. })
  114. }
  115. func List(c *movies.Collection) http.HandlerFunc {
  116. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  117. t, err := template.ParseFiles("templates/list.html")
  118. if err != nil {
  119. fmt.Println(err)
  120. return
  121. }
  122. var errs []error
  123. splitted := strings.Split(r.URL.Path, "/")
  124. if len(splitted) < 3 {
  125. w.WriteHeader(http.StatusNotFound)
  126. return
  127. }
  128. listID := splitted[2]
  129. l, ok := c.GetList(listID)
  130. if !ok {
  131. w.WriteHeader(http.StatusNotFound)
  132. return
  133. }
  134. if r.Method == "POST" {
  135. updated, err := movies.UnmarshalList([]byte(r.FormValue("list_json")))
  136. if err != nil {
  137. errs = append(errs, err)
  138. } else if listID != updated.ID {
  139. errs = append(errs, fmt.Errorf("you cannot change the imdb id."))
  140. } else {
  141. l = updated
  142. c.UpdateList(updated)
  143. }
  144. }
  145. b, err := json.MarshalIndent(l, "", " ")
  146. if err != nil {
  147. errs = append(errs, err)
  148. }
  149. t.Execute(w, map[string]interface{}{
  150. "List": l,
  151. "ListJSON": string(b),
  152. "Errors": errs,
  153. })
  154. })
  155. }