From 43c81cf8e1e13343293a05a437dadd109fbe46c5 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Wed, 18 Mar 2026 13:46:57 +0300 Subject: [PATCH] ch8.1 --- cmd/api/movies.go | 24 ++++++++++++++++-------- cmd/api/routes.go | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/api/movies.go b/cmd/api/movies.go index ec3e2c9..81a171b 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -94,10 +94,10 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques } var input struct { - Title string `json:"title"` - Year int32 `json:"year"` - Runtime data.Runtime `json:"runtime"` - Genres []string `json:"genres"` + Title *string `json:"title"` + Year *int32 `json:"year"` + Runtime *data.Runtime `json:"runtime"` + Genres []string `json:"genres"` } err = app.readJSON(w, r, &input) @@ -106,10 +106,18 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques return } - movie.Title = input.Title - movie.Year = input.Year - movie.Runtime = input.Runtime - movie.Genres = input.Genres + if input.Title != nil { + movie.Title = *input.Title + } + if input.Year != nil { + movie.Year = *input.Year + } + if input.Runtime != nil { + movie.Runtime = *input.Runtime + } + if input.Genres != nil { + movie.Genres = input.Genres + } v := validator.New() if data.ValidateMovie(v, movie); !v.Valid() { diff --git a/cmd/api/routes.go b/cmd/api/routes.go index eaf9b58..5b1b003 100644 --- a/cmd/api/routes.go +++ b/cmd/api/routes.go @@ -15,7 +15,7 @@ func (app *application) routes() *httprouter.Router { router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler) router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler) router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler) - router.HandlerFunc(http.MethodPut, "/v1/movies/:id", app.updateMovieHandler) + router.HandlerFunc(http.MethodPatch, "/v1/movies/:id", app.updateMovieHandler) router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.deleteMovieHandler) return router }