ch9.2-9.3
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"gitea.local.lab/Lbenedar/greenlight/internal/data"
|
||||
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||
@@ -93,6 +94,13 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
if r.Header.Get("X-Expected-Version") != "" {
|
||||
if strconv.FormatInt(int64(movie.Version), 32) != r.Header.Get("X-Expected-Version") {
|
||||
app.editConflictResponse(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var input struct {
|
||||
Title *string `json:"title"`
|
||||
Year *int32 `json:"year"`
|
||||
@@ -165,3 +173,39 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (app *application) listMoviesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var input struct {
|
||||
Title string
|
||||
Genres []string
|
||||
data.Filters
|
||||
}
|
||||
|
||||
v := validator.New()
|
||||
|
||||
qs := r.URL.Query()
|
||||
|
||||
input.Title = app.readString(qs, "title", "")
|
||||
input.Genres = app.readCSV(qs, "genres", []string{})
|
||||
input.Filters.Page = app.readInt(qs, "page", 1, v)
|
||||
input.Filters.PageSize = app.readInt(qs, "page_size", 20, v)
|
||||
input.Filters.Sort = app.readString(qs, "sort", "id")
|
||||
|
||||
input.Filters.SortSafelist = []string{"id", "title", "year", "runtime", "-id", "-title", "-year", "-runtime"}
|
||||
|
||||
if data.ValidateFilters(v, input.Filters); !v.Valid() {
|
||||
app.failedValidationResponse(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
movies, err := app.models.Movies.GetAll(input.Title, input.Genres, input.Filters)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = app.writeJSON(w, http.StatusOK, envelope{"movies": movies}, nil)
|
||||
if err != nil {
|
||||
app.serverErrorResponse(w, r, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user