Complete API handlers refactoring to eliminate type switching and use repository pattern consistently

This commit is contained in:
2025-10-08 20:36:20 +02:00
parent 01b921bfa3
commit bbf728d110
16 changed files with 268 additions and 2654 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -3,6 +3,7 @@ package api
import (
"log"
"net/http"
"slices"
"strings"
"time"
)
@@ -92,13 +93,7 @@ func CORSPreflightHandler(w http.ResponseWriter, r *http.Request) {
}
// Check if origin is allowed
originAllowed := false
for _, allowed := range allowedOrigins {
if origin == allowed {
originAllowed = true
break
}
}
originAllowed := slices.Contains(allowedOrigins, origin)
if originAllowed {
w.Header().Set("Access-Control-Allow-Origin", origin)

View File

@@ -1,35 +1,7 @@
package api
import "time"
// API request/response models
type ContentItem struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
HTMLContent string `json:"html_content"`
OriginalTemplate string `json:"original_template"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastEditedBy string `json:"last_edited_by"`
}
type ContentVersion struct {
VersionID int64 `json:"version_id"`
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
HTMLContent string `json:"html_content"`
OriginalTemplate string `json:"original_template"`
CreatedAt time.Time `json:"created_at"`
CreatedBy string `json:"created_by"`
}
type ContentResponse struct {
Content []ContentItem `json:"content"`
}
type ContentVersionsResponse struct {
Versions []ContentVersion `json:"versions"`
}
// Use db package types directly for API responses - no duplication needed
// Request models are kept below as they're different (input DTOs)
// Element context for backend ID generation
type ElementContext struct {
@@ -55,44 +27,7 @@ type RollbackContentRequest struct {
RolledBackBy string `json:"rolled_back_by,omitempty"`
}
// Collection API models
type CollectionItem struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
ContainerHTML string `json:"container_html"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastEditedBy string `json:"last_edited_by"`
Templates []CollectionTemplate `json:"templates,omitempty"`
Items []CollectionItemData `json:"items,omitempty"`
}
type CollectionTemplate struct {
TemplateID int `json:"template_id"`
CollectionID string `json:"collection_id"`
SiteID string `json:"site_id"`
Name string `json:"name"`
HTMLTemplate string `json:"html_template"`
IsDefault bool `json:"is_default"`
CreatedAt time.Time `json:"created_at"`
}
type CollectionItemData struct {
ItemID string `json:"item_id"`
CollectionID string `json:"collection_id"`
SiteID string `json:"site_id"`
TemplateID int `json:"template_id"`
HTMLContent string `json:"html_content"`
Position int `json:"position"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LastEditedBy string `json:"last_edited_by"`
TemplateName string `json:"template_name,omitempty"`
}
type CollectionResponse struct {
Collections []CollectionItem `json:"collections"`
}
// Collection response types also use db package directly
// Collection request models
type CreateCollectionRequest struct {