77 lines
2.0 KiB
Go
77 lines
2.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"doormile/db"
|
|
"doormile/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func GetBookingFromCache(c *fiber.Ctx) error {
|
|
bookingID := c.Params("booking_id")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
data, err := db.Rdb.HGetAll(ctx, fmt.Sprintf("bookings:%s", bookingID)).Result()
|
|
if err != nil {
|
|
return utils.Internal(c, "failed to fetch booking from cache")
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
return utils.NotFound(c, "booking not found in cache")
|
|
}
|
|
|
|
return utils.OK(c, data)
|
|
}
|
|
|
|
func GetCustomerBookingsFromCache(c *fiber.Ctx) error {
|
|
customerID := c.Params("customer_id")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
ids, err := db.Rdb.SMembers(ctx, fmt.Sprintf("bookings:customer:%s", customerID)).Result()
|
|
if err != nil {
|
|
return utils.Internal(c, "failed to fetch customer booking IDs from cache")
|
|
}
|
|
|
|
bookings := make([]map[string]string, 0, len(ids))
|
|
for _, bid := range ids {
|
|
ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second)
|
|
bdata, berr := db.Rdb.HGetAll(ctx2, fmt.Sprintf("bookings:%s", bid)).Result()
|
|
cancel2()
|
|
if berr == nil && len(bdata) > 0 {
|
|
bookings = append(bookings, bdata)
|
|
}
|
|
}
|
|
|
|
return utils.List(c, bookings, int64(len(bookings)))
|
|
}
|
|
|
|
func ListAllBookingsFromCache(c *fiber.Ctx) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
ids, err := db.Rdb.SMembers(ctx, "bookings:all").Result()
|
|
if err != nil {
|
|
return utils.Internal(c, "failed to fetch booking IDs from cache")
|
|
}
|
|
|
|
bookings := make([]map[string]string, 0, len(ids))
|
|
for _, bid := range ids {
|
|
ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Second)
|
|
bdata, berr := db.Rdb.HGetAll(ctx2, fmt.Sprintf("bookings:%s", bid)).Result()
|
|
cancel2()
|
|
if berr == nil && len(bdata) > 0 {
|
|
bookings = append(bookings, bdata)
|
|
}
|
|
}
|
|
|
|
return utils.List(c, bookings, int64(len(bookings)))
|
|
}
|