2022-08-16 16:35:45 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-08-17 15:32:42 +02:00
|
|
|
"fmt"
|
2022-08-16 16:35:45 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name, err := app.readCommandNameParam(r)
|
|
|
|
if err != nil {
|
2022-08-16 18:05:55 +02:00
|
|
|
app.logError(r, err)
|
2022-08-16 16:35:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data for a specific movie from our helper method,
|
|
|
|
// then check if an error was returned, and which.
|
|
|
|
command, err := app.Models.Commands.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, data.ErrRecordNotFound):
|
2022-08-16 18:05:55 +02:00
|
|
|
app.logError(r, err)
|
2022-08-16 16:35:45 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-08-17 21:17:29 +02:00
|
|
|
|
2022-08-16 18:05:55 +02:00
|
|
|
app.Logger.Infow("GET Command",
|
|
|
|
"Command", command,
|
|
|
|
)
|
2022-08-17 21:17:29 +02:00
|
|
|
|
2022-08-16 18:05:55 +02:00
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"command": command}, nil)
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
func (app *application) createCommandHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var input struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
Category string `json:"category"`
|
|
|
|
Level int `json:"level"`
|
|
|
|
}
|
2022-08-16 16:35:45 +02:00
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
err := app.readJSON(w, r, &input)
|
2022-08-16 16:35:45 +02:00
|
|
|
if err != nil {
|
2022-08-17 15:32:42 +02:00
|
|
|
app.badRequestResponse(w, r, err)
|
|
|
|
return
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
command := &data.Command{
|
|
|
|
Name: input.Name,
|
|
|
|
Text: input.Text,
|
|
|
|
Category: input.Category,
|
|
|
|
Level: input.Level,
|
|
|
|
}
|
2022-08-16 16:35:45 +02:00
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
err = app.Models.Commands.Insert(command)
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
return
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
headers := make(http.Header)
|
|
|
|
headers.Set("Location", fmt.Sprintf("/v1/commands/%s", command.Name))
|
2022-08-16 16:35:45 +02:00
|
|
|
|
2022-08-17 21:17:29 +02:00
|
|
|
app.Logger.Infow("PUT Command",
|
|
|
|
"Command", command,
|
|
|
|
)
|
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
err = app.writeJSON(w, http.StatusCreated, envelope{"command": command}, headers)
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
2022-08-16 16:35:45 +02:00
|
|
|
}
|
2022-08-17 15:32:42 +02:00
|
|
|
|
2022-08-17 20:57:25 +02:00
|
|
|
func (app *application) updateCommandHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name, err := app.readCommandNameParam(r)
|
|
|
|
if err != nil {
|
|
|
|
app.notFoundResponse(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := app.Models.Commands.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, data.ErrRecordNotFound):
|
|
|
|
app.notFoundResponse(w, r)
|
|
|
|
default:
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var input struct {
|
|
|
|
Name *string `json:"name"`
|
|
|
|
Text *string `json:"text"`
|
|
|
|
Category *string `json:"category"`
|
|
|
|
Level *int `json:"level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.readJSON(w, r, &input)
|
|
|
|
if err != nil {
|
|
|
|
app.badRequestResponse(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is a name since we successfully queried the database for
|
|
|
|
// a command, so no need to check != nil.
|
|
|
|
command.Name = *input.Name
|
|
|
|
|
|
|
|
if input.Text != nil {
|
|
|
|
command.Text = *input.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Category != nil {
|
|
|
|
command.Category = *input.Category
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.Level != nil {
|
|
|
|
command.Level = *input.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Commands.Update(command)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, data.ErrEditConflict):
|
|
|
|
app.editConflictResponse(w, r)
|
|
|
|
default:
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:17:29 +02:00
|
|
|
app.Logger.Infow("PATCH Command",
|
|
|
|
"Command", command,
|
|
|
|
)
|
|
|
|
|
2022-08-17 20:57:25 +02:00
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"command": command}, nil)
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-17 18:07:50 +02:00
|
|
|
func (app *application) deleteCommandHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
name, err := app.readCommandNameParam(r)
|
|
|
|
if err != nil {
|
|
|
|
app.notFoundResponse(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = app.Models.Commands.Delete(name)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, data.ErrRecordNotFound):
|
|
|
|
app.notFoundResponse(w, r)
|
|
|
|
default:
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-17 21:17:29 +02:00
|
|
|
app.Logger.Infow("DELETE Command",
|
|
|
|
"Name", name,
|
|
|
|
)
|
|
|
|
|
2022-08-17 18:07:50 +02:00
|
|
|
err = app.writeJSON(w, http.StatusOK, envelope{"message": fmt.Sprintf("command %s deleted", name)}, nil)
|
|
|
|
if err != nil {
|
|
|
|
app.serverErrorResponse(w, r, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 15:32:42 +02:00
|
|
|
type envelope map[string]interface{}
|