|
|
|
|
@ -122,11 +122,20 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// var input struct {
|
|
|
|
|
// Title string `json:"title"`
|
|
|
|
|
// Year int32 `json:"year"`
|
|
|
|
|
// Runtime data.Runtime `json:"runtime"`
|
|
|
|
|
// Genres []string `json:"genres"`
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Choosing to use pointers to allow us to determine if the zero value was sent
|
|
|
|
|
// by the client or if it was excluded from the client submission
|
|
|
|
|
var input struct {
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
Year int32 `json:"year"`
|
|
|
|
|
Runtime data.Runtime `json:"runtime"`
|
|
|
|
|
Genres []string `json:"genres"`
|
|
|
|
|
Title *string `json:"title"`
|
|
|
|
|
Year *int32 `json:"year"`
|
|
|
|
|
Runtime *data.Runtime `json:"runtime"`
|
|
|
|
|
Genres []string `json:"genres"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = app.readJSON(w, r, &input)
|
|
|
|
|
@ -135,10 +144,19 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
movie.Title = input.Title
|
|
|
|
|
movie.Year = input.Year
|
|
|
|
|
movie.Runtime = input.Runtime
|
|
|
|
|
movie.Genres = input.Genres
|
|
|
|
|
if input.Title != nil {
|
|
|
|
|
movie.Title = *input.Title
|
|
|
|
|
}
|
|
|
|
|
if input.Year != nil {
|
|
|
|
|
movie.Year = *input.Year
|
|
|
|
|
}
|
|
|
|
|
if input.Runtime != nil {
|
|
|
|
|
movie.Runtime = *input.Runtime
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if input.Genres != nil {
|
|
|
|
|
movie.Genres = input.Genres
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v := validator.New()
|
|
|
|
|
|
|
|
|
|
@ -149,7 +167,12 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
|
|
|
|
|
err = app.models.Movies.Update(r.Context(), movie)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, data.ErrEditConflict):
|
|
|
|
|
app.editConflictResponse(w, r)
|
|
|
|
|
default:
|
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -167,7 +190,20 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Maybe should be passing a timeout context
|
|
|
|
|
_, err = app.models.Movies.Get(r.Context(), id)
|
|
|
|
|
// WE DON"T NEED THIS BECAUSE EXEC RETURNS A RESULT OBJECT WITH THE NUMBER
|
|
|
|
|
// OF ROWS AFFECTED. IF THE RESULT IS 0 THEN WE 404
|
|
|
|
|
// _, err = app.models.Movies.Get(r.Context(), id)
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// switch {
|
|
|
|
|
// case errors.Is(err, data.ErrRecordNotFound):
|
|
|
|
|
// app.notFoundResponse(w, r)
|
|
|
|
|
// default:
|
|
|
|
|
// app.serverErrorResponse(w, r, err)
|
|
|
|
|
// }
|
|
|
|
|
// return
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
err = app.models.Movies.Delete(r.Context(), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, data.ErrRecordNotFound):
|
|
|
|
|
@ -178,13 +214,15 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = app.models.Movies.Delete(r.Context(), id)
|
|
|
|
|
// I personally think this should be a 202 or a 204 but Alex thinks otherwise.
|
|
|
|
|
// his rule of thumb is if the clients are humans send human readable messages. If they
|
|
|
|
|
// are machines then status code is alright.
|
|
|
|
|
//w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
|
|
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"message": "movie successfully deleted"}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|