diff --git a/cmd/api/command.go b/cmd/api/command.go index f3a075f..744e2ca 100644 --- a/cmd/api/command.go +++ b/cmd/api/command.go @@ -11,7 +11,7 @@ import ( func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Request) { name, err := app.readCommandNameParam(r) if err != nil { - // app.Logger.Errorf("showCommandHandler, Command not found", err) + app.logError(r, err) return } @@ -21,18 +21,20 @@ func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Reques if err != nil { switch { case errors.Is(err, data.ErrRecordNotFound): - app.Logger.Errorf("showCommandHandler, Command not found", err) + app.logError(r, err) return default: app.serverErrorResponse(w, r, err) } return } - app.Logger.Info("Command Name:", command.Name) - err = app.writeJSON(w, http.StatusOK, envelope{"movie": command}, nil) - // if err != nil { - // app.serverErrorResponse(w, r, err) - // } + app.Logger.Infow("GET Command", + "Command", command, + ) + err = app.writeJSON(w, http.StatusOK, envelope{"command": command}, nil) + if err != nil { + app.serverErrorResponse(w, r, err) + } } type envelope map[string]interface{} diff --git a/cmd/api/errors.go b/cmd/api/errors.go index 5176302..6c6fecd 100644 --- a/cmd/api/errors.go +++ b/cmd/api/errors.go @@ -16,8 +16,10 @@ func (app *application) serverErrorResponse(w http.ResponseWriter, r *http.Reque // book we'll upgrade this to use structured logging, and record additional information // about the request including the HTTP method and URL. func (app *application) logError(r *http.Request, err error) { - app.Logger.Infow("logError", - "error", err) + app.Logger.Errorw("Error", + "Request URI", r.RequestURI, + "error", err, + ) } func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, status int, message interface{}) { @@ -25,5 +27,5 @@ func (app *application) errorResponse(w http.ResponseWriter, r *http.Request, st // Write the response using the writeJSON() helper. If this happens to return an // error then log it, and fall back to sending the client an empty response with a // 500 Internal Server Error status code. - fmt.Fprintf(w, "Error: %s", status) + fmt.Fprintf(w, "Error: %d", status) } diff --git a/cmd/api/helpers.go b/cmd/api/helpers.go index da3abfe..87a8a6f 100644 --- a/cmd/api/helpers.go +++ b/cmd/api/helpers.go @@ -1,30 +1,27 @@ package main import ( - "errors" "net/http" - "strconv" "github.com/julienschmidt/httprouter" ) -func (app *application) readIDParam(r *http.Request) (int64, error) { - params := httprouter.ParamsFromContext(r.Context()) - - // Use `ByName()` function to get the value of the "id" parameter from the slice. - // The value returned by `ByName()` is always a string so we try to convert it to - // base64 with a bit size of 64. - id, err := strconv.ParseInt(params.ByName("id"), 10, 64) - if err != nil || id < 1 { - return 0, errors.New("invalid id parameter") - } - - return id, nil -} +// func (app *application) readIDParam(r *http.Request) (int64, error) { +// params := httprouter.ParamsFromContext(r.Context()) +// +// // Use `ByName()` function to get the value of the "id" parameter from the slice. +// // The value returned by `ByName()` is always a string so we try to convert it to +// // base64 with a bit size of 64. +// id, err := strconv.ParseInt(params.ByName("id"), 10, 64) +// if err != nil || id < 1 { +// return 0, errors.New("invalid id parameter") +// } +// +// return id, nil +// } func (app *application) readCommandNameParam(r *http.Request) (string, error) { params := httprouter.ParamsFromContext(r.Context()) - app.Logger.Info(r.Context()) // Use `ByName()` function to get the value of the "id" parameter from the slice. // The value returned by `ByName()` is always a string so we try to convert it to diff --git a/migrations/000003_create_commands_table.up.sql b/migrations/000003_create_commands_table.up.sql index 5cc2b03..8682f29 100644 --- a/migrations/000003_create_commands_table.up.sql +++ b/migrations/000003_create_commands_table.up.sql @@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS commands ( INSERT INTO commands (name,"text","category","level") VALUES ('repeat','xset r rate 175 50','default',0), + ('xset','xset r rate 175 50','default',0), ('eurkey','setxkbmap -layout eu','default',0), ('clueless','ch02 ch21 ch31','default',0), ('justinfan','64537','default',0),