Files
unified-media-manager/internal/api/root_folder.go
2026-04-24 10:45:19 -07:00

68 lines
1.9 KiB
Go

package api
import (
"context"
"net/http"
"strconv"
"time"
"github.com/TopherMayor/unified-media-manager/internal/service"
"github.com/labstack/echo/v4"
)
func listRootFolders(svc *service.RootFolderService) echo.HandlerFunc {
return func(c echo.Context) error {
ctx, cancel := context.WithTimeout(c.Request().Context(), 10*time.Second)
defer cancel()
folders, err := svc.List(ctx)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
if folders == nil {
folders = []service.RootFolder{}
}
return c.JSON(http.StatusOK, folders)
}
}
func createRootFolder(svc *service.RootFolderService) echo.HandlerFunc {
return func(c echo.Context) error {
ctx, cancel := context.WithTimeout(c.Request().Context(), 5*time.Second)
defer cancel()
var req service.CreateRootFolderRequest
if err := c.Bind(&req); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
}
id, err := svc.Create(ctx, req)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusCreated, map[string]int64{"id": id})
}
}
func deleteRootFolder(svc *service.RootFolderService) echo.HandlerFunc {
return func(c echo.Context) error {
ctx, cancel := context.WithTimeout(c.Request().Context(), 5*time.Second)
defer cancel()
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
}
if err := svc.Delete(ctx, id); err != nil {
if err.Error() == "root folder not found" {
return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()})
}
return c.JSON(http.StatusOK, map[string]string{"status": "deleted"})
}
}