ch9.2-9.3

This commit is contained in:
lbenedar
2026-03-18 16:31:33 +03:00
parent 985340c557
commit 17267ed145
6 changed files with 149 additions and 0 deletions

View File

@@ -6,9 +6,11 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
"github.com/julienschmidt/httprouter"
)
@@ -83,3 +85,38 @@ func (app *application) readJSON(w http.ResponseWriter, r *http.Request, dst any
}
return nil
}
func (app *application) readString(qs url.Values, key string, defaultValue string) string {
s := qs.Get(key)
if s == "" {
return defaultValue
}
return s
}
func (app *application) readCSV(qs url.Values, key string, defaultValue []string) []string {
csv := qs.Get(key)
if csv == "" {
return defaultValue
}
return strings.Split(csv, ",")
}
func (app *application) readInt(qs url.Values, key string, defaultValue int, v *validator.Validator) int {
s := qs.Get(key)
if s == "" {
return defaultValue
}
i, err := strconv.Atoi(s)
if err != nil {
v.AddError(key, "must be an integer value")
return defaultValue
}
return i
}

View File

@@ -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)
}
}

View File

@@ -13,6 +13,8 @@ func (app *application) routes() *httprouter.Router {
router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse)
router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler)
router.HandlerFunc(http.MethodGet, "/v1/movies", app.listMoviesHandler)
router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler)
router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler)
router.HandlerFunc(http.MethodPatch, "/v1/movies/:id", app.updateMovieHandler)