diff --git a/internal/handlers/collections.go b/internal/handlers/collections.go index 0069ddf..59aad7f 100644 --- a/internal/handlers/collections.go +++ b/internal/handlers/collections.go @@ -35,21 +35,21 @@ func NewCollectionHandler(db *database.Queries, libraryService *services.Library } type CreateCollectionRequest struct { - Name string `json:"name" validate:"required"` - Description string `json:"description"` - Color string `json:"color"` - Icon string `json:"icon"` - AutoAssignRules []services.Rule `json:"auto_assign_rules"` - ViewSettings map[string]interface{} `json:"view_settings"` + Name string `form:"name" json:"name" validate:"required"` + Description string `form:"description" json:"description"` + Color string `form:"color" json:"color"` + Icon string `form:"icon" json:"icon"` + AutoAssignRules []services.Rule `form:"auto_assign_rules" json:"auto_assign_rules"` + ViewSettings map[string]interface{} `form:"view_settings" json:"view_settings"` } type UpdateCollectionRequest struct { - Name string `json:"name" validate:"required"` - Description string `json:"description"` - Color string `json:"color"` - Icon string `json:"icon"` - AutoAssignRules []services.Rule `json:"auto_assign_rules"` - ViewSettings map[string]interface{} `json:"view_settings"` + Name string `form:"name" json:"name" validate:"required"` + Description string `form:"description" json:"description"` + Color string `form:"color" json:"color"` + Icon string `form:"icon" json:"icon"` + AutoAssignRules []services.Rule `form:"auto_assign_rules" json:"auto_assign_rules"` + ViewSettings map[string]interface{} `form:"view_settings" json:"view_settings"` } type AddBooksRequest struct { @@ -113,7 +113,7 @@ func (h *CollectionHandler) CreateCollection(c echo.Context) error { } bookCount := int32(0) - return c.JSON(http.StatusCreated, map[string]interface{}{ + response := map[string]interface{}{ "id": uuid.UUID(collection.ID.Bytes).String(), "user_id": uuid.UUID(collection.UserID.Bytes).String(), "name": collection.Name, @@ -124,7 +124,14 @@ func (h *CollectionHandler) CreateCollection(c echo.Context) error { "view_settings": collection.ViewSettings, "book_count": bookCount, "created_at": collection.CreatedAt.Time.String(), - }) + } + + // Add redirect header for HTMX requests + if c.Request().Header.Get("HX-Request") == "true" { + c.Response().Header().Set("HX-Redirect", "/collections") + } + + return c.JSON(http.StatusCreated, response) } func (h *CollectionHandler) GetCollections(c echo.Context) error { @@ -252,7 +259,7 @@ func (h *CollectionHandler) UpdateCollection(c echo.Context) error { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } - return c.JSON(http.StatusOK, map[string]interface{}{ + response := map[string]interface{}{ "id": uuid.UUID(collection.ID.Bytes).String(), "user_id": uuid.UUID(collection.UserID.Bytes).String(), "name": collection.Name, @@ -262,7 +269,14 @@ func (h *CollectionHandler) UpdateCollection(c echo.Context) error { "auto_assign_rules": json.RawMessage(collection.AutoAssignRules), "view_settings": json.RawMessage(collection.ViewSettings), "created_at": collection.CreatedAt.Time.String(), - }) + } + + // Add redirect header for HTMX requests + if c.Request().Header.Get("HX-Request") == "true" { + c.Response().Header().Set("HX-Redirect", "/collections") + } + + return c.JSON(http.StatusCreated, response) } func (h *CollectionHandler) DeleteCollection(c echo.Context) error { @@ -276,6 +290,11 @@ func (h *CollectionHandler) DeleteCollection(c echo.Context) error { return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) } + if c.Request().Header.Get("HX-Request") == "true" { + html := `