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

@@ -33,7 +33,7 @@ type ContentHandler struct {
// NewContentHandler creates a new content handler
func NewContentHandler(database *db.Database, authService *auth.AuthService) *ContentHandler {
// Create database client for engine
dbClient := engine.NewDatabaseClient(database)
dbClient := database.NewContentRepository()
return &ContentHandler{
database: database,
@@ -311,7 +311,7 @@ func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
ID: contentID,
SiteID: siteID,
HtmlContent: req.HTMLContent,
OriginalTemplate: engine.ToNullString(req.OriginalTemplate),
OriginalTemplate: db.ToNullString(req.OriginalTemplate),
LastEditedBy: userID,
})
case "postgresql":
@@ -319,7 +319,7 @@ func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
ID: contentID,
SiteID: siteID,
HtmlContent: req.HTMLContent,
OriginalTemplate: engine.ToNullString(req.OriginalTemplate),
OriginalTemplate: db.ToNullString(req.OriginalTemplate),
LastEditedBy: userID,
})
default:
@@ -676,7 +676,7 @@ func (h *ContentHandler) convertToAPIContent(content interface{}) ContentItem {
ID: c.ID,
SiteID: c.SiteID,
HTMLContent: c.HtmlContent,
OriginalTemplate: engine.FromNullString(c.OriginalTemplate),
OriginalTemplate: db.FromNullString(c.OriginalTemplate),
CreatedAt: time.Unix(c.CreatedAt, 0),
UpdatedAt: time.Unix(c.UpdatedAt, 0),
LastEditedBy: c.LastEditedBy,
@@ -687,7 +687,7 @@ func (h *ContentHandler) convertToAPIContent(content interface{}) ContentItem {
ID: c.ID,
SiteID: c.SiteID,
HTMLContent: c.HtmlContent,
OriginalTemplate: engine.FromNullString(c.OriginalTemplate),
OriginalTemplate: db.FromNullString(c.OriginalTemplate),
CreatedAt: time.Unix(c.CreatedAt, 0),
UpdatedAt: time.Unix(c.UpdatedAt, 0),
LastEditedBy: c.LastEditedBy,
@@ -727,7 +727,7 @@ func (h *ContentHandler) convertToAPIVersionList(versionList interface{}) []Cont
ContentID: version.ContentID,
SiteID: version.SiteID,
HTMLContent: version.HtmlContent,
OriginalTemplate: engine.FromNullString(version.OriginalTemplate),
OriginalTemplate: db.FromNullString(version.OriginalTemplate),
CreatedAt: time.Unix(version.CreatedAt, 0),
CreatedBy: version.CreatedBy,
}
@@ -742,7 +742,7 @@ func (h *ContentHandler) convertToAPIVersionList(versionList interface{}) []Cont
ContentID: version.ContentID,
SiteID: version.SiteID,
HTMLContent: version.HtmlContent,
OriginalTemplate: engine.FromNullString(version.OriginalTemplate),
OriginalTemplate: db.FromNullString(version.OriginalTemplate),
CreatedAt: time.Unix(version.CreatedAt, 0),
CreatedBy: version.CreatedBy,
}
@@ -994,10 +994,11 @@ func (h *ContentHandler) CreateCollectionItem(w http.ResponseWriter, r *http.Req
}
// Create database client for atomic operations
dbClient := engine.NewDatabaseClient(h.database)
dbClient := h.database.NewContentRepository()
// Use atomic collection item creation
createdItem, err := dbClient.CreateCollectionItemAtomic(
context.Background(),
req.SiteID,
req.CollectionID,
req.TemplateID,