53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package server
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"net/http"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"g.fc.run/ai/byre/internal/api"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (s *Server) ListSavedCommands(w http.ResponseWriter, r *http.Request) {
|
||
|
|
cmds, err := s.store.SavedCommands(r.Context(), actor(r))
|
||
|
|
if err != nil {
|
||
|
|
writeStoreError(w, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
writeJSON(w, http.StatusOK, cmds)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) CreateSavedCommand(w http.ResponseWriter, r *http.Request) {
|
||
|
|
s.saveCommand(w, r, "", http.StatusCreated)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) UpdateSavedCommand(w http.ResponseWriter, r *http.Request, commandID string) {
|
||
|
|
s.saveCommand(w, r, commandID, http.StatusOK)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) saveCommand(w http.ResponseWriter, r *http.Request, id string, status int) {
|
||
|
|
req := api.SavedCommandSave{}
|
||
|
|
if !readJSON(w, r, &req) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if strings.TrimSpace(req.Name) == "" || strings.TrimSpace(req.Command) == "" {
|
||
|
|
writeError(w, http.StatusBadRequest, errors.New("a saved command needs a name and a command"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c, err := s.store.SaveSavedCommand(r.Context(), actor(r), id, req.Name, req.Command, actor(r))
|
||
|
|
if err != nil {
|
||
|
|
writeStoreError(w, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
writeJSON(w, status, c)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *Server) DeleteSavedCommand(w http.ResponseWriter, r *http.Request, commandID string) {
|
||
|
|
err := s.store.DeleteSavedCommand(r.Context(), actor(r), commandID, actor(r))
|
||
|
|
if err != nil {
|
||
|
|
writeStoreError(w, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
w.WriteHeader(http.StatusNoContent)
|
||
|
|
}
|