51 lines
1.0 KiB
Go
51 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"greenlight.ergz/internal/data"
|
|
)
|
|
|
|
func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
Title string `json:"title"`
|
|
Year int32 `json:"year"`
|
|
Runtime data.Runtime `json:"runtime"`
|
|
Genres []string `json:"genres"`
|
|
}
|
|
|
|
err := app.readJSON(w, r, &input)
|
|
if err != nil {
|
|
app.errorResponse(w, r, http.StatusBadRequest, err.Error())
|
|
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 {
|
|
app.notFoundResponse(w, r)
|
|
return
|
|
}
|
|
|
|
movie := data.Movie{
|
|
ID: id,
|
|
CreatedAt: time.Now(),
|
|
Title: "Casablanca",
|
|
Runtime: 102,
|
|
Genres: []string{"drama", "romance", "war"},
|
|
Version: 1,
|
|
}
|
|
|
|
envelopedMovie := envelope{"movie": movie}
|
|
err = app.writeJSON(w, http.StatusOK, envelopedMovie, nil)
|
|
if err != nil {
|
|
app.serverErrorResponse(w, r, err)
|
|
}
|
|
|
|
}
|