first commit
This commit is contained in:
214
go-api/internal/httpserver/definitions.go
Normal file
214
go-api/internal/httpserver/definitions.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/krow/krow-backend/go-api/internal/authctx"
|
||||
"github.com/krow/krow-backend/go-api/internal/domain"
|
||||
)
|
||||
|
||||
func (s *Server) routeDefinitions(mux *http.ServeMux) int {
|
||||
mux.HandleFunc("GET /api/v1/agent-definitions", s.handleAgentDefinitionsList)
|
||||
mux.HandleFunc("POST /api/v1/agent-definitions", s.handleAgentDefinitionsCreate)
|
||||
mux.HandleFunc("GET /api/v1/agent-definitions/{id}", s.handleAgentDefinitionsGet)
|
||||
mux.HandleFunc("PATCH /api/v1/agent-definitions/{id}", s.handleAgentDefinitionsUpdate)
|
||||
mux.HandleFunc("DELETE /api/v1/agent-definitions/{id}", s.handleAgentDefinitionsDelete)
|
||||
|
||||
mux.HandleFunc("GET /api/v1/skill-definitions", s.handleSkillDefinitionsList)
|
||||
mux.HandleFunc("POST /api/v1/skill-definitions", s.handleSkillDefinitionsCreate)
|
||||
mux.HandleFunc("GET /api/v1/skill-definitions/{id}", s.handleSkillDefinitionsGet)
|
||||
mux.HandleFunc("PATCH /api/v1/skill-definitions/{id}", s.handleSkillDefinitionsUpdate)
|
||||
mux.HandleFunc("DELETE /api/v1/skill-definitions/{id}", s.handleSkillDefinitionsDelete)
|
||||
|
||||
return 10
|
||||
}
|
||||
|
||||
/* ── Agents ─────────────────────────────────────────────────────────────── */
|
||||
|
||||
func (s *Server) handleAgentDefinitionsList(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
params, err := s.definitions.ParseListParams(r.URL.Query())
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
page, err := s.definitions.ListAgents(r.Context(), ident, params)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writePage(w, page)
|
||||
}
|
||||
|
||||
func (s *Server) handleAgentDefinitionsGet(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.GetAgent(r.Context(), ident, r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleAgentDefinitionsCreate(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := decodeBody(r)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.CreateAgent(r.Context(), ident, body)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusCreated, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleAgentDefinitionsUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := decodeBody(r)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.UpdateAgent(r.Context(), ident, r.PathValue("id"), body)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleAgentDefinitionsDelete(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.DeleteAgent(r.Context(), ident, r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
/* ── Skills ─────────────────────────────────────────────────────────────── */
|
||||
|
||||
func (s *Server) handleSkillDefinitionsList(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
params, err := s.definitions.ParseListParams(r.URL.Query())
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
page, err := s.definitions.ListSkills(r.Context(), ident, params)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writePage(w, page)
|
||||
}
|
||||
|
||||
func (s *Server) handleSkillDefinitionsGet(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.GetSkill(r.Context(), ident, r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleSkillDefinitionsCreate(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := decodeBody(r)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.CreateSkill(r.Context(), ident, body)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusCreated, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleSkillDefinitionsUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := decodeBody(r)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.UpdateSkill(r.Context(), ident, r.PathValue("id"), body)
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
|
||||
func (s *Server) handleSkillDefinitionsDelete(w http.ResponseWriter, r *http.Request) {
|
||||
ident, err := authctx.MustFrom(r.Context())
|
||||
if err != nil {
|
||||
writeError(w, s.log, domain.Internal(err))
|
||||
return
|
||||
}
|
||||
|
||||
rec, err := s.definitions.DeleteSkill(r.Context(), ident, r.PathValue("id"))
|
||||
if err != nil {
|
||||
writeError(w, s.log, err)
|
||||
return
|
||||
}
|
||||
writeRecord(w, http.StatusOK, rec)
|
||||
}
|
||||
Reference in New Issue
Block a user