package main import ( "fmt" "net/http" "strconv" "time" "git.runcible.io/learning/pulley/internal/data" "github.com/julienschmidt/httprouter" ) func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { // Declare an anonymous struct to hold the information that we expect to be in the // HTTP request body (note that the field names and types in the struct are a subset // of the Movie struct that we created earlier). This struct will be our *target // decode destination*. // just like encoding the field names need to be exported to have Decode set them var input struct { Title string `json:"title"` Year int32 `json:"year"` // implemented an Unmarshaler receiver for this // Runtime int32 `json:"runtime"` Runtime data.Runtime `json:"runtime"` Genres []string `json:"genres"` } // reads from the request body and decodes to our input struct // must be a non-nil pointer. Otherwise will raise json.InvalidUnmarshalError err := app.readJSON(w, r, &input) if err != nil { app.badRequestResponse(w, r, err) return } // TODO save to DB // Dump the contents of the input struct in a HTTP response // +v is the default format value plus field names for structs w.WriteHeader(http.StatusCreated) fmt.Fprintf(w, "%+v\n", input) } func (app *application) getMovieHandler(w http.ResponseWriter, r *http.Request) { // When httprouter is parsing a request, any interpolated URL parameters will be // stored in the request context. We can use the ParamsFromContext() function to // retrieve a slice containing these parameter names and values. // TODO refactor id retrieval to an app.readIDParam receiver params := httprouter.ParamsFromContext(r.Context()) id, err := strconv.ParseInt(params.ByName("id"), 10, 64) if err != nil || id < 1 { // http.NotFound(w, r) app.notFoundResponse(w, r) return } movie := data.Movie{ ID: id, CreatedAt: time.Now(), Title: "Eisenhorn", // Runtime: 102, Genres: []string{"sci-fi", "action"}, Version: 1, } app.logger.Info("Hit the get movies and found", "movie", movie.Title) err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) if err != nil { //app.logger.Error(err.Error()) //http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) app.serverErrorResponse(w, r, err) } } func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) { // JSON has to be double quoted. FYI you would never really do this // js := `{"status": "available", "environment": %q, "version": %q}` // js = fmt.Sprintf(js, app.config.env, Version) // w.Write([]byte(js)) env := envelope{ "status": "available", "system_info": map[string]string{ "environment": app.config.env, "version": Version, }, } // js, err := json.Marshal(data) // if err != nil { // app.logger.Error(err.Error()) // http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) // return // } // // Append a newline to the JSON. This is just a small nicety to make it easier to // // view in terminal applications. // js = append(js, '\n') // w.Header().Set("Content-Type", "application/json") // w.Write(js) err := app.writeJSON(w, 200, env, nil) if err != nil { //app.logger.Error(err.Error()) //http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError) app.serverErrorResponse(w, r, err) return } }