CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (pull_request) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Canceled after 0s
CI / Web (dms-browser) (pull_request) Canceled after 0s
CI / Web (pdf-viewer) (pull_request) Canceled after 0s
CI / Web (shl) (pull_request) Canceled after 0s
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
// Package autoruleapi stellt die Pflege der Auto-Regeln (MET-05) ueber HTTP bereit.
|
|
package autoruleapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"gitea.perlbach24.de/scripte/nexarch/dms/internal/autorule"
|
|
)
|
|
|
|
type Server struct{ pool *pgxpool.Pool }
|
|
|
|
func NewServer(pool *pgxpool.Pool) *Server { return &Server{pool: pool} }
|
|
|
|
func (s *Server) RegisterRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /auto-rules", s.handleList)
|
|
mux.HandleFunc("POST /auto-rules", s.handleCreate)
|
|
mux.HandleFunc("PUT /auto-rules/{id}", s.handleUpdate)
|
|
mux.HandleFunc("DELETE /auto-rules/{id}", s.handleDelete)
|
|
}
|
|
|
|
type ruleDTO struct {
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name"`
|
|
Priority int `json:"priority"`
|
|
Enabled bool `json:"enabled"`
|
|
FilenamePattern *string `json:"filename_pattern"`
|
|
TextPattern *string `json:"text_pattern"`
|
|
MimeType *string `json:"mime_type"`
|
|
AddTagIDs []string `json:"add_tag_ids"`
|
|
DocumentTypeID *string `json:"document_type_id"`
|
|
FolderID *string `json:"folder_id"`
|
|
StopProcessing bool `json:"stop_processing"`
|
|
}
|
|
|
|
func toDTO(r autorule.Rule) ruleDTO {
|
|
return ruleDTO{r.ID, r.Name, r.Priority, r.Enabled, r.FilenamePattern, r.TextPattern, r.MimeType, r.AddTagIDs, r.DocumentTypeID, r.FolderID, r.StopProcessing}
|
|
}
|
|
|
|
func (d ruleDTO) rule(id string) autorule.Rule {
|
|
return autorule.Rule{ID: id, Name: d.Name, Priority: d.Priority, Enabled: d.Enabled, FilenamePattern: d.FilenamePattern, TextPattern: d.TextPattern,
|
|
MimeType: d.MimeType, AddTagIDs: d.AddTagIDs, DocumentTypeID: d.DocumentTypeID, FolderID: d.FolderID, StopProcessing: d.StopProcessing}
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
|
|
func writeError(w http.ResponseWriter, status int, msg string) {
|
|
writeJSON(w, status, map[string]string{"error": msg})
|
|
}
|
|
|
|
func caller(w http.ResponseWriter, r *http.Request) (string, bool) {
|
|
c := strings.TrimSpace(r.URL.Query().Get("caller"))
|
|
if c == "" {
|
|
writeError(w, http.StatusBadRequest, "fehlender pflicht-parameter \"caller\"")
|
|
return "", false
|
|
}
|
|
return c, true
|
|
}
|
|
|
|
func fail(w http.ResponseWriter, err error) {
|
|
switch {
|
|
case errors.Is(err, autorule.ErrInvalidRule):
|
|
writeError(w, http.StatusUnprocessableEntity, err.Error())
|
|
case errors.Is(err, autorule.ErrNotFound):
|
|
writeError(w, http.StatusNotFound, err.Error())
|
|
default:
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleList(w http.ResponseWriter, r *http.Request) {
|
|
rules, err := autorule.List(r.Context(), s.pool)
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
out := make([]ruleDTO, len(rules))
|
|
for i, rule := range rules {
|
|
out[i] = toDTO(rule)
|
|
}
|
|
writeJSON(w, http.StatusOK, out)
|
|
}
|
|
|
|
func (s *Server) handleCreate(w http.ResponseWriter, r *http.Request) {
|
|
actor, ok := caller(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var d ruleDTO
|
|
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
|
|
writeError(w, http.StatusBadRequest, "ungueltiger request-body: "+err.Error())
|
|
return
|
|
}
|
|
rule, err := autorule.Create(r.Context(), s.pool, actor, d.rule(""))
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusCreated, toDTO(rule))
|
|
}
|
|
|
|
func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {
|
|
actor, ok := caller(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var d ruleDTO
|
|
if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
|
|
writeError(w, http.StatusBadRequest, "ungueltiger request-body: "+err.Error())
|
|
return
|
|
}
|
|
rule, err := autorule.Update(r.Context(), s.pool, actor, d.rule(r.PathValue("id")))
|
|
if err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, toDTO(rule))
|
|
}
|
|
|
|
func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) {
|
|
actor, ok := caller(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := autorule.Delete(r.Context(), s.pool, actor, r.PathValue("id")); err != nil {
|
|
fail(w, err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|