Consolidate type definitions and fix API contract

- Move all ContentItem, ContentClient, ContentResponse types to engine/types.go as single source of truth
- Remove duplicate type definitions from content/types.go
- Update all imports across codebase to use engine types
- Enhance engine to extract existing data-content-id from HTML markup
- Simplify frontend to always send html_markup, let server handle ID extraction/generation
- Fix contentId reference errors in frontend error handling
- Add getAttribute helper method to engine for ID extraction
- Add GetAllContent method to engine.DatabaseClient
- Update enhancer to use engine.ContentClient interface
- All builds and API endpoints verified working

This resolves the 400 Bad Request errors and creates a unified architecture where the server is the single source of truth for all ID generation and content type management.
This commit is contained in:
2025-09-16 16:45:29 +02:00
parent d0ac3088b4
commit d877366be0
15 changed files with 150 additions and 181 deletions

View File

@@ -8,6 +8,8 @@ import (
"net/url"
"strings"
"time"
"github.com/insertr/insertr/internal/engine"
)
// HTTPClient implements ContentClient for HTTP API access
@@ -29,7 +31,7 @@ func NewHTTPClient(baseURL, apiKey string) *HTTPClient {
}
// GetContent fetches a single content item by ID
func (c *HTTPClient) GetContent(siteID, contentID string) (*ContentItem, error) {
func (c *HTTPClient) GetContent(siteID, contentID string) (*engine.ContentItem, error) {
url := fmt.Sprintf("%s/api/content/%s?site_id=%s", c.BaseURL, contentID, siteID)
req, err := http.NewRequest("GET", url, nil)
@@ -60,7 +62,7 @@ func (c *HTTPClient) GetContent(siteID, contentID string) (*ContentItem, error)
return nil, fmt.Errorf("reading response: %w", err)
}
var item ContentItem
var item engine.ContentItem
if err := json.Unmarshal(body, &item); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -69,9 +71,9 @@ func (c *HTTPClient) GetContent(siteID, contentID string) (*ContentItem, error)
}
// GetBulkContent fetches multiple content items by IDs
func (c *HTTPClient) GetBulkContent(siteID string, contentIDs []string) (map[string]ContentItem, error) {
func (c *HTTPClient) GetBulkContent(siteID string, contentIDs []string) (map[string]engine.ContentItem, error) {
if len(contentIDs) == 0 {
return make(map[string]ContentItem), nil
return make(map[string]engine.ContentItem), nil
}
// Build query parameters
@@ -107,13 +109,13 @@ func (c *HTTPClient) GetBulkContent(siteID string, contentIDs []string) (map[str
return nil, fmt.Errorf("reading response: %w", err)
}
var response ContentResponse
var response engine.ContentResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
// Convert slice to map for easy lookup
result := make(map[string]ContentItem)
result := make(map[string]engine.ContentItem)
for _, item := range response.Content {
result[item.ID] = item
}
@@ -122,7 +124,7 @@ func (c *HTTPClient) GetBulkContent(siteID string, contentIDs []string) (map[str
}
// GetAllContent fetches all content for a site
func (c *HTTPClient) GetAllContent(siteID string) (map[string]ContentItem, error) {
func (c *HTTPClient) GetAllContent(siteID string) (map[string]engine.ContentItem, error) {
url := fmt.Sprintf("%s/api/content?site_id=%s", c.BaseURL, siteID)
req, err := http.NewRequest("GET", url, nil)
@@ -149,13 +151,13 @@ func (c *HTTPClient) GetAllContent(siteID string) (map[string]ContentItem, error
return nil, fmt.Errorf("reading response: %w", err)
}
var response ContentResponse
var response engine.ContentResponse
if err := json.Unmarshal(body, &response); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
// Convert slice to map for easy lookup
result := make(map[string]ContentItem)
result := make(map[string]engine.ContentItem)
for _, item := range response.Content {
result[item.ID] = item
}