Updating createMovieHandler accepting data.Runtime for the Runtime field. Updating internal/data/runtime.go with the json.Unmarshaler interface to transform our client's runtime string field '%d mins' into a Runtime type format.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 58s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 58s
This commit is contained in:
@@ -11,10 +11,10 @@ import (
|
|||||||
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Declare an anonymous struct to hold the information that we expect to be in the HTTP request body (note that the field names and types in the struct are a subset of the Movie struct that we created earlier). This struct will be our *target decode destination*.
|
// Declare an anonymous struct to hold the information that we expect to be in the HTTP request body (note that the field names and types in the struct are a subset of the Movie struct that we created earlier). This struct will be our *target decode destination*.
|
||||||
var input struct {
|
var input struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Year int32 `json:"year"`
|
Year int32 `json:"year"`
|
||||||
Runtime int32 `json:"runtime"`
|
Runtime data.Runtime `json:"runtime"`
|
||||||
Genres []string `json:"genres"`
|
Genres []string `json:"genres"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use the new readJSON() helper to decode the request body into the input struct. If this returns an error, we send the client the error message along with a 400 Bad Request status code, just like before.
|
// Use the new readJSON() helper to decode the request body into the input struct. If this returns an error, we send the client the error message along with a 400 Bad Request status code, just like before.
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrInvalidRuntimeFormat : Define an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully
|
||||||
|
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
|
||||||
|
|
||||||
// Runtime
|
// Runtime
|
||||||
/*
|
/*
|
||||||
Declare a custom Runtime type, which has the underlying type int32
|
Declare a custom Runtime type, which has the underlying type int32
|
||||||
@@ -28,3 +33,32 @@ func (r Runtime) MarshalJSON() ([]byte, error) {
|
|||||||
// Convert the quoted string value to a byte slice and return it.
|
// Convert the quoted string value to a byte slice and return it.
|
||||||
return []byte(quotedJSONValue), nil
|
return []byte(quotedJSONValue), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON
|
||||||
|
// Implement a UnmarshalJSON() method on the Runtime type so that it satisfies the json.Unmarshaler interface. IMPORTANT: Because UnmarshalJSON() needs to modify the receiver (our Runtime type), we must use a pointer receiver for this to work correctly. Otherwise, we will only be modifying a copy (which is then discarded when this method returns).
|
||||||
|
func (r *Runtime) UnmarshalJSON(jsonValue []byte) error {
|
||||||
|
// We expect that the incoming JSON value will be a string in the format "<runtimes> mins", and the first thing we need to do is remove the surrounding double-quotes from this string. If we can't unquote it, then we return the ErrInvalidRuntimeFormat error.
|
||||||
|
unquotedJSONValue, err := strconv.Unquote(string(jsonValue))
|
||||||
|
if err != nil {
|
||||||
|
return ErrInvalidRuntimeFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split the string to isolate the part containing the number.
|
||||||
|
parts := strings.Split(unquotedJSONValue, " ")
|
||||||
|
|
||||||
|
// Sanity check the parts of the string to make sure it was in the expected format. If it isn't, we return the ErrInvalidRuntimeFormat error again.
|
||||||
|
if len(parts) != 2 || parts[1] != "mins" {
|
||||||
|
return ErrInvalidRuntimeFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, parse the string containing the number into an int32. Again, if this fails, return the ErrInvalidRuntimeFormat error.
|
||||||
|
i, err := strconv.ParseInt(parts[0], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return ErrInvalidRuntimeFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the int32 to a Runtime type and assign this to the receiver. Note that we use the * operator to deference the receiver (which is a pointer to a Runtime type) in order to set the underlying value of the pointer.
|
||||||
|
*r = Runtime(i)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user