refactor: implement database-specific schema architecture with schema-as-query pattern

🏗️ **Major Database Schema Refactoring**

**Problem Solved**: Eliminated model duplication and multiple sources of truth by:
- Removed duplicate models (`internal/models/content.go`)
- Replaced inlined schema strings with sqlc-generated setup functions
- Implemented database-specific schemas with proper NOT NULL constraints

**Key Improvements**:
 **Single Source of Truth**: Database schemas define all types, no manual sync needed
 **Clean Generated Types**: sqlc generates `string` and `int64` instead of `sql.NullString/sql.NullTime`
 **Schema-as-Query Pattern**: Setup functions generated by sqlc for type safety
 **Database-Specific Optimization**: SQLite INTEGER timestamps, PostgreSQL BIGINT timestamps
 **Cross-Database Compatibility**: Single codebase supports both SQLite and PostgreSQL

**Architecture Changes**:
- `db/sqlite/` - SQLite-specific schema and setup queries
- `db/postgresql/` - PostgreSQL-specific schema and setup queries
- `db/queries/` - Cross-database CRUD queries using `sqlc.arg()` syntax
- `internal/db/database.go` - Database abstraction with runtime selection
- `internal/api/models.go` - Clean API models for requests/responses

**Version Control System**: Complete element-level history with user attribution and rollback

**Verification**:  Full API workflow tested (create → update → rollback → versions)
**Production Ready**: Supports SQLite (development) → PostgreSQL (production) migration
This commit is contained in:
2025-09-09 00:25:07 +02:00
parent 161c320304
commit bab329b429
41 changed files with 3703 additions and 561 deletions

View File

@@ -1,158 +1,123 @@
# Insertr Content Server
The HTTP API server that provides content storage and retrieval for the Insertr CMS system.
REST API server for the Insertr CMS system. Provides content management with version control and user attribution.
## 🚀 Quick Start
## Features
### Build and Run
```bash
# Build the server
go build -o insertr-server ./cmd/server
- **Content Management**: Full CRUD operations for content items
- **Version Control**: Complete edit history with rollback functionality
- **User Attribution**: Track who made each change
- **Type-Safe Database**: Uses sqlc for generated Go code from SQL
- **SQLite & PostgreSQL**: Database flexibility for development to production
# Start with default settings
./insertr-server
## API Endpoints
# Start with custom port and database
./insertr-server --port 8080 --db ./content.db
```
### Development
```bash
# Install dependencies
go mod tidy
# Run directly with go
go run ./cmd/server --port 8080
```
## 📊 API Endpoints
The server implements the exact API contract expected by both the Go CLI client and JavaScript browser client:
### Content Retrieval
### Content Operations
- `GET /api/content?site_id={site}` - Get all content for a site
- `GET /api/content/{id}?site_id={site}` - Get single content item
- `GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}` - Get multiple items
### Content Modification
- `GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}` - Get multiple content items
- `POST /api/content` - Create new content
- `PUT /api/content/{id}?site_id={site}` - Update existing content
- `PUT /api/content/{id}?site_id={site}` - Update existing content
- `DELETE /api/content/{id}?site_id={site}` - Delete content
### System
- `GET /health` - Health check endpoint
### Version Control
- `GET /api/content/{id}/versions?site_id={site}` - Get version history
- `POST /api/content/{id}/rollback?site_id={site}` - Rollback to specific version
## 🗄️ Database
### Health & Status
- `GET /health` - Server health check
Uses SQLite by default for simplicity. The database schema:
## User Attribution
```sql
CREATE TABLE content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id, site_id)
);
All content operations support user attribution via the `X-User-ID` header:
```bash
curl -X PUT "http://localhost:8080/api/content/hero-title?site_id=demo" \
-H "Content-Type: application/json" \
-H "X-User-ID: john@example.com" \
-d '{"value": "Updated content"}'
```
## 🔧 Configuration
## Quick Start
### Command Line Options
- `--port` - Server port (default: 8080)
- `--db` - SQLite database path (default: ./insertr.db)
```bash
# Build server
go build -o insertr-server ./cmd/server
### CORS
Currently configured for development with `Access-Control-Allow-Origin: *`.
For production, configure CORS appropriately.
# Start server
./insertr-server --port 8080
## 🧪 Testing
# Check health
curl http://localhost:8080/health
```
### API Testing Examples
## Development
### Using sqlc
```bash
# Install sqlc
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# Generate Go code from SQL
sqlc generate
# Build with generated code
go build ./cmd/server
```
### Database Schema
See `db/schema/schema.sql` for the complete schema. Key tables:
- `content` - Current content versions
- `content_versions` - Complete version history
### Example Version Control Workflow
```bash
# Create content
curl -X POST "http://localhost:8080/api/content" \
-H "Content-Type: application/json" \
-d '{"id":"hero-title","value":"Welcome!","type":"text"}'
-H "X-User-ID: alice@example.com" \
-d '{
"id": "hero-title",
"site_id": "demo",
"value": "Original Title",
"type": "text"
}'
# Get content
curl "http://localhost:8080/api/content/hero-title?site_id=demo"
# Update content
# Update content (creates version)
curl -X PUT "http://localhost:8080/api/content/hero-title?site_id=demo" \
-H "Content-Type: application/json" \
-d '{"value":"Updated Welcome!"}'
-H "X-User-ID: bob@example.com" \
-d '{"value": "Updated Title"}'
# View version history
curl "http://localhost:8080/api/content/hero-title/versions?site_id=demo"
# Rollback to version 1
curl -X POST "http://localhost:8080/api/content/hero-title/rollback?site_id=demo" \
-H "Content-Type: application/json" \
-H "X-User-ID: admin@example.com" \
-d '{"version_id": 1}'
```
### Integration Testing
```bash
# From project root
./test-integration.sh
```
## 🏗️ Architecture Integration
This server bridges the gap between:
1. **Browser Editor** (`lib/`) - JavaScript client that saves edits
2. **CLI Enhancement** (`insertr-cli/`) - Go client that pulls content during builds
3. **Static Site Generation** - Enhanced HTML with database content
### Content Flow
```
Browser Edit → HTTP Server → SQLite Database
CLI Build Process ← HTTP Server ← SQLite Database
Enhanced Static Site
```
## 🚀 Production Deployment
### Docker (Recommended)
```dockerfile
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o insertr-server ./cmd/server
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/insertr-server .
EXPOSE 8080
CMD ["./insertr-server"]
```
## Configuration
### Environment Variables
- `PORT` - Server port
- `DB_PATH` - Database file path
- `CORS_ORIGIN` - Allowed CORS origin for production
- `PORT` - Server port (default: 8080)
- `DB_PATH` - SQLite database file path (default: ./insertr.db)
### Health Monitoring
The `/health` endpoint returns JSON status for monitoring:
```json
{"status":"healthy","service":"insertr-server"}
### Command Line Flags
```bash
./insertr-server --help
```
## 🔐 Security Considerations
## Production Deployment
### Current State (Development)
- Open CORS policy
- No authentication required
- SQLite database (single file)
### Production TODO
- [ ] JWT/OAuth authentication
- [ ] PostgreSQL database option
- [ ] Rate limiting
- [ ] Input validation and sanitization
- [ ] HTTPS enforcement
- [ ] Configurable CORS origins
---
**Status**: ✅ Fully functional development server
**Next**: Production hardening and authentication
1. **Database**: Consider PostgreSQL for production scale
2. **Authentication**: Integrate with your auth system via middleware
3. **CORS**: Configure appropriate CORS policies
4. **SSL**: Serve over HTTPS
5. **Monitoring**: Add logging and metrics collection

View File

@@ -23,7 +23,7 @@ func main() {
flag.Parse()
// Initialize database
database, err := db.NewSQLiteDB(*dbPath)
database, err := db.NewDatabase(*dbPath)
if err != nil {
log.Fatalf("Failed to initialize database: %v", err)
}
@@ -48,12 +48,17 @@ func main() {
// Content endpoints matching the expected API contract
apiRouter.HandleFunc("/bulk", contentHandler.GetBulkContent).Methods("GET")
apiRouter.HandleFunc("/{id}/versions", contentHandler.GetContentVersions).Methods("GET")
apiRouter.HandleFunc("/{id}/rollback", contentHandler.RollbackContent).Methods("POST")
apiRouter.HandleFunc("/{id}", contentHandler.GetContent).Methods("GET")
apiRouter.HandleFunc("/{id}", contentHandler.UpdateContent).Methods("PUT")
apiRouter.HandleFunc("/{id}", contentHandler.DeleteContent).Methods("DELETE")
apiRouter.HandleFunc("", contentHandler.GetAllContent).Methods("GET")
apiRouter.HandleFunc("", contentHandler.CreateContent).Methods("POST")
// Handle CORS preflight requests explicitly
apiRouter.HandleFunc("/{id}/versions", api.CORSPreflightHandler).Methods("OPTIONS")
apiRouter.HandleFunc("/{id}/rollback", api.CORSPreflightHandler).Methods("OPTIONS")
apiRouter.HandleFunc("/{id}", api.CORSPreflightHandler).Methods("OPTIONS")
apiRouter.HandleFunc("", api.CORSPreflightHandler).Methods("OPTIONS")
apiRouter.HandleFunc("/bulk", api.CORSPreflightHandler).Methods("OPTIONS")
@@ -68,8 +73,11 @@ func main() {
fmt.Printf(" GET /api/content?site_id={site}\n")
fmt.Printf(" GET /api/content/{id}?site_id={site}\n")
fmt.Printf(" GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}\n")
fmt.Printf(" GET /api/content/{id}/versions?site_id={site}\n")
fmt.Printf(" POST /api/content\n")
fmt.Printf(" PUT /api/content/{id}\n")
fmt.Printf(" POST /api/content/{id}/rollback\n")
fmt.Printf(" DELETE /api/content/{id}?site_id={site}\n")
fmt.Printf("\n🔄 Press Ctrl+C to shutdown gracefully\n\n")
// Setup graceful shutdown

View File

@@ -0,0 +1,42 @@
-- PostgreSQL-specific schema with BIGINT UNIX timestamps
-- Main content table (current versions only)
CREATE TABLE content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at BIGINT DEFAULT EXTRACT(EPOCH FROM NOW()) NOT NULL,
updated_at BIGINT DEFAULT EXTRACT(EPOCH FROM NOW()) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
);
-- Version history table for rollback functionality
CREATE TABLE content_versions (
version_id SERIAL PRIMARY KEY,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at BIGINT DEFAULT EXTRACT(EPOCH FROM NOW()) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);
-- Function and trigger to automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = EXTRACT(EPOCH FROM NOW());
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_content_updated_at
BEFORE UPDATE ON content
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

View File

@@ -0,0 +1,47 @@
-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
);
-- name: InitializeVersionsTable :exec
CREATE TABLE IF NOT EXISTS content_versions (
version_id SERIAL PRIMARY KEY,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
);
-- name: CreateContentSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
-- name: CreateContentUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);
-- name: CreateVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);
-- name: CreateUpdateFunction :exec
CREATE OR REPLACE FUNCTION update_content_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = EXTRACT(EPOCH FROM NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- name: CreateUpdateTrigger :exec
DROP TRIGGER IF EXISTS update_content_updated_at ON content;
CREATE TRIGGER update_content_updated_at
BEFORE UPDATE ON content
FOR EACH ROW
EXECUTE FUNCTION update_content_timestamp();

View File

@@ -0,0 +1,30 @@
-- name: GetContent :one
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id);
-- name: GetAllContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = sqlc.arg(site_id)
ORDER BY updated_at DESC;
-- name: GetBulkContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = sqlc.arg(site_id) AND id IN (sqlc.slice('ids'));
-- name: CreateContent :one
INSERT INTO content (id, site_id, value, type, last_edited_by)
VALUES (sqlc.arg(id), sqlc.arg(site_id), sqlc.arg(value), sqlc.arg(type), sqlc.arg(last_edited_by))
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by;
-- name: UpdateContent :one
UPDATE content
SET value = sqlc.arg(value), type = sqlc.arg(type), last_edited_by = sqlc.arg(last_edited_by)
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id)
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by;
-- name: DeleteContent :exec
DELETE FROM content
WHERE id = sqlc.arg(id) AND site_id = sqlc.arg(site_id);

View File

@@ -0,0 +1,29 @@
-- name: CreateContentVersion :exec
INSERT INTO content_versions (content_id, site_id, value, type, created_by)
VALUES (sqlc.arg(content_id), sqlc.arg(site_id), sqlc.arg(value), sqlc.arg(type), sqlc.arg(created_by));
-- name: GetContentVersionHistory :many
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE content_id = sqlc.arg(content_id) AND site_id = sqlc.arg(site_id)
ORDER BY created_at DESC
LIMIT sqlc.arg(limit_count);
-- name: GetContentVersion :one
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE version_id = sqlc.arg(version_id);
-- name: GetAllVersionsForSite :many
SELECT
cv.version_id, cv.content_id, cv.site_id, cv.value, cv.type, cv.created_at, cv.created_by,
c.value as current_value
FROM content_versions cv
LEFT JOIN content c ON cv.content_id = c.id AND cv.site_id = c.site_id
WHERE cv.site_id = sqlc.arg(site_id)
ORDER BY cv.created_at DESC
LIMIT sqlc.arg(limit_count);
-- name: DeleteOldVersions :exec
DELETE FROM content_versions
WHERE created_at < sqlc.arg(created_before) AND site_id = sqlc.arg(site_id);

View File

@@ -0,0 +1,36 @@
-- SQLite-specific schema with INTEGER timestamps
-- Main content table (current versions only)
CREATE TABLE content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
updated_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
);
-- Version history table for rollback functionality
CREATE TABLE content_versions (
version_id INTEGER PRIMARY KEY AUTOINCREMENT,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
);
-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);
-- Trigger to automatically update updated_at timestamp
CREATE TRIGGER IF NOT EXISTS update_content_updated_at
AFTER UPDATE ON content
FOR EACH ROW
BEGIN
UPDATE content SET updated_at = strftime('%s', 'now') WHERE id = NEW.id AND site_id = NEW.site_id;
END;

View File

@@ -0,0 +1,39 @@
-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
updated_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
);
-- name: InitializeVersionsTable :exec
CREATE TABLE IF NOT EXISTS content_versions (
version_id INTEGER PRIMARY KEY AUTOINCREMENT,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
);
-- name: CreateContentSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
-- name: CreateContentUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);
-- name: CreateVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);
-- name: CreateUpdateTrigger :exec
CREATE TRIGGER IF NOT EXISTS update_content_updated_at
AFTER UPDATE ON content
FOR EACH ROW
BEGIN
UPDATE content SET updated_at = strftime('%s', 'now') WHERE id = NEW.id AND site_id = NEW.site_id;
END;

View File

@@ -6,3 +6,5 @@ require (
github.com/gorilla/mux v1.8.1
github.com/mattn/go-sqlite3 v1.14.32
)
require github.com/lib/pq v1.10.9 // indirect

View File

@@ -1,4 +1,6 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

Binary file not shown.

View File

@@ -1,27 +1,34 @@
package api
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/mux"
"github.com/insertr/server/internal/db"
"github.com/insertr/server/internal/models"
"github.com/insertr/server/internal/db/postgresql"
"github.com/insertr/server/internal/db/sqlite"
)
// ContentHandler handles all content-related HTTP requests
type ContentHandler struct {
db *db.SQLiteDB
database *db.Database
}
// NewContentHandler creates a new content handler
func NewContentHandler(database *db.SQLiteDB) *ContentHandler {
return &ContentHandler{db: database}
func NewContentHandler(database *db.Database) *ContentHandler {
return &ContentHandler{
database: database,
}
}
// GetContent handles GET /api/content/{id}?site_id={site}
// GetContent handles GET /api/content/{id}
func (h *ContentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contentID := vars["id"]
@@ -32,78 +39,119 @@ func (h *ContentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
return
}
if contentID == "" {
http.Error(w, "content ID is required", http.StatusBadRequest)
var content interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
content, err = h.database.GetSQLiteQueries().GetContent(context.Background(), sqlite.GetContentParams{
ID: contentID,
SiteID: siteID,
})
case "postgresql":
content, err = h.database.GetPostgreSQLQueries().GetContent(context.Background(), postgresql.GetContentParams{
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
content, err := h.db.GetContent(siteID, contentID)
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Content not found", http.StatusNotFound)
return
}
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
if content == nil {
http.NotFound(w, r)
return
}
item := h.convertToAPIContent(content)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(content)
json.NewEncoder(w).Encode(item)
}
// GetAllContent handles GET /api/content?site_id={site}
// GetAllContent handles GET /api/content
func (h *ContentHandler) GetAllContent(w http.ResponseWriter, r *http.Request) {
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
items, err := h.db.GetAllContent(siteID)
var dbContent interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
dbContent, err = h.database.GetSQLiteQueries().GetAllContent(context.Background(), siteID)
case "postgresql":
dbContent, err = h.database.GetPostgreSQLQueries().GetAllContent(context.Background(), siteID)
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
response := models.ContentResponse{
Content: items,
}
items := h.convertToAPIContentList(dbContent)
response := ContentResponse{Content: items}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// GetBulkContent handles GET /api/content/bulk?site_id={site}&ids[]={id1}&ids[]={id2}
// GetBulkContent handles GET /api/content/bulk
func (h *ContentHandler) GetBulkContent(w http.ResponseWriter, r *http.Request) {
siteID := r.URL.Query().Get("site_id")
contentIDs := r.URL.Query()["ids"]
if siteID == "" {
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
if len(contentIDs) == 0 {
// Return empty response if no IDs provided
response := models.ContentResponse{
Content: []models.ContentItem{},
// Parse ids parameter
idsParam := r.URL.Query()["ids[]"]
if len(idsParam) == 0 {
// Try single ids parameter
idsStr := r.URL.Query().Get("ids")
if idsStr == "" {
http.Error(w, "ids parameter is required", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
idsParam = strings.Split(idsStr, ",")
}
var dbContent interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
dbContent, err = h.database.GetSQLiteQueries().GetBulkContent(context.Background(), sqlite.GetBulkContentParams{
SiteID: siteID,
Ids: idsParam,
})
case "postgresql":
dbContent, err = h.database.GetPostgreSQLQueries().GetBulkContent(context.Background(), postgresql.GetBulkContentParams{
SiteID: siteID,
Ids: idsParam,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
items, err := h.db.GetBulkContent(siteID, contentIDs)
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
response := models.ContentResponse{
Content: items,
}
items := h.convertToAPIContentList(dbContent)
response := ContentResponse{Content: items}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
@@ -111,55 +159,64 @@ func (h *ContentHandler) GetBulkContent(w http.ResponseWriter, r *http.Request)
// CreateContent handles POST /api/content
func (h *ContentHandler) CreateContent(w http.ResponseWriter, r *http.Request) {
var req CreateContentRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
siteID = "demo" // Default to demo site for compatibility
siteID = req.SiteID // fallback to request body
}
if siteID == "" {
siteID = "default" // final fallback
}
var req models.CreateContentRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
// Extract user from request (for now, use X-User-ID header or fallback)
userID := r.Header.Get("X-User-ID")
if userID == "" && req.CreatedBy != "" {
userID = req.CreatedBy
}
if userID == "" {
userID = "anonymous"
}
var content interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
content, err = h.database.GetSQLiteQueries().CreateContent(context.Background(), sqlite.CreateContentParams{
ID: req.ID,
SiteID: siteID,
Value: req.Value,
Type: req.Type,
LastEditedBy: userID,
})
case "postgresql":
content, err = h.database.GetPostgreSQLQueries().CreateContent(context.Background(), postgresql.CreateContentParams{
ID: req.ID,
SiteID: siteID,
Value: req.Value,
Type: req.Type,
LastEditedBy: userID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
// Validate content type
validTypes := []string{"text", "markdown", "link"}
isValidType := false
for _, validType := range validTypes {
if req.Type == validType {
isValidType = true
break
}
}
if !isValidType {
http.Error(w, fmt.Sprintf("Invalid content type. Must be one of: %s", strings.Join(validTypes, ", ")), http.StatusBadRequest)
return
}
// Check if content already exists
existing, err := h.db.GetContent(siteID, req.ID)
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("Failed to create content: %v", err), http.StatusInternalServerError)
return
}
if existing != nil {
http.Error(w, "Content with this ID already exists", http.StatusConflict)
return
}
// Create content
content, err := h.db.CreateContent(siteID, req.ID, req.Value, req.Type)
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
item := h.convertToAPIContent(content)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(content)
json.NewEncoder(w).Encode(item)
}
// UpdateContent handles PUT /api/content/{id}
@@ -169,32 +226,443 @@ func (h *ContentHandler) UpdateContent(w http.ResponseWriter, r *http.Request) {
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
siteID = "demo" // Default to demo site for compatibility
}
if contentID == "" {
http.Error(w, "content ID is required", http.StatusBadRequest)
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
var req models.UpdateContentRequest
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&req); err != nil {
http.Error(w, fmt.Sprintf("Invalid JSON: %v", err), http.StatusBadRequest)
var req UpdateContentRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Extract user from request
userID := r.Header.Get("X-User-ID")
if userID == "" && req.UpdatedBy != "" {
userID = req.UpdatedBy
}
if userID == "" {
userID = "anonymous"
}
// Get current content for version history and type preservation
var currentContent interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
currentContent, err = h.database.GetSQLiteQueries().GetContent(context.Background(), sqlite.GetContentParams{
ID: contentID,
SiteID: siteID,
})
case "postgresql":
currentContent, err = h.database.GetPostgreSQLQueries().GetContent(context.Background(), postgresql.GetContentParams{
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
// Update content
content, err := h.db.UpdateContent(siteID, contentID, req.Value)
if err != nil {
if strings.Contains(err.Error(), "not found") {
http.NotFound(w, r)
if err == sql.ErrNoRows {
http.Error(w, "Content not found", http.StatusNotFound)
return
}
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
// Archive current version before updating
err = h.createContentVersion(currentContent)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to create version: %v", err), http.StatusInternalServerError)
return
}
// Determine content type
contentType := req.Type
if contentType == "" {
contentType = h.getContentType(currentContent) // preserve existing type if not specified
}
// Update the content
var updatedContent interface{}
switch h.database.GetDBType() {
case "sqlite3":
updatedContent, err = h.database.GetSQLiteQueries().UpdateContent(context.Background(), sqlite.UpdateContentParams{
Value: req.Value,
Type: contentType,
LastEditedBy: userID,
ID: contentID,
SiteID: siteID,
})
case "postgresql":
updatedContent, err = h.database.GetPostgreSQLQueries().UpdateContent(context.Background(), postgresql.UpdateContentParams{
Value: req.Value,
Type: contentType,
LastEditedBy: userID,
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Failed to update content: %v", err), http.StatusInternalServerError)
return
}
item := h.convertToAPIContent(updatedContent)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(content)
json.NewEncoder(w).Encode(item)
}
// DeleteContent handles DELETE /api/content/{id}
func (h *ContentHandler) DeleteContent(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contentID := vars["id"]
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
var err error
switch h.database.GetDBType() {
case "sqlite3":
err = h.database.GetSQLiteQueries().DeleteContent(context.Background(), sqlite.DeleteContentParams{
ID: contentID,
SiteID: siteID,
})
case "postgresql":
err = h.database.GetPostgreSQLQueries().DeleteContent(context.Background(), postgresql.DeleteContentParams{
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Failed to delete content: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
// GetContentVersions handles GET /api/content/{id}/versions
func (h *ContentHandler) GetContentVersions(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contentID := vars["id"]
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
// Parse limit parameter (default to 10)
limit := int64(10)
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
if parsedLimit, err := strconv.ParseInt(limitStr, 10, 64); err == nil {
limit = parsedLimit
}
}
var dbVersions interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
dbVersions, err = h.database.GetSQLiteQueries().GetContentVersionHistory(context.Background(), sqlite.GetContentVersionHistoryParams{
ContentID: contentID,
SiteID: siteID,
LimitCount: limit,
})
case "postgresql":
// Note: PostgreSQL uses different parameter names due to int32 vs int64
dbVersions, err = h.database.GetPostgreSQLQueries().GetContentVersionHistory(context.Background(), postgresql.GetContentVersionHistoryParams{
ContentID: contentID,
SiteID: siteID,
LimitCount: int32(limit),
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
versions := h.convertToAPIVersionList(dbVersions)
response := ContentVersionsResponse{Versions: versions}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// RollbackContent handles POST /api/content/{id}/rollback
func (h *ContentHandler) RollbackContent(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
contentID := vars["id"]
siteID := r.URL.Query().Get("site_id")
if siteID == "" {
http.Error(w, "site_id parameter is required", http.StatusBadRequest)
return
}
var req RollbackContentRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
// Get the target version
var targetVersion interface{}
var err error
switch h.database.GetDBType() {
case "sqlite3":
targetVersion, err = h.database.GetSQLiteQueries().GetContentVersion(context.Background(), req.VersionID)
case "postgresql":
targetVersion, err = h.database.GetPostgreSQLQueries().GetContentVersion(context.Background(), int32(req.VersionID))
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Version not found", http.StatusNotFound)
return
}
http.Error(w, fmt.Sprintf("Database error: %v", err), http.StatusInternalServerError)
return
}
// Verify the version belongs to the correct content
if !h.versionMatches(targetVersion, contentID, siteID) {
http.Error(w, "Version does not match content", http.StatusBadRequest)
return
}
// Extract user from request
userID := r.Header.Get("X-User-ID")
if userID == "" && req.RolledBackBy != "" {
userID = req.RolledBackBy
}
if userID == "" {
userID = "anonymous"
}
// Archive current version before rollback
var currentContent interface{}
switch h.database.GetDBType() {
case "sqlite3":
currentContent, err = h.database.GetSQLiteQueries().GetContent(context.Background(), sqlite.GetContentParams{
ID: contentID,
SiteID: siteID,
})
case "postgresql":
currentContent, err = h.database.GetPostgreSQLQueries().GetContent(context.Background(), postgresql.GetContentParams{
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Failed to get current content: %v", err), http.StatusInternalServerError)
return
}
err = h.createContentVersion(currentContent)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to create version: %v", err), http.StatusInternalServerError)
return
}
// Rollback to target version
var updatedContent interface{}
switch h.database.GetDBType() {
case "sqlite3":
sqliteVersion := targetVersion.(sqlite.ContentVersion)
updatedContent, err = h.database.GetSQLiteQueries().UpdateContent(context.Background(), sqlite.UpdateContentParams{
Value: sqliteVersion.Value,
Type: sqliteVersion.Type,
LastEditedBy: userID,
ID: contentID,
SiteID: siteID,
})
case "postgresql":
pgVersion := targetVersion.(postgresql.ContentVersion)
updatedContent, err = h.database.GetPostgreSQLQueries().UpdateContent(context.Background(), postgresql.UpdateContentParams{
Value: pgVersion.Value,
Type: pgVersion.Type,
LastEditedBy: userID,
ID: contentID,
SiteID: siteID,
})
default:
http.Error(w, "Unsupported database type", http.StatusInternalServerError)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Failed to rollback content: %v", err), http.StatusInternalServerError)
return
}
item := h.convertToAPIContent(updatedContent)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(item)
}
// Helper functions for type conversion
func (h *ContentHandler) convertToAPIContent(content interface{}) ContentItem {
switch h.database.GetDBType() {
case "sqlite3":
c := content.(sqlite.Content)
return ContentItem{
ID: c.ID,
SiteID: c.SiteID,
Value: c.Value,
Type: c.Type,
CreatedAt: time.Unix(c.CreatedAt, 0),
UpdatedAt: time.Unix(c.UpdatedAt, 0),
LastEditedBy: c.LastEditedBy,
}
case "postgresql":
c := content.(postgresql.Content)
return ContentItem{
ID: c.ID,
SiteID: c.SiteID,
Value: c.Value,
Type: c.Type,
CreatedAt: time.Unix(c.CreatedAt, 0),
UpdatedAt: time.Unix(c.UpdatedAt, 0),
LastEditedBy: c.LastEditedBy,
}
}
return ContentItem{} // Should never happen
}
func (h *ContentHandler) convertToAPIContentList(contentList interface{}) []ContentItem {
switch h.database.GetDBType() {
case "sqlite3":
list := contentList.([]sqlite.Content)
items := make([]ContentItem, len(list))
for i, content := range list {
items[i] = h.convertToAPIContent(content)
}
return items
case "postgresql":
list := contentList.([]postgresql.Content)
items := make([]ContentItem, len(list))
for i, content := range list {
items[i] = h.convertToAPIContent(content)
}
return items
}
return []ContentItem{} // Should never happen
}
func (h *ContentHandler) convertToAPIVersionList(versionList interface{}) []ContentVersion {
switch h.database.GetDBType() {
case "sqlite3":
list := versionList.([]sqlite.ContentVersion)
versions := make([]ContentVersion, len(list))
for i, version := range list {
versions[i] = ContentVersion{
VersionID: version.VersionID,
ContentID: version.ContentID,
SiteID: version.SiteID,
Value: version.Value,
Type: version.Type,
CreatedAt: time.Unix(version.CreatedAt, 0),
CreatedBy: version.CreatedBy,
}
}
return versions
case "postgresql":
list := versionList.([]postgresql.ContentVersion)
versions := make([]ContentVersion, len(list))
for i, version := range list {
versions[i] = ContentVersion{
VersionID: int64(version.VersionID),
ContentID: version.ContentID,
SiteID: version.SiteID,
Value: version.Value,
Type: version.Type,
CreatedAt: time.Unix(version.CreatedAt, 0),
CreatedBy: version.CreatedBy,
}
}
return versions
}
return []ContentVersion{} // Should never happen
}
func (h *ContentHandler) createContentVersion(content interface{}) error {
switch h.database.GetDBType() {
case "sqlite3":
c := content.(sqlite.Content)
return h.database.GetSQLiteQueries().CreateContentVersion(context.Background(), sqlite.CreateContentVersionParams{
ContentID: c.ID,
SiteID: c.SiteID,
Value: c.Value,
Type: c.Type,
CreatedBy: c.LastEditedBy,
})
case "postgresql":
c := content.(postgresql.Content)
return h.database.GetPostgreSQLQueries().CreateContentVersion(context.Background(), postgresql.CreateContentVersionParams{
ContentID: c.ID,
SiteID: c.SiteID,
Value: c.Value,
Type: c.Type,
CreatedBy: c.LastEditedBy,
})
}
return fmt.Errorf("unsupported database type")
}
func (h *ContentHandler) getContentType(content interface{}) string {
switch h.database.GetDBType() {
case "sqlite3":
return content.(sqlite.Content).Type
case "postgresql":
return content.(postgresql.Content).Type
}
return ""
}
func (h *ContentHandler) versionMatches(version interface{}, contentID, siteID string) bool {
switch h.database.GetDBType() {
case "sqlite3":
v := version.(sqlite.ContentVersion)
return v.ContentID == contentID && v.SiteID == siteID
case "postgresql":
v := version.(postgresql.ContentVersion)
return v.ContentID == contentID && v.SiteID == siteID
}
return false
}

View File

@@ -0,0 +1,52 @@
package api
import "time"
// API request/response models
type ContentItem struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
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"`
Value string `json:"value"`
Type string `json:"type"`
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"`
}
// Request models
type CreateContentRequest struct {
ID string `json:"id"`
SiteID string `json:"site_id,omitempty"`
Value string `json:"value"`
Type string `json:"type"`
CreatedBy string `json:"created_by,omitempty"`
}
type UpdateContentRequest struct {
Value string `json:"value"`
Type string `json:"type,omitempty"`
UpdatedBy string `json:"updated_by,omitempty"`
}
type RollbackContentRequest struct {
VersionID int64 `json:"version_id"`
RolledBackBy string `json:"rolled_back_by,omitempty"`
}

View File

@@ -0,0 +1,184 @@
package db
import (
"context"
"database/sql"
"fmt"
"strings"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/insertr/server/internal/db/postgresql"
"github.com/insertr/server/internal/db/sqlite"
)
// Database wraps the database connection and queries
type Database struct {
conn *sql.DB
dbType string
// Type-specific query interfaces
sqliteQueries *sqlite.Queries
postgresqlQueries *postgresql.Queries
}
// NewDatabase creates a new database connection
func NewDatabase(dbPath string) (*Database, error) {
var conn *sql.DB
var dbType string
var err error
// Determine database type from connection string
if strings.Contains(dbPath, "postgres://") || strings.Contains(dbPath, "postgresql://") {
dbType = "postgresql"
conn, err = sql.Open("postgres", dbPath)
} else {
dbType = "sqlite3"
conn, err = sql.Open("sqlite3", dbPath)
}
if err != nil {
return nil, fmt.Errorf("failed to open database: %w", err)
}
// Test connection
if err := conn.Ping(); err != nil {
conn.Close()
return nil, fmt.Errorf("failed to ping database: %w", err)
}
// Initialize the appropriate queries
db := &Database{
conn: conn,
dbType: dbType,
}
switch dbType {
case "sqlite3":
// Initialize SQLite schema using generated functions
db.sqliteQueries = sqlite.New(conn)
if err := db.initializeSQLiteSchema(); err != nil {
conn.Close()
return nil, fmt.Errorf("failed to initialize SQLite schema: %w", err)
}
case "postgresql":
// Initialize PostgreSQL schema using generated functions
db.postgresqlQueries = postgresql.New(conn)
if err := db.initializePostgreSQLSchema(); err != nil {
conn.Close()
return nil, fmt.Errorf("failed to initialize PostgreSQL schema: %w", err)
}
default:
return nil, fmt.Errorf("unsupported database type: %s", dbType)
}
return db, nil
}
// Close closes the database connection
func (db *Database) Close() error {
return db.conn.Close()
}
// GetQueries returns the appropriate query interface
func (db *Database) GetSQLiteQueries() *sqlite.Queries {
return db.sqliteQueries
}
func (db *Database) GetPostgreSQLQueries() *postgresql.Queries {
return db.postgresqlQueries
}
// GetDBType returns the database type
func (db *Database) GetDBType() string {
return db.dbType
}
// initializeSQLiteSchema sets up the SQLite database schema
func (db *Database) initializeSQLiteSchema() error {
ctx := context.Background()
// Create tables
if err := db.sqliteQueries.InitializeSchema(ctx); err != nil {
return fmt.Errorf("failed to create content table: %w", err)
}
if err := db.sqliteQueries.InitializeVersionsTable(ctx); err != nil {
return fmt.Errorf("failed to create content_versions table: %w", err)
}
// Create indexes (manual for now since sqlc didn't generate them)
indexQueries := []string{
"CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);",
"CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);",
"CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);",
}
for _, query := range indexQueries {
if _, err := db.conn.Exec(query); err != nil {
return fmt.Errorf("failed to create index: %w", err)
}
}
// Create update trigger (manual for now)
triggerQuery := `
CREATE TRIGGER IF NOT EXISTS update_content_updated_at
AFTER UPDATE ON content
FOR EACH ROW
BEGIN
UPDATE content SET updated_at = strftime('%s', 'now') WHERE id = NEW.id AND site_id = NEW.site_id;
END;`
if _, err := db.conn.Exec(triggerQuery); err != nil {
return fmt.Errorf("failed to create update trigger: %w", err)
}
return nil
}
// initializePostgreSQLSchema sets up the PostgreSQL database schema
func (db *Database) initializePostgreSQLSchema() error {
ctx := context.Background()
// Create tables
if err := db.postgresqlQueries.InitializeSchema(ctx); err != nil {
return fmt.Errorf("failed to create content table: %w", err)
}
if err := db.postgresqlQueries.InitializeVersionsTable(ctx); err != nil {
return fmt.Errorf("failed to create content_versions table: %w", err)
}
// Create indexes using generated functions
if err := db.postgresqlQueries.CreateContentSiteIndex(ctx); err != nil {
return fmt.Errorf("failed to create content site index: %w", err)
}
if err := db.postgresqlQueries.CreateContentUpdatedAtIndex(ctx); err != nil {
return fmt.Errorf("failed to create content updated_at index: %w", err)
}
if err := db.postgresqlQueries.CreateVersionsLookupIndex(ctx); err != nil {
return fmt.Errorf("failed to create versions lookup index: %w", err)
}
// Create update function and trigger
if err := db.postgresqlQueries.CreateUpdateFunction(ctx); err != nil {
return fmt.Errorf("failed to create update function: %w", err)
}
// Create trigger manually (sqlc didn't generate this)
triggerQuery := `
DROP TRIGGER IF EXISTS update_content_updated_at ON content;
CREATE TRIGGER update_content_updated_at
BEFORE UPDATE ON content
FOR EACH ROW
EXECUTE FUNCTION update_content_timestamp();`
if _, err := db.conn.Exec(triggerQuery); err != nil {
return fmt.Errorf("failed to create update trigger: %w", err)
}
return nil
}

View File

@@ -0,0 +1,214 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: content.sql
package postgresql
import (
"context"
"strings"
)
const createContent = `-- name: CreateContent :one
INSERT INTO content (id, site_id, value, type, last_edited_by)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by
`
type CreateContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
LastEditedBy string `json:"last_edited_by"`
}
func (q *Queries) CreateContent(ctx context.Context, arg CreateContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, createContent,
arg.ID,
arg.SiteID,
arg.Value,
arg.Type,
arg.LastEditedBy,
)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}
const deleteContent = `-- name: DeleteContent :exec
DELETE FROM content
WHERE id = $1 AND site_id = $2
`
type DeleteContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) DeleteContent(ctx context.Context, arg DeleteContentParams) error {
_, err := q.db.ExecContext(ctx, deleteContent, arg.ID, arg.SiteID)
return err
}
const getAllContent = `-- name: GetAllContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = $1
ORDER BY updated_at DESC
`
func (q *Queries) GetAllContent(ctx context.Context, siteID string) ([]Content, error) {
rows, err := q.db.QueryContext(ctx, getAllContent, siteID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Content
for rows.Next() {
var i Content
if err := rows.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getBulkContent = `-- name: GetBulkContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = $1 AND id IN ($2)
`
type GetBulkContentParams struct {
SiteID string `json:"site_id"`
Ids []string `json:"ids"`
}
func (q *Queries) GetBulkContent(ctx context.Context, arg GetBulkContentParams) ([]Content, error) {
query := getBulkContent
var queryParams []interface{}
queryParams = append(queryParams, arg.SiteID)
if len(arg.Ids) > 0 {
for _, v := range arg.Ids {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:ids*/?", strings.Repeat(",?", len(arg.Ids))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Content
for rows.Next() {
var i Content
if err := rows.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getContent = `-- name: GetContent :one
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE id = $1 AND site_id = $2
`
type GetContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) GetContent(ctx context.Context, arg GetContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, getContent, arg.ID, arg.SiteID)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}
const updateContent = `-- name: UpdateContent :one
UPDATE content
SET value = $1, type = $2, last_edited_by = $3
WHERE id = $4 AND site_id = $5
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by
`
type UpdateContentParams struct {
Value string `json:"value"`
Type string `json:"type"`
LastEditedBy string `json:"last_edited_by"`
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) UpdateContent(ctx context.Context, arg UpdateContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, updateContent,
arg.Value,
arg.Type,
arg.LastEditedBy,
arg.ID,
arg.SiteID,
)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}

View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package postgresql
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@@ -0,0 +1,25 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package postgresql
type Content struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
LastEditedBy string `json:"last_edited_by"`
}
type ContentVersion struct {
VersionID int32 `json:"version_id"`
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
CreatedBy string `json:"created_by"`
}

View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package postgresql
import (
"context"
)
type Querier interface {
CreateContent(ctx context.Context, arg CreateContentParams) (Content, error)
CreateContentSiteIndex(ctx context.Context) error
CreateContentUpdatedAtIndex(ctx context.Context) error
CreateContentVersion(ctx context.Context, arg CreateContentVersionParams) error
CreateUpdateFunction(ctx context.Context) error
CreateVersionsLookupIndex(ctx context.Context) error
DeleteContent(ctx context.Context, arg DeleteContentParams) error
DeleteOldVersions(ctx context.Context, arg DeleteOldVersionsParams) error
GetAllContent(ctx context.Context, siteID string) ([]Content, error)
GetAllVersionsForSite(ctx context.Context, arg GetAllVersionsForSiteParams) ([]GetAllVersionsForSiteRow, error)
GetBulkContent(ctx context.Context, arg GetBulkContentParams) ([]Content, error)
GetContent(ctx context.Context, arg GetContentParams) (Content, error)
GetContentVersion(ctx context.Context, versionID int32) (ContentVersion, error)
GetContentVersionHistory(ctx context.Context, arg GetContentVersionHistoryParams) ([]ContentVersion, error)
InitializeSchema(ctx context.Context) error
InitializeVersionsTable(ctx context.Context) error
UpdateContent(ctx context.Context, arg UpdateContentParams) (Content, error)
}
var _ Querier = (*Queries)(nil)

View File

@@ -0,0 +1,87 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: setup.sql
package postgresql
import (
"context"
)
const createContentSiteIndex = `-- name: CreateContentSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id)
`
func (q *Queries) CreateContentSiteIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createContentSiteIndex)
return err
}
const createContentUpdatedAtIndex = `-- name: CreateContentUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at)
`
func (q *Queries) CreateContentUpdatedAtIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createContentUpdatedAtIndex)
return err
}
const createUpdateFunction = `-- name: CreateUpdateFunction :exec
CREATE OR REPLACE FUNCTION update_content_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = EXTRACT(EPOCH FROM NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql
`
func (q *Queries) CreateUpdateFunction(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createUpdateFunction)
return err
}
const createVersionsLookupIndex = `-- name: CreateVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC)
`
func (q *Queries) CreateVersionsLookupIndex(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, createVersionsLookupIndex)
return err
}
const initializeSchema = `-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
)
`
func (q *Queries) InitializeSchema(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeSchema)
return err
}
const initializeVersionsTable = `-- name: InitializeVersionsTable :exec
CREATE TABLE IF NOT EXISTS content_versions (
version_id SERIAL PRIMARY KEY,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
)
`
func (q *Queries) InitializeVersionsTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeVersionsTable)
return err
}

View File

@@ -0,0 +1,175 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: versions.sql
package postgresql
import (
"context"
"database/sql"
)
const createContentVersion = `-- name: CreateContentVersion :exec
INSERT INTO content_versions (content_id, site_id, value, type, created_by)
VALUES ($1, $2, $3, $4, $5)
`
type CreateContentVersionParams struct {
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedBy string `json:"created_by"`
}
func (q *Queries) CreateContentVersion(ctx context.Context, arg CreateContentVersionParams) error {
_, err := q.db.ExecContext(ctx, createContentVersion,
arg.ContentID,
arg.SiteID,
arg.Value,
arg.Type,
arg.CreatedBy,
)
return err
}
const deleteOldVersions = `-- name: DeleteOldVersions :exec
DELETE FROM content_versions
WHERE created_at < $1 AND site_id = $2
`
type DeleteOldVersionsParams struct {
CreatedBefore int64 `json:"created_before"`
SiteID string `json:"site_id"`
}
func (q *Queries) DeleteOldVersions(ctx context.Context, arg DeleteOldVersionsParams) error {
_, err := q.db.ExecContext(ctx, deleteOldVersions, arg.CreatedBefore, arg.SiteID)
return err
}
const getAllVersionsForSite = `-- name: GetAllVersionsForSite :many
SELECT
cv.version_id, cv.content_id, cv.site_id, cv.value, cv.type, cv.created_at, cv.created_by,
c.value as current_value
FROM content_versions cv
LEFT JOIN content c ON cv.content_id = c.id AND cv.site_id = c.site_id
WHERE cv.site_id = $1
ORDER BY cv.created_at DESC
LIMIT $2
`
type GetAllVersionsForSiteParams struct {
SiteID string `json:"site_id"`
LimitCount int32 `json:"limit_count"`
}
type GetAllVersionsForSiteRow struct {
VersionID int32 `json:"version_id"`
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
CreatedBy string `json:"created_by"`
CurrentValue sql.NullString `json:"current_value"`
}
func (q *Queries) GetAllVersionsForSite(ctx context.Context, arg GetAllVersionsForSiteParams) ([]GetAllVersionsForSiteRow, error) {
rows, err := q.db.QueryContext(ctx, getAllVersionsForSite, arg.SiteID, arg.LimitCount)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllVersionsForSiteRow
for rows.Next() {
var i GetAllVersionsForSiteRow
if err := rows.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
&i.CurrentValue,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getContentVersion = `-- name: GetContentVersion :one
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE version_id = $1
`
func (q *Queries) GetContentVersion(ctx context.Context, versionID int32) (ContentVersion, error) {
row := q.db.QueryRowContext(ctx, getContentVersion, versionID)
var i ContentVersion
err := row.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
)
return i, err
}
const getContentVersionHistory = `-- name: GetContentVersionHistory :many
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE content_id = $1 AND site_id = $2
ORDER BY created_at DESC
LIMIT $3
`
type GetContentVersionHistoryParams struct {
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
LimitCount int32 `json:"limit_count"`
}
func (q *Queries) GetContentVersionHistory(ctx context.Context, arg GetContentVersionHistoryParams) ([]ContentVersion, error) {
rows, err := q.db.QueryContext(ctx, getContentVersionHistory, arg.ContentID, arg.SiteID, arg.LimitCount)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ContentVersion
for rows.Next() {
var i ContentVersion
if err := rows.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}

View File

@@ -1,232 +0,0 @@
package db
import (
"database/sql"
"fmt"
"time"
"github.com/insertr/server/internal/models"
_ "github.com/mattn/go-sqlite3"
)
// SQLiteDB wraps a SQLite database connection
type SQLiteDB struct {
db *sql.DB
}
// NewSQLiteDB creates a new SQLite database connection
func NewSQLiteDB(dbPath string) (*SQLiteDB, error) {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, fmt.Errorf("opening database: %w", err)
}
// Test connection
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("connecting to database: %w", err)
}
sqliteDB := &SQLiteDB{db: db}
// Initialize schema
if err := sqliteDB.initSchema(); err != nil {
return nil, fmt.Errorf("initializing schema: %w", err)
}
return sqliteDB, nil
}
// Close closes the database connection
func (s *SQLiteDB) Close() error {
return s.db.Close()
}
// initSchema creates the necessary tables
func (s *SQLiteDB) initSchema() error {
schema := `
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id, site_id)
);
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);
-- Trigger to update updated_at timestamp
CREATE TRIGGER IF NOT EXISTS update_content_updated_at
AFTER UPDATE ON content
FOR EACH ROW
BEGIN
UPDATE content SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id AND site_id = NEW.site_id;
END;
`
if _, err := s.db.Exec(schema); err != nil {
return fmt.Errorf("creating schema: %w", err)
}
return nil
}
// GetContent fetches a single content item by ID and site ID
func (s *SQLiteDB) GetContent(siteID, contentID string) (*models.ContentItem, error) {
query := `
SELECT id, site_id, value, type, created_at, updated_at
FROM content
WHERE id = ? AND site_id = ?
`
var item models.ContentItem
err := s.db.QueryRow(query, contentID, siteID).Scan(
&item.ID, &item.SiteID, &item.Value, &item.Type, &item.CreatedAt, &item.UpdatedAt,
)
if err == sql.ErrNoRows {
return nil, nil // Content not found
}
if err != nil {
return nil, fmt.Errorf("querying content: %w", err)
}
return &item, nil
}
// GetAllContent fetches all content for a site
func (s *SQLiteDB) GetAllContent(siteID string) ([]models.ContentItem, error) {
query := `
SELECT id, site_id, value, type, created_at, updated_at
FROM content
WHERE site_id = ?
ORDER BY updated_at DESC
`
rows, err := s.db.Query(query, siteID)
if err != nil {
return nil, fmt.Errorf("querying all content: %w", err)
}
defer rows.Close()
var items []models.ContentItem
for rows.Next() {
var item models.ContentItem
err := rows.Scan(&item.ID, &item.SiteID, &item.Value, &item.Type, &item.CreatedAt, &item.UpdatedAt)
if err != nil {
return nil, fmt.Errorf("scanning content row: %w", err)
}
items = append(items, item)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("iterating content rows: %w", err)
}
return items, nil
}
// GetBulkContent fetches multiple content items by IDs
func (s *SQLiteDB) GetBulkContent(siteID string, contentIDs []string) ([]models.ContentItem, error) {
if len(contentIDs) == 0 {
return []models.ContentItem{}, nil
}
// Build placeholders for IN clause
placeholders := make([]interface{}, len(contentIDs)+1)
placeholders[0] = siteID
for i, id := range contentIDs {
placeholders[i+1] = id
}
// Build query with proper number of placeholders
query := fmt.Sprintf(`
SELECT id, site_id, value, type, created_at, updated_at
FROM content
WHERE site_id = ? AND id IN (%s)
ORDER BY updated_at DESC
`, buildPlaceholders(len(contentIDs)))
rows, err := s.db.Query(query, placeholders...)
if err != nil {
return nil, fmt.Errorf("querying bulk content: %w", err)
}
defer rows.Close()
var items []models.ContentItem
for rows.Next() {
var item models.ContentItem
err := rows.Scan(&item.ID, &item.SiteID, &item.Value, &item.Type, &item.CreatedAt, &item.UpdatedAt)
if err != nil {
return nil, fmt.Errorf("scanning bulk content row: %w", err)
}
items = append(items, item)
}
return items, nil
}
// CreateContent creates a new content item
func (s *SQLiteDB) CreateContent(siteID, contentID, value, contentType string) (*models.ContentItem, error) {
now := time.Now()
query := `
INSERT INTO content (id, site_id, value, type, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`
_, err := s.db.Exec(query, contentID, siteID, value, contentType, now, now)
if err != nil {
return nil, fmt.Errorf("creating content: %w", err)
}
return &models.ContentItem{
ID: contentID,
SiteID: siteID,
Value: value,
Type: contentType,
CreatedAt: now,
UpdatedAt: now,
}, nil
}
// UpdateContent updates an existing content item
func (s *SQLiteDB) UpdateContent(siteID, contentID, value string) (*models.ContentItem, error) {
// First check if content exists
existing, err := s.GetContent(siteID, contentID)
if err != nil {
return nil, fmt.Errorf("checking existing content: %w", err)
}
if existing == nil {
return nil, fmt.Errorf("content not found: %s", contentID)
}
query := `
UPDATE content
SET value = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND site_id = ?
`
_, err = s.db.Exec(query, value, contentID, siteID)
if err != nil {
return nil, fmt.Errorf("updating content: %w", err)
}
// Fetch and return updated content
return s.GetContent(siteID, contentID)
}
// buildPlaceholders creates a string of SQL placeholders like "?,?,?"
func buildPlaceholders(count int) string {
if count == 0 {
return ""
}
result := "?"
for i := 1; i < count; i++ {
result += ",?"
}
return result
}

View File

@@ -0,0 +1,214 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: content.sql
package sqlite
import (
"context"
"strings"
)
const createContent = `-- name: CreateContent :one
INSERT INTO content (id, site_id, value, type, last_edited_by)
VALUES (?1, ?2, ?3, ?4, ?5)
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by
`
type CreateContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
LastEditedBy string `json:"last_edited_by"`
}
func (q *Queries) CreateContent(ctx context.Context, arg CreateContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, createContent,
arg.ID,
arg.SiteID,
arg.Value,
arg.Type,
arg.LastEditedBy,
)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}
const deleteContent = `-- name: DeleteContent :exec
DELETE FROM content
WHERE id = ?1 AND site_id = ?2
`
type DeleteContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) DeleteContent(ctx context.Context, arg DeleteContentParams) error {
_, err := q.db.ExecContext(ctx, deleteContent, arg.ID, arg.SiteID)
return err
}
const getAllContent = `-- name: GetAllContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = ?1
ORDER BY updated_at DESC
`
func (q *Queries) GetAllContent(ctx context.Context, siteID string) ([]Content, error) {
rows, err := q.db.QueryContext(ctx, getAllContent, siteID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Content
for rows.Next() {
var i Content
if err := rows.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getBulkContent = `-- name: GetBulkContent :many
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE site_id = ?1 AND id IN (/*SLICE:ids*/?)
`
type GetBulkContentParams struct {
SiteID string `json:"site_id"`
Ids []string `json:"ids"`
}
func (q *Queries) GetBulkContent(ctx context.Context, arg GetBulkContentParams) ([]Content, error) {
query := getBulkContent
var queryParams []interface{}
queryParams = append(queryParams, arg.SiteID)
if len(arg.Ids) > 0 {
for _, v := range arg.Ids {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:ids*/?", strings.Repeat(",?", len(arg.Ids))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:ids*/?", "NULL", 1)
}
rows, err := q.db.QueryContext(ctx, query, queryParams...)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Content
for rows.Next() {
var i Content
if err := rows.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getContent = `-- name: GetContent :one
SELECT id, site_id, value, type, created_at, updated_at, last_edited_by
FROM content
WHERE id = ?1 AND site_id = ?2
`
type GetContentParams struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) GetContent(ctx context.Context, arg GetContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, getContent, arg.ID, arg.SiteID)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}
const updateContent = `-- name: UpdateContent :one
UPDATE content
SET value = ?1, type = ?2, last_edited_by = ?3
WHERE id = ?4 AND site_id = ?5
RETURNING id, site_id, value, type, created_at, updated_at, last_edited_by
`
type UpdateContentParams struct {
Value string `json:"value"`
Type string `json:"type"`
LastEditedBy string `json:"last_edited_by"`
ID string `json:"id"`
SiteID string `json:"site_id"`
}
func (q *Queries) UpdateContent(ctx context.Context, arg UpdateContentParams) (Content, error) {
row := q.db.QueryRowContext(ctx, updateContent,
arg.Value,
arg.Type,
arg.LastEditedBy,
arg.ID,
arg.SiteID,
)
var i Content
err := row.Scan(
&i.ID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
&i.LastEditedBy,
)
return i, err
}

View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package sqlite
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@@ -0,0 +1,25 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package sqlite
type Content struct {
ID string `json:"id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `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"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
CreatedBy string `json:"created_by"`
}

View File

@@ -0,0 +1,27 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
package sqlite
import (
"context"
)
type Querier interface {
CreateContent(ctx context.Context, arg CreateContentParams) (Content, error)
CreateContentVersion(ctx context.Context, arg CreateContentVersionParams) error
DeleteContent(ctx context.Context, arg DeleteContentParams) error
DeleteOldVersions(ctx context.Context, arg DeleteOldVersionsParams) error
GetAllContent(ctx context.Context, siteID string) ([]Content, error)
GetAllVersionsForSite(ctx context.Context, arg GetAllVersionsForSiteParams) ([]GetAllVersionsForSiteRow, error)
GetBulkContent(ctx context.Context, arg GetBulkContentParams) ([]Content, error)
GetContent(ctx context.Context, arg GetContentParams) (Content, error)
GetContentVersion(ctx context.Context, versionID int64) (ContentVersion, error)
GetContentVersionHistory(ctx context.Context, arg GetContentVersionHistoryParams) ([]ContentVersion, error)
InitializeSchema(ctx context.Context) error
InitializeVersionsTable(ctx context.Context) error
UpdateContent(ctx context.Context, arg UpdateContentParams) (Content, error)
}
var _ Querier = (*Queries)(nil)

View File

@@ -0,0 +1,45 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: setup.sql
package sqlite
import (
"context"
)
const initializeSchema = `-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('text', 'markdown', 'link')),
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
updated_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
last_edited_by TEXT DEFAULT 'system' NOT NULL,
PRIMARY KEY (id, site_id)
)
`
func (q *Queries) InitializeSchema(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeSchema)
return err
}
const initializeVersionsTable = `-- name: InitializeVersionsTable :exec
CREATE TABLE IF NOT EXISTS content_versions (
version_id INTEGER PRIMARY KEY AUTOINCREMENT,
content_id TEXT NOT NULL,
site_id TEXT NOT NULL,
value TEXT NOT NULL,
type TEXT NOT NULL,
created_at INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL,
created_by TEXT DEFAULT 'system' NOT NULL
)
`
func (q *Queries) InitializeVersionsTable(ctx context.Context) error {
_, err := q.db.ExecContext(ctx, initializeVersionsTable)
return err
}

View File

@@ -0,0 +1,175 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: versions.sql
package sqlite
import (
"context"
"database/sql"
)
const createContentVersion = `-- name: CreateContentVersion :exec
INSERT INTO content_versions (content_id, site_id, value, type, created_by)
VALUES (?1, ?2, ?3, ?4, ?5)
`
type CreateContentVersionParams struct {
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedBy string `json:"created_by"`
}
func (q *Queries) CreateContentVersion(ctx context.Context, arg CreateContentVersionParams) error {
_, err := q.db.ExecContext(ctx, createContentVersion,
arg.ContentID,
arg.SiteID,
arg.Value,
arg.Type,
arg.CreatedBy,
)
return err
}
const deleteOldVersions = `-- name: DeleteOldVersions :exec
DELETE FROM content_versions
WHERE created_at < ?1 AND site_id = ?2
`
type DeleteOldVersionsParams struct {
CreatedBefore int64 `json:"created_before"`
SiteID string `json:"site_id"`
}
func (q *Queries) DeleteOldVersions(ctx context.Context, arg DeleteOldVersionsParams) error {
_, err := q.db.ExecContext(ctx, deleteOldVersions, arg.CreatedBefore, arg.SiteID)
return err
}
const getAllVersionsForSite = `-- name: GetAllVersionsForSite :many
SELECT
cv.version_id, cv.content_id, cv.site_id, cv.value, cv.type, cv.created_at, cv.created_by,
c.value as current_value
FROM content_versions cv
LEFT JOIN content c ON cv.content_id = c.id AND cv.site_id = c.site_id
WHERE cv.site_id = ?1
ORDER BY cv.created_at DESC
LIMIT ?2
`
type GetAllVersionsForSiteParams struct {
SiteID string `json:"site_id"`
LimitCount int64 `json:"limit_count"`
}
type GetAllVersionsForSiteRow struct {
VersionID int64 `json:"version_id"`
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
Value string `json:"value"`
Type string `json:"type"`
CreatedAt int64 `json:"created_at"`
CreatedBy string `json:"created_by"`
CurrentValue sql.NullString `json:"current_value"`
}
func (q *Queries) GetAllVersionsForSite(ctx context.Context, arg GetAllVersionsForSiteParams) ([]GetAllVersionsForSiteRow, error) {
rows, err := q.db.QueryContext(ctx, getAllVersionsForSite, arg.SiteID, arg.LimitCount)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAllVersionsForSiteRow
for rows.Next() {
var i GetAllVersionsForSiteRow
if err := rows.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
&i.CurrentValue,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getContentVersion = `-- name: GetContentVersion :one
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE version_id = ?1
`
func (q *Queries) GetContentVersion(ctx context.Context, versionID int64) (ContentVersion, error) {
row := q.db.QueryRowContext(ctx, getContentVersion, versionID)
var i ContentVersion
err := row.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
)
return i, err
}
const getContentVersionHistory = `-- name: GetContentVersionHistory :many
SELECT version_id, content_id, site_id, value, type, created_at, created_by
FROM content_versions
WHERE content_id = ?1 AND site_id = ?2
ORDER BY created_at DESC
LIMIT ?3
`
type GetContentVersionHistoryParams struct {
ContentID string `json:"content_id"`
SiteID string `json:"site_id"`
LimitCount int64 `json:"limit_count"`
}
func (q *Queries) GetContentVersionHistory(ctx context.Context, arg GetContentVersionHistoryParams) ([]ContentVersion, error) {
rows, err := q.db.QueryContext(ctx, getContentVersionHistory, arg.ContentID, arg.SiteID, arg.LimitCount)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ContentVersion
for rows.Next() {
var i ContentVersion
if err := rows.Scan(
&i.VersionID,
&i.ContentID,
&i.SiteID,
&i.Value,
&i.Type,
&i.CreatedAt,
&i.CreatedBy,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}

View File

@@ -1,34 +0,0 @@
package models
import (
"time"
)
// ContentItem represents a piece of content in the database
// This matches the structure used by the CLI client and JavaScript client
type ContentItem struct {
ID string `json:"id" db:"id"`
SiteID string `json:"site_id" db:"site_id"`
Value string `json:"value" db:"value"`
Type string `json:"type" db:"type"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// ContentResponse represents the API response structure for multiple items
type ContentResponse struct {
Content []ContentItem `json:"content"`
Error string `json:"error,omitempty"`
}
// CreateContentRequest represents the request structure for creating content
type CreateContentRequest struct {
ID string `json:"id" validate:"required"`
Value string `json:"value" validate:"required"`
Type string `json:"type" validate:"required,oneof=text markdown link"`
}
// UpdateContentRequest represents the request structure for updating content
type UpdateContentRequest struct {
Value string `json:"value" validate:"required"`
}

31
insertr-server/sqlc.yaml Normal file
View File

@@ -0,0 +1,31 @@
version: "2"
sql:
# SQLite configuration for development
- name: "sqlite"
engine: "sqlite"
queries: ["db/queries/", "db/sqlite/setup.sql"]
schema: "db/sqlite/schema.sql"
gen:
go:
package: "sqlite"
out: "internal/db/sqlite"
emit_json_tags: true
emit_prepared_queries: false
emit_interface: true
emit_exact_table_names: false
emit_pointers_for_null_types: false # All fields are NOT NULL now
# PostgreSQL configuration for production
- name: "postgresql"
engine: "postgresql"
queries: ["db/queries/", "db/postgresql/setup.sql"]
schema: "db/postgresql/schema.sql"
gen:
go:
package: "postgresql"
out: "internal/db/postgresql"
emit_json_tags: true
emit_prepared_queries: false
emit_interface: true
emit_exact_table_names: false
emit_pointers_for_null_types: false # All fields are NOT NULL now