Refactor database layer to eliminate type switching and simplify architecture

- Replace type switching with clean repository pattern using sqlc-generated code
- Move ContentRepository interface and domain models to db package
- Create separate SQLiteRepository and PostgreSQLRepository implementations
- Remove unnecessary RepositoryAdapter and ContentClient interface duplication
- Update all clients (HTTP, Mock) to implement db.ContentRepository directly
- Add context.Context parameters to all repository methods (Go best practice)
- Eliminate duplicate domain models and type conversions
- Remove type aliases - use db package types directly throughout codebase
- Update engine, content managers, and API handlers to use repositories directly

Benefits:
- Zero runtime type switching overhead
- Single source of truth for domain models
- Clean package boundaries and separation of concerns
- Standard Go interface patterns with context support
- Easier testing with mockable repository interface
- Maintainable: adding new database types requires no changes to existing code
This commit is contained in:
2025-10-08 19:34:21 +02:00
parent 38c2897ece
commit 01b921bfa3
16 changed files with 785 additions and 712 deletions

View File

@@ -48,73 +48,3 @@ type ContentEntry struct {
HTMLContent string
Template string
}
// ContentClient interface for accessing content data
// This will be implemented by database clients, HTTP clients, and mock clients
type ContentClient interface {
GetContent(siteID, contentID string) (*ContentItem, error)
GetBulkContent(siteID string, contentIDs []string) (map[string]ContentItem, error)
GetAllContent(siteID string) (map[string]ContentItem, error)
CreateContent(siteID, contentID, htmlContent, originalTemplate, lastEditedBy string) (*ContentItem, error)
// Collection operations
GetCollection(siteID, collectionID string) (*CollectionItem, error)
CreateCollection(siteID, collectionID, containerHTML, lastEditedBy string) (*CollectionItem, error)
GetCollectionItems(siteID, collectionID string) ([]CollectionItemWithTemplate, error)
GetCollectionTemplates(siteID, collectionID string) ([]CollectionTemplateItem, error)
CreateCollectionTemplate(siteID, collectionID, name, htmlTemplate string, isDefault bool) (*CollectionTemplateItem, error)
CreateCollectionItem(siteID, collectionID, itemID string, templateID int, htmlContent string, position int, lastEditedBy string) (*CollectionItemWithTemplate, error)
CreateCollectionItemAtomic(siteID, collectionID string, templateID int, lastEditedBy string) (*CollectionItemWithTemplate, error)
}
// ContentItem represents a piece of content from the database
type ContentItem struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
HTMLContent string `json:"html_content"`
OriginalTemplate string `json:"original_template"`
UpdatedAt string `json:"updated_at"`
LastEditedBy string `json:"last_edited_by,omitempty"`
}
// ContentResponse represents the API response structure
type ContentResponse struct {
Content []ContentItem `json:"content"`
Error string `json:"error,omitempty"`
}
// CollectionItem represents a collection container from the database
type CollectionItem struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
ContainerHTML string `json:"container_html"`
UpdatedAt string `json:"updated_at"`
LastEditedBy string `json:"last_edited_by,omitempty"`
}
// CollectionTemplateItem represents a collection template from the database
type CollectionTemplateItem 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"`
}
// CollectionItemWithTemplate represents a collection item with its template information
type CollectionItemWithTemplate 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"`
UpdatedAt string `json:"updated_at"`
LastEditedBy string `json:"last_edited_by"`
// Template information
TemplateName string `json:"template_name"`
HTMLTemplate string `json:"html_template"`
IsDefault bool `json:"is_default"`
}