code refactoring

This commit is contained in:
2024-02-12 12:02:23 +01:00
parent cc06487b0a
commit 65b0c8bc0e
7 changed files with 51 additions and 32 deletions

View File

@@ -30,5 +30,6 @@ func ApplyRouter(db *sql.DB, mdb *internal.MemoryDB, mq *internal.MessageQueue)
r.Post("/template", h.AddTemplate())
r.Get("/template/all", h.GetTemplates())
r.Delete("/template/{id}", h.DeleteTemplate())
r.Get("/tree", h.DirectoryTree())
}
}

View File

@@ -154,3 +154,22 @@ func (h *Handler) DeleteTemplate() http.HandlerFunc {
}
}
}
func (h *Handler) DirectoryTree() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
tree, err := h.service.DirectoryTree(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(w).Encode(tree)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
"github.com/marcopeocchi/yt-dlp-web-ui/server/sys"
)
type Service struct {
@@ -118,3 +119,7 @@ func (s *Service) DeleteTemplate(ctx context.Context, id string) error {
return err
}
func (s *Service) DirectoryTree(ctx context.Context) (*internal.Stack[sys.FSNode], error) {
return sys.DirectoryTree()
}