From 281bc33897552c039ce44a6480ada923c2ce15b4 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Tue, 10 Mar 2026 15:08:01 +0300 Subject: [PATCH] ch3.3 --- cmd/api/movies.go | 19 ++++++++++++++++++- internal/data/movies.go | 13 +++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 internal/data/movies.go diff --git a/cmd/api/movies.go b/cmd/api/movies.go index 5b2ddab..e1ffc6f 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -3,6 +3,9 @@ package main import ( "fmt" "net/http" + "time" + + "gitea.local.lab/Lbenedar/greenlight/internal/data" ) func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { @@ -15,5 +18,19 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) http.NotFound(w, r) return } - fmt.Fprintf(w, "show the details of movie %d\n", id) + + movie := data.Movie{ + ID: id, + CreatedAt: time.Now(), + Title: "Casablanca", + Runtime: 102, + Genres: []string{"drama", "romance", "war"}, + Version: 1, + } + + err = app.writeJSON(w, http.StatusOK, movie, nil) + if err != nil { + app.logger.Print(err) + http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) + } } diff --git a/internal/data/movies.go b/internal/data/movies.go new file mode 100644 index 0000000..f1b042c --- /dev/null +++ b/internal/data/movies.go @@ -0,0 +1,13 @@ +package data + +import "time" + +type Movie struct { + ID int64 `json:"id"` + CreatedAt time.Time `json:"-"` + Title string `json:"title"` + Year int32 `json:"year,omitempty"` + Runtime int32 `json:"runtime,omitempty,string"` + Genres []string `json:"genres,omitempty"` + Version int32 `json:"version"` +}