get agents working

This commit is contained in:
SwiftyOS
2024-09-13 19:55:02 +02:00
parent 55eb917162
commit 286202cc66
7 changed files with 138 additions and 20 deletions

View File

@@ -19,8 +19,8 @@ func NewDB(cfg *config.Config) (*pgxpool.Pool, error) {
return pgxpool.New(context.Background(), cfg.DatabaseURL)
}
func GetAgents(ctx context.Context, db *pgxpool.Pool, page int, pageSize int, name *string, keywords *string, categories *string) ([]models.Agent, error) {
logger := zap.L().With(zap.String("function", "GetAgents"))
func GetAgents(ctx context.Context, db *pgxpool.Pool, logger *zap.Logger, page int, pageSize int, name *string, keywords *string, categories *string) ([]models.Agent, error) {
logger = logger.With(zap.String("function", "GetAgents")).With(zap.String("file", "db.go"))
logger.Debug("Query parameters",
zap.Int("page", page),
@@ -30,12 +30,12 @@ func GetAgents(ctx context.Context, db *pgxpool.Pool, page int, pageSize int, na
zap.String("categories", utils.StringOrNil(categories)))
query := `
SELECT * FROM "Agents"
WHERE submission_status = 'APPROVED'
SELECT "id", "name", "description", "author", "keywords", "categories", "graph" FROM "Agents"
WHERE "submissionStatus" = 'APPROVED'
AND ($3::text IS NULL OR name ILIKE '%' || $3 || '%')
AND ($4::text IS NULL OR $4 = ANY(keywords))
AND ($5::text IS NULL OR $5 = ANY(categories))
ORDER BY created_at DESC
ORDER BY "createdAt" DESC
LIMIT $1 OFFSET $2
`
@@ -70,6 +70,9 @@ func GetAgents(ctx context.Context, db *pgxpool.Pool, page int, pageSize int, na
}
logger.Info("Found agents", zap.Int("count", len(agents)))
if agents == nil {
agents = []models.Agent{}
}
return agents, err
}

View File

@@ -1,19 +1,19 @@
-- Sample data for Agents table (10 agents)
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('b609e5fd-c992-4be9-b68f-afc1980f93c0', 'AI Recruiter', 'An AI-powered tool that assists HR teams with talent acquisition, screening, and shortlisting.', 'Author1', ARRAY['recruitment', 'HR'], ARRAY['human resources', 'talent management'], '{"key": "value"}');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph", "submissionStatus")
VALUES ('b609e5fd-c992-4be9-b68f-afc1980f93c0', 'AI Recruiter', 'An AI-powered tool that assists HR teams with talent acquisition, screening, and shortlisting.', 'Author1', ARRAY['recruitment', 'HR'], ARRAY['human resources', 'talent management'], '{"key": "value"}', 'APPROVED');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('3b6d8f75-99d3-41e3-b484-4b2c5f835f5b', 'Customer Service Bot', 'A chatbot that provides 24/7 support and assistance to customers, handling common inquiries and issues.', 'Author2', ARRAY['customer service', 'chatbot'], ARRAY['customer experience', 'artificial intelligence'], '{"key": "value"}');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph", "submissionStatus")
VALUES ('3b6d8f75-99d3-41e3-b484-4b2c5f835f5b', 'Customer Service Bot', 'A chatbot that provides 24/7 support and assistance to customers, handling common inquiries and issues.', 'Author2', ARRAY['customer service', 'chatbot'], ARRAY['customer experience', 'artificial intelligence'], '{"key": "value"}', 'APPROVED');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('eaa773b1-5efa-485f-b2f0-2e05bae6d297', 'Financial Advisor', 'An AI-powered financial advisor that offers personalized investment recommendations and portfolio management.', 'Author3', ARRAY['finance', 'investment'], ARRAY['wealth management', 'artificial intelligence'], '{"key": "value"}');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph", "submissionStatus")
VALUES ('eaa773b1-5efa-485f-b2f0-2e05bae6d297', 'Financial Advisor', 'An AI-powered financial advisor that offers personalized investment recommendations and portfolio management.', 'Author3', ARRAY['finance', 'investment'], ARRAY['wealth management', 'artificial intelligence'], '{"key": "value"}', 'APPROVED');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('b47e40a7-ad5f-4b29-9eac-abd5b728f19a', 'AI Content Writer', 'An AI-powered tool that generates high-quality content for websites, blogs, and marketing materials.', 'Author4', ARRAY['content writing', 'AI'], ARRAY['marketing', 'artificial intelligence'], '{"key": "value"}');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph", "submissionStatus")
VALUES ('b47e40a7-ad5f-4b29-9eac-abd5b728f19a', 'AI Content Writer', 'An AI-powered tool that generates high-quality content for websites, blogs, and marketing materials.', 'Author4', ARRAY['content writing', 'AI'], ARRAY['marketing', 'artificial intelligence'], '{"key": "value"}', 'APPROVED');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('a4d3598f-6180-4e6d-96bf-6e15c3de05a9', 'AI Image Generator', 'An AI-powered tool that creates realistic images based on text prompts.', 'Author5', ARRAY['image generation', 'AI'], ARRAY['marketing', 'artificial intelligence'], '{"key": "value"}');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph", "submissionStatus")
VALUES ('a4d3598f-6180-4e6d-96bf-6e15c3de05a9', 'AI Image Generator', 'An AI-powered tool that creates realistic images based on text prompts.', 'Author5', ARRAY['image generation', 'AI'], ARRAY['marketing', 'artificial intelligence'], '{"key": "value"}', 'APPROVED');
INSERT INTO "Agents" ("id", "name", "description", "author", "keywords", "categories", "graph")
VALUES ('9f332ff3-4c74-4f5b-9838-65938a06711f', 'AI Video Editor', 'An AI-powered tool that edits and enhances videos with advanced AI algorithms.', 'Author6', ARRAY['video editing', 'AI'], ARRAY['marketing', 'artificial intelligence'], '{"key": "value"}');

View File

@@ -251,6 +251,46 @@ const docTemplate = `{
"Agents"
],
"summary": "Get Agents",
"parameters": [
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "pageSize",
"in": "query"
},
{
"type": "string",
"description": "Agent Name",
"name": "name",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Keywords",
"name": "keywords",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Categories",
"name": "categories",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",

View File

@@ -240,6 +240,46 @@
"Agents"
],
"summary": "Get Agents",
"parameters": [
{
"type": "integer",
"description": "Page number",
"name": "page",
"in": "query"
},
{
"type": "integer",
"description": "Page size",
"name": "pageSize",
"in": "query"
},
{
"type": "string",
"description": "Agent Name",
"name": "name",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Keywords",
"name": "keywords",
"in": "query"
},
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "csv",
"description": "Categories",
"name": "categories",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",

View File

@@ -197,6 +197,33 @@ paths:
consumes:
- application/json
description: Get Agents
parameters:
- description: Page number
in: query
name: page
type: integer
- description: Page size
in: query
name: pageSize
type: integer
- description: Agent Name
in: query
name: name
type: string
- collectionFormat: csv
description: Keywords
in: query
items:
type: string
name: keywords
type: array
- collectionFormat: csv
description: Categories
in: query
items:
type: string
name: categories
type: array
produces:
- application/json
responses:

View File

@@ -23,11 +23,18 @@ import (
// @Tags Agents
// @Accept json
// @Produce json
// @Param page query int false "Page number"
// @Param pageSize query int false "Page size"
// @Param name query string false "Agent Name"
// @Param keywords query []string false "Keywords"
// @Param categories query []string false "Categories"
// @Success 200 {array} models.Agent
// @Router /agents [get]
func GetAgents(db *pgxpool.Pool) gin.HandlerFunc {
func GetAgents(db *pgxpool.Pool, log_ctx *zap.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
logger := zap.L().With(zap.String("function", "ListAgents"))
logger := log_ctx.With(zap.String("function", "GetAgents")).With(zap.String("file", "handlers/agents.go"))
logger.Info("Get Agents Request Started")
// Get pagination parameters from context
page := getPageFromContext(c.Request.Context())
pageSize := getPageSizeFromContext(c.Request.Context())
@@ -44,9 +51,10 @@ func GetAgents(db *pgxpool.Pool) gin.HandlerFunc {
zap.String("keywords", utils.StringOrNil(keywords)),
zap.String("categories", utils.StringOrNil(categories)))
agents, err := database.GetAgents(c.Request.Context(), db, page, pageSize, name, keywords, categories)
agents, err := database.GetAgents(c.Request.Context(), db, log_ctx, page, pageSize, name, keywords, categories)
if err != nil {
logger.Error("Failed to fetch agents", zap.Error(err))
logger.Error("Database requested returned error!", zap.Error(err))
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch agents"})
return
}

View File

@@ -81,7 +81,7 @@ func main() {
agents := api.Group("/agents")
{
agents.GET("", handlers.GetAgents(db))
agents.GET("", handlers.GetAgents(db, logger))
agents.GET("/:agent_id", handlers.GetAgentDetails(db))
agents.GET("/:agent_id/download", handlers.DownloadAgent(db))
agents.GET("/:agent_id/download-file", handlers.DownloadAgentFile(db))