feat: validation, postgress connection,

This commit is contained in:
2026-03-12 11:23:18 +01:00
parent abf2db2798
commit 56fa7b6c21
15 changed files with 269 additions and 37 deletions
+23 -8
View File
@@ -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)
}