feat: validation, postgress connection,
This commit is contained in:
@@ -6,26 +6,41 @@ import (
|
||||
"time"
|
||||
|
||||
"greenlight.debuggingjon.dev/internal/data"
|
||||
"greenlight.debuggingjon.dev/internal/validator"
|
||||
)
|
||||
|
||||
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
Title string `json:"title"`
|
||||
Year int32 `json:"year"`
|
||||
Runtime int32 `json:"runtime"`
|
||||
Genres []string `json:"genres"`
|
||||
Title string `json:"title"`
|
||||
Year int32 `json:"year"`
|
||||
Runtime data.Runtime `json:"runtime"`
|
||||
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.
|
||||
err := app.readJSON(w, r, &input)
|
||||
if err != nil {
|
||||
// Use the new badRequestResponse() helper.
|
||||
app.badRequestResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Copy the values from the input struct to a new Movie struct.
|
||||
movie := &data.Movie{
|
||||
Title: input.Title,
|
||||
Year: input.Year,
|
||||
Runtime: input.Runtime,
|
||||
Genres: input.Genres,
|
||||
}
|
||||
|
||||
// Initialize a new Validator.
|
||||
v := validator.New()
|
||||
|
||||
// Call the ValidateMovie() function and return a response containing the errors if
|
||||
// any of the checks fail.
|
||||
if data.ValidateMovie(v, movie); !v.Valid() {
|
||||
app.failedValidationResponse(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%+v\n", input)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user