diff --git a/cmd/api/healthcheck.go b/cmd/api/healthcheck.go index e404edd..c107b72 100644 --- a/cmd/api/healthcheck.go +++ b/cmd/api/healthcheck.go @@ -1,30 +1,23 @@ package main import ( - "encoding/json" "net/http" ) func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) { - // create a mapping that will be converted to json - data := map[string]string{ - "status": "available", - "environment": app.config.env, - "version": version, + envelopedHealth := envelope{ + "status": "available", + "system_info": map[string]string{ + "environment": app.config.env, + "version": version, + }, } - js, err := json.Marshal(data) + err := app.writeJSON(w, http.StatusOK, envelopedHealth, nil) if err != nil { app.logger.Print(err) http.Error(w, "the server encountered an error and could process your request", http.StatusInternalServerError) - return } - js = append(js, '\n') - - w.Header().Set("Content-Type", "application/json") - - w.Write(js) - } diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go index b3d3b04..8f47c62 100644 --- a/cmd/api/helpers.go +++ b/cmd/api/helpers.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "errors" "net/http" "strconv" @@ -8,6 +9,8 @@ import ( "github.com/julienschmidt/httprouter" ) +type envelope map[string]any + func (app *application) readIDParam(r *http.Request) (int64, error) { params := httprouter.ParamsFromContext(r.Context()) @@ -18,3 +21,22 @@ func (app *application) readIDParam(r *http.Request) (int64, error) { return id, nil } + +func (app *application) writeJSON(w http.ResponseWriter, status int, data any, headers http.Header) error { + js, err := json.Marshal(data) + if err != nil { + return err + } + + js = append(js, '\n') + + for key, value := range headers { + w.Header()[key] = value + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + w.Write(js) + + return nil +} diff --git a/cmd/api/movies.go b/cmd/api/movies.go index e4e5b95..a259a39 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -3,6 +3,9 @@ package main import ( "fmt" "net/http" + "time" + + "greenlight.ergz/internal/data" ) func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { @@ -16,5 +19,20 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) 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, + } + + envelopedMovie := envelope{"movie": movie} + err = app.writeJSON(w, http.StatusOK, envelopedMovie, nil) + if err != nil { + app.logger.Print(err) + http.Error(w, "the server encountered an error and could process your request", http.StatusInternalServerError) + } + } diff --git a/internal/data/movies.go b/internal/data/movies.go new file mode 100644 index 0000000..8fb98c1 --- /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 Runtime `json:"runtime,omitempty,string"` + Genres []string `json:"gentres,omitempty"` + Version int32 `json:"version"` +} diff --git a/internal/data/runtime.go b/internal/data/runtime.go new file mode 100644 index 0000000..1476f85 --- /dev/null +++ b/internal/data/runtime.go @@ -0,0 +1,16 @@ +package data + +import ( + "fmt" + "strconv" +) + +type Runtime int32 + +func (r Runtime) MarshalJSON() ([]byte, error) { + jsonValue := fmt.Sprintf("%d mins", r) + + quotedJSONValue := strconv.Quote(jsonValue) + return []byte(quotedJSONValue), nil + +}