mirror-nourybot/cmd/api/command.go

182 lines
3.7 KiB
Go
Raw Normal View History

package main
import (
"errors"
"fmt"
"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)
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)
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-18 22:01:48 +02:00
w.Header().Set("Access-Control-Allow-Origin", "*")
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)
}
}
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"`
}
err := app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}
command := &data.Command{
Name: input.Name,
Text: input.Text,
Category: input.Category,
Level: input.Level,
}
err = app.Models.Commands.Insert(command)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
headers := make(http.Header)
headers.Set("Location", fmt.Sprintf("/v1/commands/%s", command.Name))
2022-08-17 21:17:29 +02:00
app.Logger.Infow("PUT Command",
"Command", command,
)
err = app.writeJSON(w, http.StatusCreated, envelope{"command": command}, headers)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}
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)
2022-08-21 21:04:27 +02:00
return
2022-08-17 20:57:25 +02:00
default:
app.serverErrorResponse(w, r, err)
2022-08-21 21:04:27 +02:00
return
2022-08-17 20:57:25 +02:00
}
}
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)
}
}
type envelope map[string]interface{}