|
|
|
@ -36,7 +36,7 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// use intermediate struct for decoding, use Movie struct for validation
|
|
|
|
// use intermediate struct for decoding, use Movie struct for validation
|
|
|
|
m := &data.Movie{
|
|
|
|
movie := &data.Movie{
|
|
|
|
Title: input.Title,
|
|
|
|
Title: input.Title,
|
|
|
|
Year: input.Year,
|
|
|
|
Year: input.Year,
|
|
|
|
Runtime: input.Runtime,
|
|
|
|
Runtime: input.Runtime,
|
|
|
|
@ -45,24 +45,24 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
|
|
|
|
|
|
|
v := validator.New()
|
|
|
|
v := validator.New()
|
|
|
|
|
|
|
|
|
|
|
|
if data.ValidateMovie(v, m); !v.Valid() {
|
|
|
|
if data.ValidateMovie(v, movie); !v.Valid() {
|
|
|
|
app.failedValidationResponse(w, r, v.Errors)
|
|
|
|
app.failedValidationResponse(w, r, v.Errors)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We can implement a timeout context if we would like here
|
|
|
|
// We can implement a timeout context if we would like here
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
|
|
|
|
err = app.models.Movies.Insert(ctx, m)
|
|
|
|
err = app.models.Movies.Insert(ctx, movie)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Include location headers in HTTP response for newly created resource
|
|
|
|
// Include location headers in HTTP response for newly created resource
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Location", fmt.Sprintf("/v1/movies/%d", m.ID))
|
|
|
|
headers.Set("Location", fmt.Sprintf("/v1/movies/%d", movie.ID))
|
|
|
|
|
|
|
|
|
|
|
|
// Write a JSON 201 Response with movie data in the response body and Location header
|
|
|
|
// Write a JSON 201 Response with movie data in the response body and Location header
|
|
|
|
err = app.writeJSON(w, http.StatusCreated, envelope{"movie": m}, headers)
|
|
|
|
err = app.writeJSON(w, http.StatusCreated, data.MovieResponse{Movie: movie}, headers)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -112,7 +112,7 @@ func (app *application) getMovieHandler(w http.ResponseWriter, r *http.Request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
|
|
|
|
err = app.writeJSON(w, http.StatusOK, data.MovieResponse{Movie: movie}, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
//http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
|
|
|
//http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
@ -192,7 +192,7 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
|
|
|
|
err = app.writeJSON(w, http.StatusOK, data.MovieResponse{Movie: movie}, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -235,7 +235,7 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
// are machines then status code is alright.
|
|
|
|
// are machines then status code is alright.
|
|
|
|
//w.WriteHeader(http.StatusAccepted)
|
|
|
|
//w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
|
|
|
|
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"message": "movie successfully deleted"}, nil)
|
|
|
|
err = app.writeJSON(w, http.StatusOK, data.GenericMessageResponse{Message: "movie successfully deleted"}, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -274,7 +274,9 @@ func (app *application) listMoviesHandler(w http.ResponseWriter, r *http.Request
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
app.writeJSON(w, 200, envelope{"movies": movies, "metadata": metadata}, nil)
|
|
|
|
|
|
|
|
|
|
|
|
// app.writeJSON(w, 200, envelope{"movies": movies, "metadata": metadata}, nil)
|
|
|
|
|
|
|
|
app.writeJSON(w, 200, data.ListMoviesResponse{Metadata: metadata, Movies: movies}, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
@ -282,13 +284,13 @@ func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
// js := `{"status": "available", "environment": %q, "version": %q}`
|
|
|
|
// js := `{"status": "available", "environment": %q, "version": %q}`
|
|
|
|
// js = fmt.Sprintf(js, app.config.env, Version)
|
|
|
|
// js = fmt.Sprintf(js, app.config.env, Version)
|
|
|
|
// w.Write([]byte(js))
|
|
|
|
// w.Write([]byte(js))
|
|
|
|
env := envelope{
|
|
|
|
// env := envelope{
|
|
|
|
"status": "available",
|
|
|
|
// "status": "available",
|
|
|
|
"system_info": map[string]string{
|
|
|
|
// "system_info": map[string]string{
|
|
|
|
"environment": app.config.Env,
|
|
|
|
// "environment": app.config.Env,
|
|
|
|
"version": Version,
|
|
|
|
// "version": Version,
|
|
|
|
},
|
|
|
|
// },
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// js, err := json.Marshal(data)
|
|
|
|
// js, err := json.Marshal(data)
|
|
|
|
// if err != nil {
|
|
|
|
// if err != nil {
|
|
|
|
@ -301,7 +303,7 @@ func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
// js = append(js, '\n')
|
|
|
|
// js = append(js, '\n')
|
|
|
|
// w.Header().Set("Content-Type", "application/json")
|
|
|
|
// w.Header().Set("Content-Type", "application/json")
|
|
|
|
// w.Write(js)
|
|
|
|
// w.Write(js)
|
|
|
|
err := app.writeJSON(w, 200, env, nil)
|
|
|
|
err := app.writeJSON(w, 200, data.HealthCheckResponse{Status: "available", SystemInfo: data.SystemInfo{Environment: app.config.Env, Version: Version}}, nil)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
//app.logger.Error(err.Error())
|
|
|
|
//app.logger.Error(err.Error())
|
|
|
|
//http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
|
|
|
//http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
|
|
|
|