fixing db queries

This commit is contained in:
SwiftyOS
2024-09-12 19:02:16 +02:00
parent 30e3d65711
commit 9e18c26e65
6 changed files with 32 additions and 32 deletions

View File

@@ -32,7 +32,7 @@ The project is organized into several packages:
4. Run database migrations:
```
migrate -source file://database/migrations -database "postgresql://user:password@localhost:5432/dbname?sslmode=disable" up
migrate -source file://database/migrations -database "postgresql://agpt_user:pass123@localhost:5432/apgt_marketplace?sslmode=disable" up
```
## Running the Application

View File

@@ -1,5 +1,5 @@
ServerAddress: ":8015"
DatabaseURL: "postgresql://agpt_user:pass123@localhost:5432/agpt_marketplace?connect_timeout=60"
DatabaseURL: "postgresql://agpt_user:pass123@localhost:5433/agpt_marketplace?connect_timeout=60"
JWTSecret: "Z86RsQ+nhSk+A8ODJX1kQA11JCk9nlw8n+MRdSgmR+P1sMPTTDG1rjBTwj7Ucjb3TRHSVxkCNPgXISmzU/vMkA=="
JWTAlgorithm: "HS256"
CORSAllowOrigins:

View File

@@ -30,7 +30,7 @@ func GetAgents(ctx context.Context, db *pgxpool.Pool, page int, pageSize int, na
zap.String("categories", utils.StringOrNil(categories)))
query := `
SELECT * FROM agents
SELECT * FROM "Agents"
WHERE submission_status = 'APPROVED'
AND ($3::text IS NULL OR name ILIKE '%' || $3 || '%')
AND ($4::text IS NULL OR $4 = ANY(keywords))
@@ -111,7 +111,7 @@ func SubmitAgent(ctx context.Context, db *pgxpool.Pool, request models.AddAgentR
// Insert the agent into the database
_, err = tx.Exec(ctx, `
INSERT INTO agents (id, name, description, author, keywords, categories, graph, version, created_at, updated_at, submission_date, submission_status)
INSERT INTO "Agents" (id, name, description, author, keywords, categories, graph, version, created_at, updated_at, submission_date, submission_status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
`, agentWithMetadata.ID, agentWithMetadata.Name, agentWithMetadata.Description, agentWithMetadata.Author,
agentWithMetadata.Keywords, agentWithMetadata.Categories, agentWithMetadata.Graph,
@@ -139,7 +139,7 @@ func GetAgentDetails(ctx context.Context, db *pgxpool.Pool, agentID string) (*mo
query := `
SELECT id, name, description, author, keywords, categories, graph, version, created_at, updated_at, submission_date, submission_status
FROM agents
FROM "Agents"
WHERE id = $1
`
@@ -176,7 +176,7 @@ func IncrementDownloadCount(ctx context.Context, db *pgxpool.Pool, agentID strin
logger := zap.L().With(zap.String("function", "IncrementDownloadCount"))
query := `
UPDATE agents
UPDATE "Agents"
SET download_count = download_count + 1
WHERE id = $1
`
@@ -196,7 +196,7 @@ func GetAgentFile(ctx context.Context, db *pgxpool.Pool, agentID string) (*model
query := `
SELECT id, name, graph
FROM agents
FROM "Agents"
WHERE id = $1
`
@@ -227,8 +227,8 @@ func GetTopAgentsByDownloads(ctx context.Context, db *pgxpool.Pool, page, pageSi
query := `
SELECT a.id, a.name, a.description, a.author, a.keywords, a.categories, a.graph, at.downloads
FROM agents a
JOIN analytics_tracker at ON a.id = at.agent_id
FROM "Agents" a
JOIN "AnalyticsTracker" at ON a.id = at.agent_id
WHERE a.submission_status = 'APPROVED'
ORDER BY at.downloads DESC
LIMIT $1 OFFSET $2
@@ -262,7 +262,7 @@ func GetTopAgentsByDownloads(ctx context.Context, db *pgxpool.Pool, page, pageSi
}
var totalCount int
err = db.QueryRow(ctx, "SELECT COUNT(*) FROM agents WHERE submission_status = 'APPROVED'").Scan(&totalCount)
err = db.QueryRow(ctx, `SELECT COUNT(*) FROM "Agents" WHERE submission_status = 'APPROVED'`).Scan(&totalCount)
if err != nil {
logger.Error("Failed to get total count", zap.Error(err))
return nil, 0, err
@@ -279,8 +279,8 @@ func GetFeaturedAgents(ctx context.Context, db *pgxpool.Pool, category string, p
query := `
SELECT a.id, a.name, a.description, a.author, a.keywords, a.categories, a.graph
FROM agents a
JOIN featured_agent fa ON a.id = fa.agent_id
FROM "Agents" a
JOIN "FeaturedAgent" fa ON a.id = fa.agent_id
WHERE $1 = ANY(fa.featured_categories) AND fa.is_active = true AND a.submission_status = 'APPROVED'
ORDER BY a.created_at DESC
LIMIT $2 OFFSET $3
@@ -313,7 +313,7 @@ func GetFeaturedAgents(ctx context.Context, db *pgxpool.Pool, category string, p
}
var totalCount int
err = db.QueryRow(ctx, "SELECT COUNT(*) FROM featured_agent fa JOIN agents a ON fa.agent_id = a.id WHERE $1 = ANY(fa.featured_categories) AND fa.is_active = true AND a.submission_status = 'APPROVED'", category).Scan(&totalCount)
err = db.QueryRow(ctx, `SELECT COUNT(*) FROM "FeaturedAgent" fa JOIN "Agents" a ON fa.agent_id = a.id WHERE $1 = ANY(fa.featured_categories) AND fa.is_active = true AND a.submission_status = 'APPROVED'`, category).Scan(&totalCount)
if err != nil {
logger.Error("Failed to get total count of featured agents", zap.Error(err))
return nil, 0, err
@@ -366,7 +366,7 @@ func Search(ctx context.Context, db *pgxpool.Pool, query string, categories []st
a.submission_status,
a.submission_date,
ts_rank(CAST(a.search AS tsvector), query.q) AS rank
FROM agents a, query
FROM "Agents" a, query
WHERE a.submission_status = 'APPROVED' %s
ORDER BY %s
LIMIT $2
@@ -478,7 +478,7 @@ func SetAgentFeatured(ctx context.Context, db *pgxpool.Pool, agentID string, isA
// Check if the agent exists
var exists bool
err := db.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM agents WHERE id = $1)", agentID).Scan(&exists)
err := db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM "Agents" WHERE id = $1)`, agentID).Scan(&exists)
if err != nil {
logger.Error("Failed to check if agent exists", zap.Error(err))
return nil, fmt.Errorf("failed to check if agent exists: %w", err)
@@ -493,7 +493,7 @@ func SetAgentFeatured(ctx context.Context, db *pgxpool.Pool, agentID string, isA
if isActive {
// Set the agent as featured
query = `
INSERT INTO featured_agent (agent_id, featured_categories, is_active)
INSERT INTO "FeaturedAgent" (agent_id, featured_categories, is_active)
VALUES ($1, $2, $3)
ON CONFLICT (agent_id) DO UPDATE
SET featured_categories = $2, is_active = $3
@@ -503,7 +503,7 @@ func SetAgentFeatured(ctx context.Context, db *pgxpool.Pool, agentID string, isA
} else {
// Unset the agent as featured
query = `
DELETE FROM featured_agent
DELETE FROM "FeaturedAgent"
WHERE agent_id = $1
RETURNING agent_id, featured_categories, is_active
`
@@ -540,7 +540,7 @@ func GetAgentFeatured(ctx context.Context, db *pgxpool.Pool, agentID string) (*m
query := `
SELECT agent_id, featured_categories, is_active
FROM featured_agent
FROM "FeaturedAgent"
WHERE agent_id = $1
`
@@ -569,7 +569,7 @@ func RemoveFeaturedCategory(ctx context.Context, db *pgxpool.Pool, agentID strin
logger.Info("Removing featured category", zap.String("agentID", agentID), zap.String("category", category))
query := `
UPDATE featured_agent
UPDATE "FeaturedAgent"
SET featured_categories = array_remove(featured_categories, $1)
WHERE agent_id = $2
RETURNING agent_id, featured_categories, is_active
@@ -603,8 +603,8 @@ func GetNotFeaturedAgents(ctx context.Context, db *pgxpool.Pool, page, pageSize
query := `
SELECT a.id, a.name, a.description, a.author, a.keywords, a.categories, a.graph
FROM agents a
LEFT JOIN featured_agent fa ON a.id = fa.agent_id
FROM "Agents" a
LEFT JOIN "FeaturedAgent" fa ON a.id = fa.agent_id
WHERE (fa.agent_id IS NULL OR fa.featured_categories = '{}')
AND a.submission_status = 'APPROVED'
ORDER BY a.created_at DESC
@@ -649,7 +649,7 @@ func GetAgentSubmissions(ctx context.Context, db *pgxpool.Pool, page, pageSize i
query := `
SELECT a.id, a.name, a.description, a.author, a.keywords, a.categories, a.graph, a.created_at, a.updated_at, a.version, a.submission_status, a.submission_review_date, a.submission_review_comments
FROM agents a
FROM "Agents" a
WHERE a.submission_status = 'PENDING'
`
@@ -714,7 +714,7 @@ func GetAgentSubmissions(ctx context.Context, db *pgxpool.Pool, page, pageSize i
}
// Get total count
countQuery := `SELECT COUNT(*) FROM agents WHERE submission_status = 'PENDING'`
countQuery := `SELECT COUNT(*) FROM "Agents" WHERE submission_status = 'PENDING'`
var totalCount int
err = db.QueryRow(ctx, countQuery).Scan(&totalCount)
if err != nil {
@@ -731,7 +731,7 @@ func ReviewSubmission(ctx context.Context, db *pgxpool.Pool, agentID string, ver
logger.Info("Reviewing agent submission", zap.String("agentID", agentID), zap.Int("version", version))
query := `
UPDATE agents
UPDATE "Agents"
SET submission_status = $1,
submission_review_date = NOW(),
submission_review_comments = $2
@@ -775,7 +775,7 @@ func GetAllCategories(ctx context.Context, db *pgxpool.Pool) ([]string, error) {
query := `
SELECT DISTINCT unnest(categories) AS category
FROM agents
FROM "Agents"
ORDER BY category
`

View File

@@ -4,7 +4,7 @@ services:
environment:
- POSTGRES_USER=agpt_user
- POSTGRES_PASSWORD=pass123
- POSTGRES_DB=agpt_local
- POSTGRES_DB=agpt_marketplace
healthcheck:
test: pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB
interval: 10s

View File

@@ -204,16 +204,16 @@ func TopAgentsByDownloads(db *pgxpool.Pool) gin.HandlerFunc {
}
}
func GetFeaturedAgents(db *pgxpool.Pool) gin.HandlerFunc {
func GetFeaturedAgents(db *pgxpool.Pool, logger *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "GetFeaturedAgents"))
logger := logger.With(zap.String("function", "GetFeaturedAgents"))
logger.Info("Handling request for featured agents")
category := c.Query("category")
if category == "" {
logger.Error("Category is required")
c.JSON(http.StatusBadRequest, gin.H{"error": "Category is required"})
return
logger.Debug("No category specified, fetching all featured agents")
} else {
logger.Debug("Fetching featured agents for category", zap.String("category", category))
}
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))

View File

@@ -83,7 +83,7 @@ func main() {
agents.GET("/:agent_id/download", handlers.DownloadAgent(db))
agents.GET("/:agent_id/download-file", handlers.DownloadAgentFile(db))
agents.GET("/top-downloads", handlers.TopAgentsByDownloads(db))
agents.GET("/featured", handlers.GetFeaturedAgents(db))
agents.GET("/featured", handlers.GetFeaturedAgents(db, logger))
agents.GET("/search", handlers.SearchAgents(db))
agents.POST("/submit", middleware.Auth(cfg), handlers.SubmitAgent(db))
}