Files
greenlight/cmd/api/movies.go

45 lines
1.1 KiB
Go

package main
import (
"fmt"
"greenlight.craftr.fr/internal/data"
"net/http"
"time"
)
// "POST /v1/movies" endpoint.
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "create a new movie")
}
// "GET /v1/movies/:id"
func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r)
if err != nil || id < 1 {
http.NotFound(w, r)
return
}
/*
Create a new instance of the Movie struct containing the ID we
extracted from the URL and some dummy data. Also notice that we
deliberately haven't set a value for the Year field.
*/
movie := data.Movie{
ID: id,
CreatedAt: time.Now(),
Title: "Casablanca",
Runtime: 102,
Genres: []string{"drama", "romance", "war"},
Version: 1,
}
// Encode the struct to JSON and send it as the HTTP response.
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil {
app.logger.Error(err.Error())
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
}
}