Skip to content

Commit 3370928

Browse files
committed
all handlers and routes
Signed-off-by: Rajdeep Mandal <mrajdeep651@gmail.com>
1 parent eb17f62 commit 3370928

File tree

4 files changed

+297
-0
lines changed

4 files changed

+297
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package handlers
2+
3+
import (
4+
"strconv"
5+
"github.com/gofiber/fiber/v2"
6+
"github.com/google/uuid"
7+
"your-project/internal/services"
8+
)
9+
10+
type AnalyticsHandler struct {
11+
service *services.AnalyticsService
12+
}
13+
14+
func NewAnalyticsHandler(service *services.AnalyticsService) *AnalyticsHandler {
15+
return &AnalyticsHandler{service: service}
16+
}
17+
18+
func (h *AnalyticsHandler) GetActivityLog(c *fiber.Ctx) error {
19+
limit := int64(10)
20+
if l := c.Query("limit"); l != "" {
21+
if parsed, err := strconv.ParseInt(l, 10, 64); err == nil {
22+
limit = parsed
23+
}
24+
}
25+
26+
activities, err := h.service.GetActivityLog(c.Context(), limit)
27+
if err != nil {
28+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
29+
"error": err.Error(),
30+
})
31+
}
32+
33+
return c.JSON(fiber.Map{
34+
"activities": activities,
35+
})
36+
}
37+
38+
func (h *AnalyticsHandler) GetVisitors(c *fiber.Ctx) error {
39+
idStr := c.Params("id")
40+
productID, err := uuid.Parse(idStr)
41+
if err != nil {
42+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
43+
"error": "Invalid product ID",
44+
})
45+
}
46+
47+
// Track this visitor
48+
visitorID := c.Get("X-Visitor-ID", c.IP())
49+
h.service.TrackVisitor(c.Context(), productID.String(), visitorID)
50+
51+
count, err := h.service.GetVisitorCount(c.Context(), productID.String())
52+
if err != nil {
53+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
54+
"error": err.Error(),
55+
})
56+
}
57+
58+
return c.JSON(fiber.Map{
59+
"unique_visitors": count,
60+
})
61+
}
62+
63+
func (h *AnalyticsHandler) GetLeaderboard(c *fiber.Ctx) error {
64+
limit := int64(10)
65+
if l := c.Query("limit"); l != "" {
66+
if parsed, err := strconv.ParseInt(l, 10, 64); err == nil {
67+
limit = parsed
68+
}
69+
}
70+
71+
leaderboard, err := h.service.GetLeaderboard(c.Context(), limit)
72+
if err != nil {
73+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
74+
"error": err.Error(),
75+
})
76+
}
77+
78+
return c.JSON(leaderboard)
79+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package handlers
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/google/uuid"
6+
"your-project/internal/services"
7+
)
8+
9+
type RatingHandler struct {
10+
service *services.RatingService
11+
}
12+
13+
func NewRatingHandler(service *services.RatingService) *RatingHandler {
14+
return &RatingHandler{service: service}
15+
}
16+
17+
func (h *RatingHandler) Create(c *fiber.Ctx) error {
18+
idStr := c.Params("id")
19+
productID, err := uuid.Parse(idStr)
20+
if err != nil {
21+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
22+
"error": "Invalid product ID",
23+
})
24+
}
25+
26+
var request struct {
27+
Score float64 `json:"score" validate:"required,min=1,max=5"`
28+
}
29+
if err := c.BodyParser(&request); err != nil {
30+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
31+
"error": err.Error(),
32+
})
33+
}
34+
35+
if err := h.service.CreateRating(productID, request.Score); err != nil {
36+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
37+
"error": err.Error(),
38+
})
39+
}
40+
41+
return c.JSON(fiber.Map{
42+
"message": "Rating added successfully",
43+
})
44+
}
45+
46+
func (h *RatingHandler) GetByProduct(c *fiber.Ctx) error {
47+
idStr := c.Params("id")
48+
productID, err := uuid.Parse(idStr)
49+
if err != nil {
50+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
51+
"error": "Invalid product ID",
52+
})
53+
}
54+
55+
ratings, err := h.service.GetRatingsByProduct(productID)
56+
if err != nil {
57+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
58+
"error": err.Error(),
59+
})
60+
}
61+
62+
summary, err := h.service.GetRatingSummary(productID)
63+
if err != nil {
64+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
65+
"error": err.Error(),
66+
})
67+
}
68+
69+
return c.JSON(fiber.Map{
70+
"ratings": ratings,
71+
"summary": summary,
72+
})
73+
}

go-fiber/internal/handlers/tag.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package handlers
2+
3+
import (
4+
"github.com/gofiber/fiber/v2"
5+
"github.com/google/uuid"
6+
"your-project/internal/services"
7+
)
8+
9+
type TagHandler struct {
10+
service *services.TagService
11+
}
12+
13+
func NewTagHandler(service *services.TagService) *TagHandler {
14+
return &TagHandler{service: service}
15+
}
16+
17+
func (h *TagHandler) AddTags(c *fiber.Ctx) error {
18+
idStr := c.Params("id")
19+
productID, err := uuid.Parse(idStr)
20+
if err != nil {
21+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
22+
"error": "Invalid product ID",
23+
})
24+
}
25+
26+
var request struct {
27+
Tags []string `json:"tags" validate:"required"`
28+
}
29+
if err := c.BodyParser(&request); err != nil {
30+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
31+
"error": err.Error(),
32+
})
33+
}
34+
35+
if err := h.service.AddTags(productID, request.Tags); err != nil {
36+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
37+
"error": err.Error(),
38+
})
39+
}
40+
41+
return c.JSON(fiber.Map{
42+
"message": "Tags added successfully",
43+
})
44+
}
45+
46+
func (h *TagHandler) GetByProduct(c *fiber.Ctx) error {
47+
idStr := c.Params("id")
48+
productID, err := uuid.Parse(idStr)
49+
if err != nil {
50+
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
51+
"error": "Invalid product ID",
52+
})
53+
}
54+
55+
tags, err := h.service.GetTagsByProduct(productID)
56+
if err != nil {
57+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
58+
"error": err.Error(),
59+
})
60+
}
61+
62+
return c.JSON(fiber.Map{
63+
"tags": tags,
64+
})
65+
}
66+
67+
func (h *TagHandler) GetProductsByTag(c *fiber.Ctx) error {
68+
tag := c.Params("tag")
69+
70+
products, err := h.service.GetProductsByTag(tag)
71+
if err != nil {
72+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
73+
"error": err.Error(),
74+
})
75+
}
76+
77+
return c.JSON(products)
78+
}

go-fiber/internal/routes/routes.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package routes
2+
3+
import (
4+
"database/sql"
5+
6+
"github.com/gofiber/fiber/v2"
7+
"github.com/redis/go-redis/v9"
8+
9+
"your-project/internal/handlers"
10+
"your-project/internal/repository"
11+
"your-project/internal/services"
12+
)
13+
14+
func Setup(app *fiber.App, db *sql.DB, rdb *redis.Client) {
15+
// Initialize repositories
16+
productRepo := repository.NewProductRepository(db)
17+
ratingRepo := repository.NewRatingRepository(db)
18+
tagRepo := repository.NewTagRepository(db)
19+
cartRepo := repository.NewCartRepository(rdb)
20+
21+
// Initialize services
22+
productService := services.NewProductService(productRepo)
23+
// Add other services here...
24+
25+
// Initialize handlers
26+
productHandler := handlers.NewProductHandler(productService)
27+
// Add other handlers here...
28+
29+
// API routes
30+
api := app.Group("/api/v1")
31+
32+
// Product routes
33+
products := api.Group("/products")
34+
products.Get("/", productHandler.List)
35+
products.Post("/", productHandler.Create)
36+
products.Get("/:id", productHandler.Get)
37+
products.Put("/:id", productHandler.Update)
38+
products.Delete("/:id", productHandler.Delete)
39+
products.Post("/bulk", productHandler.BulkCreate)
40+
41+
// Rating routes (to be implemented)
42+
// products.Post("/:id/rate", ratingHandler.Create)
43+
// products.Get("/:id/ratings", ratingHandler.GetByProduct)
44+
45+
// Tag routes (to be implemented)
46+
// products.Post("/:id/tags", tagHandler.AddTags)
47+
// products.Get("/:id/tags", tagHandler.GetByProduct)
48+
// api.Get("/tags/:tag/products", tagHandler.GetProductsByTag)
49+
50+
// Cart routes (to be implemented)
51+
// api.Post("/carts/:userId", cartHandler.Update)
52+
// api.Get("/carts/:userId", cartHandler.Get)
53+
54+
// Analytics routes (to be implemented)
55+
// api.Get("/activity", analyticsHandler.GetActivityLog)
56+
// api.Get("/products/:id/visitors", analyticsHandler.GetVisitors)
57+
// api.Get("/leaderboard", analyticsHandler.GetLeaderboard)
58+
59+
// Health check
60+
app.Get("/health", func(c *fiber.Ctx) error {
61+
return c.JSON(fiber.Map{
62+
"status": "ok",
63+
"postgres": "connected",
64+
"redis": "connected",
65+
})
66+
})
67+
}

0 commit comments

Comments
 (0)