Skip to content

Commit de4a353

Browse files
committed
admin: allow editing tmdbid
1 parent 3d1d57c commit de4a353

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

cmd/serve/admin/handlers/films.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func EditFilmHandler() func(http.ResponseWriter, *http.Request) {
279279
ID: film.ID,
280280
Title: film.Title,
281281
Year: year,
282+
TMDBID: film.TMDBID,
282283
Overview: film.Overview,
283284
Runtime: runtime,
284285
Published: film.Published,
@@ -309,6 +310,7 @@ func UpdateFilmHandler() func(http.ResponseWriter, *http.Request) {
309310

310311
title := r.FormValue("title")
311312
year := r.FormValue("year")
313+
tmdbIDStr := r.FormValue("tmdb_id")
312314
overview := r.FormValue("overview")
313315
runtimeStr := r.FormValue("runtime")
314316
published := r.FormValue("published") == "true"
@@ -322,13 +324,23 @@ func UpdateFilmHandler() func(http.ResponseWriter, *http.Request) {
322324
}
323325
}
324326

327+
var tmdbID *int
328+
if tmdbIDStr != "" {
329+
value, err := strconv.Atoi(tmdbIDStr)
330+
if err != nil {
331+
http.Error(w, "Invalid TMDB ID", http.StatusBadRequest)
332+
return
333+
}
334+
tmdbID = &value
335+
}
336+
325337
yearInt := 0
326338
if year != "" {
327339
yearInt, _ = strconv.Atoi(year)
328340
}
329341
path := generateFilmPath(title, yearInt)
330342

331-
if err := db.UpdateFilm(id, 0, title, year, path, overview, runtime, published); err != nil {
343+
if err := db.UpdateFilm(id, tmdbID, title, year, path, overview, runtime, published); err != nil {
332344
http.Error(w, "Failed to update film", http.StatusInternalServerError)
333345
return
334346
}

cmd/serve/admin/templates/edit-film.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type EditFilmData struct {
2020
ID int
2121
Title string
2222
Year string
23+
TMDBID *int
2324
Overview string
2425
Runtime string
2526
Published bool

cmd/serve/admin/templates/edit-film.html.gotpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<form action="/films/edit/{{.ID}}" method="post">
66
<label>Title: <input type="text" name="title" value="{{.Title}}" /></label>
77
<label>Year: <input type="text" name="year" value="{{.Year}}" /></label>
8+
<label>TMDB ID: <input type="text" name="tmdb_id" value="{{if .TMDBID}}{{.TMDBID}}{{end}}" /></label>
89
<label>Path: <input type="text" name="path" value="{{.Path}}" /></label>
910
<label>Runtime (minutes): <input type="text" name="runtime" value="{{.Runtime}}" /></label>
1011
<label>

cmd/serve/db/films.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ func CreateFilm(tmdbID int, title, year, path string, overview string, runtime i
105105
return id, nil
106106
}
107107

108-
func UpdateFilm(id int, tmdbID int, title, year, path string, overview string, runtime int, published bool) error {
109-
var tmdbIDPtr *int
110-
if tmdbID > 0 {
111-
tmdbIDPtr = &tmdbID
112-
}
113-
108+
func UpdateFilm(id int, tmdbID *int, title, year, path string, overview string, runtime int, published bool) error {
114109
var yearPtr *int
115110
if year != "" {
116111
y, err := strconv.Atoi(year)
@@ -128,7 +123,7 @@ func UpdateFilm(id int, tmdbID int, title, year, path string, overview string, r
128123
UPDATE films
129124
SET tmdb_id = $1, title = $2, year = $3, overview = $4, runtime = $5, published = $6, path = $7
130125
WHERE id = $8
131-
`, tmdbIDPtr, title, yearPtr, overview, runtimePtr, published, path, id)
126+
`, tmdbID, title, yearPtr, overview, runtimePtr, published, path, id)
132127
if err != nil {
133128
return fmt.Errorf("failed to update film: %w", err)
134129
}

0 commit comments

Comments
 (0)