From 5b6f33c3f068ee8622502646f4a8dc227ae7b92f Mon Sep 17 00:00:00 2001 From: Maxime Delporte Date: Fri, 10 Oct 2025 20:50:32 +0200 Subject: [PATCH] Creating helpers.go file : allowing us to extract readIDParam method for further use. Updating showMovieHandler with the new method. --- cmd/api/helpers.go | 43 +++++++++++++++++++++++++++++++++++++++++++ cmd/api/movies.go | 18 +----------------- 2 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 cmd/api/helpers.go diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go new file mode 100644 index 0000000..e92feeb --- /dev/null +++ b/cmd/api/helpers.go @@ -0,0 +1,43 @@ +package main + +import ( + "errors" + "github.com/julienschmidt/httprouter" + "net/http" + "strconv" +) + +/* +Note: The readIDParam() method doesn't use any dependencies from our +application struct so it could just be a regular function, rather than a method on +application. But in general, I suggest setting up all your application-specific handlers +and helpers so that they are methods on application. It helps maintain consistency in +your code structure, and also future-proofs your code for when those handlers and +helpers change later and they do need access to dependency. + +Retrieve the "id" URL parameter from the current request context, then convert it to +an integer and return it. If the operation isn't successful, return 0 and an error. +*/ +func (app *application) readIDParam(r *http.Request) (int64, error) { + /* + When httprouter is parsing a request, any interpolated URL parameters will be + stored in the request context. We can use the ParamsFromContext() function to + retrieve a slice containing these parameter names and values. + */ + params := httprouter.ParamsFromContext(r.Context()) + + /* + We can then use the ByName() method to get the value of the "id" parameter from + the slice. In our project all movies will have a unique positive integer ID, but + the value returned by ByName() is always a string. So we try to convert it to a + base 10 integer (with a bit size of 64). If the parameter couldn't be converted, + or is less than 1, we know the ID is invalid so we use the http.NotFound() + func to return a 404 Not Found Response. + */ + id, err := strconv.ParseInt(params.ByName("id"), 10, 64) + if err != nil || id < 1 { + return 0, errors.New("invalid id parameter") + } + + return id, nil +} diff --git a/cmd/api/movies.go b/cmd/api/movies.go index 95e9a9f..47d603a 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -2,9 +2,7 @@ package main import ( "fmt" - "github.com/julienschmidt/httprouter" "net/http" - "strconv" ) // "POST /v1/movies" endpoint. @@ -14,22 +12,8 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques // "GET /v1/movies/:id" func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) { - /* - When httprouter is parsing a request, any interpolated URL parameters will be - stored in the request context. We can use the ParamsFromContext() function to - retrieve a slice containing these parameter names and values. - */ - params := httprouter.ParamsFromContext(r.Context()) + id, err := app.readIDParam(r) - /* - We can then use the ByName() method to get the value of the "id" parameter from - the slice. In our project all movies will have a unique positive integer ID, but - the value returned by ByName() is always a string. So we try to convert it to a - base 10 integer (with a bit size of 64). If the parameter couldn't be converted, - or is less than 1, we know the ID is invalid so we use the http.NotFound() - func to return a 404 Not Found Response. - */ - id, err := strconv.ParseInt(params.ByName("id"), 10, 64) if err != nil || id < 1 { http.NotFound(w, r) return