package api import ( "linkport/internal/store" "net/http" "strings" ) func (s *Server) templates(w http.ResponseWriter, r *http.Request, path string) { if path == "templates" && r.Method == "GET" { v, e := s.store.Templates() if e != nil { fail(w, 500, "cannot load templates") return } respond(w, 200, v) return } if path == "templates" && r.Method == "POST" { var b struct { Name string `json:"name"` Bundle *store.Bundle `json:"bundle"` FromCurrent bool `json:"fromCurrent"` } if !decodeLimit(w, r, &b, 24<<20) { return } if b.FromCurrent == (b.Bundle != nil) { fail(w, 400, "provide either a template bundle or fromCurrent") return } var t store.ConfigTemplate var e error if b.FromCurrent { t, e = s.store.SaveCurrentTemplate(b.Name) } else { t, e = s.store.SaveTemplate(b.Name, *b.Bundle) } if e != nil { fail(w, 400, e.Error()) return } s.store.Event("", source(r), "template_created", t.ID) respond(w, 201, t) return } parts := strings.Split(path, "/") if len(parts) == 2 && r.Method == "GET" { t, e := s.store.Template(parts[1]) if e != nil { fail(w, 404, "template not found") return } respond(w, 200, t) return } if len(parts) == 2 && r.Method == "DELETE" { if e := s.store.DeleteTemplate(parts[1]); e != nil { fail(w, 404, "template not found") return } s.store.Event("", source(r), "template_deleted", parts[1]) respond(w, 200, map[string]bool{"ok": true}) return } fail(w, 405, "method not allowed") }