Skip to content

Commit f29d0b7

Browse files
committed
feat(handlers.go): enhance ApiV1Handler response with additional metadata and status information
refactor(handlers.go): remove unused context and MongoDB user/task handlers to simplify code chore(routes.go): remove unused purification calculation endpoint from routes setup
1 parent 8d7192c commit f29d0b7

File tree

2 files changed

+13
-61
lines changed

2 files changed

+13
-61
lines changed

internal/handlers/handlers.go

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package handlers
22

33
import (
4-
"context"
54
"os"
65
"time"
76

87
"github.com/gofiber/fiber/v2"
9-
"go.mongodb.org/mongo-driver/bson"
8+
"github.com/google/uuid"
109
"go.mongodb.org/mongo-driver/mongo"
1110
)
1211

@@ -34,14 +33,18 @@ func (h *Handler) RootHandler(c *fiber.Ctx) error {
3433

3534
// ApiV1Handler handles the "/api/v1" endpoint
3635
func (h *Handler) ApiV1Handler(c *fiber.Ctx) error {
37-
return c.JSON(fiber.Map{
38-
"message": "Welcome to API v1",
39-
"endpoints": []string{
40-
"/api/v1/health",
41-
"/api/v1/users",
42-
"/api/v1/tasks",
43-
},
44-
})
36+
return c.JSON(fiber.Map{
37+
"status": "active",
38+
"message": "Welcome to NAQA API v1",
39+
"version": "1.0.0",
40+
"env": os.Getenv("APP_ENV"),
41+
"server_time": time.Now().Format(time.RFC3339),
42+
"request_id": c.Get("X-Request-ID", uuid.New().String()),
43+
"endpoints": []string{
44+
"/api/v1/stocks",
45+
"/api/v1/health",
46+
},
47+
})
4548
}
4649

4750
// HealthCheckHandler handles the health check endpoint
@@ -58,51 +61,3 @@ func (h *Handler) HandleSomething(c *fiber.Ctx) error {
5861
"message": "Handler example",
5962
})
6063
}
61-
62-
// GetUsersHandler handles the /api/v1/users endpoint
63-
func (h *Handler) GetUsersHandler(c *fiber.Ctx) error {
64-
collection := h.db.Collection("users")
65-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
66-
defer cancel()
67-
68-
cursor, err := collection.Find(ctx, bson.M{})
69-
if err != nil {
70-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
71-
"error": "Error fetching users",
72-
})
73-
}
74-
defer cursor.Close(ctx)
75-
76-
var users []fiber.Map
77-
if err := cursor.All(ctx, &users); err != nil {
78-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
79-
"error": "Error parsing users data",
80-
})
81-
}
82-
83-
return c.JSON(users)
84-
}
85-
86-
// GetTasksHandler handles the /api/v1/tasks endpoint
87-
func (h *Handler) GetTasksHandler(c *fiber.Ctx) error {
88-
collection := h.db.Collection("tasks")
89-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
90-
defer cancel()
91-
92-
cursor, err := collection.Find(ctx, bson.M{})
93-
if err != nil {
94-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
95-
"error": "Error fetching tasks",
96-
})
97-
}
98-
defer cursor.Close(ctx)
99-
100-
var tasks []fiber.Map
101-
if err := cursor.All(ctx, &tasks); err != nil {
102-
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
103-
"error": "Error parsing tasks data",
104-
})
105-
}
106-
107-
return c.JSON(tasks)
108-
}

internal/routes/routes.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ func (r *Router) SetupRoutes() {
3030
v1 := r.app.Group("/api/v1")
3131
r.setupV1Routes(v1)
3232

33-
// Add purification calculation endpoint
34-
r.app.Post("/api/v1/stocks/calculate-purification", r.h.CalculatePurificationHandler)
35-
3633
// API routes
3734
api := r.app.Group("/api")
3835

0 commit comments

Comments
 (0)