ch8.1
This commit is contained in:
@@ -94,10 +94,10 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|||||||
}
|
}
|
||||||
|
|
||||||
var input struct {
|
var input struct {
|
||||||
Title string `json:"title"`
|
Title *string `json:"title"`
|
||||||
Year int32 `json:"year"`
|
Year *int32 `json:"year"`
|
||||||
Runtime data.Runtime `json:"runtime"`
|
Runtime *data.Runtime `json:"runtime"`
|
||||||
Genres []string `json:"genres"`
|
Genres []string `json:"genres"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err = app.readJSON(w, r, &input)
|
err = app.readJSON(w, r, &input)
|
||||||
@@ -106,10 +106,18 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
movie.Title = input.Title
|
if input.Title != nil {
|
||||||
movie.Year = input.Year
|
movie.Title = *input.Title
|
||||||
movie.Runtime = input.Runtime
|
}
|
||||||
movie.Genres = input.Genres
|
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()
|
v := validator.New()
|
||||||
if data.ValidateMovie(v, movie); !v.Valid() {
|
if data.ValidateMovie(v, movie); !v.Valid() {
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ func (app *application) routes() *httprouter.Router {
|
|||||||
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
|
||||||
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
|
||||||
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
|
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)
|
router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.deleteMovieHandler)
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user