feat: go greenlight api wip

This commit is contained in:
2026-03-11 15:50:38 +01:00
parent 2ce943f94e
commit abf2db2798
19 changed files with 469 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
package main
import (
"fmt"
"net/http"
"time"
"greenlight.debuggingjon.dev/internal/data"
)
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"`
}
// 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
}
fmt.Fprintf(w, "%+v\n", input)
}
func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r)
if err != nil {
// Use the new notFoundResponse() helper.
app.notFoundResponse(w, r)
return
}
movie := data.Movie{
ID: id,
CreatedAt: time.Now(),
Title: "Casablanca",
Runtime: 102,
Genres: []string{"drama", "romance", "war"}, Version: 1,
}
// Create an envelope{"movie": movie} instance and pass it to writeJSON(), instead
// of passing the plain movie struct.
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil {
// Use the new serverErrorResponse() helper.
app.serverErrorResponse(w, r, err)
}
}