perf: add pagination, filters, and fix Django sync pagination

This commit is contained in:
2026-04-30 15:34:25 -03:00
parent 19333cf713
commit badd54b4be
12 changed files with 413 additions and 176 deletions

View File

@@ -4,27 +4,97 @@ import (
"log"
"net/http"
"rrbec_server/internal/models"
"rrbec_server/internal/repository"
"rrbec_server/internal/service"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
type Handler struct {
svc *service.Service
svc *service.Service
syncer interface{ SyncProductsOnly() }
}
func NewHandler(svc *service.Service) *Handler {
return &Handler{svc: svc}
}
func (h *Handler) SetSyncer(syncer interface{ SyncProductsOnly() }) {
h.syncer = syncer
}
func parsePagination(c *gin.Context) *repository.PaginationParams {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
sort := c.DefaultQuery("sort", "id ASC")
params := &repository.PaginationParams{
Page: page,
Limit: limit,
Sort: sort,
Filters: make(map[string]interface{}),
}
if name := c.Query("name"); name != "" {
params.Filters["name"] = name
}
if active := c.Query("active"); active != "" {
if b, err := strconv.ParseBool(active); err == nil {
params.Filters["active"] = b
}
}
if status := c.Query("status"); status != "" {
params.Filters["status"] = status
}
if category := c.Query("category"); category != "" {
if c, err := strconv.ParseUint(category, 10, 64); err == nil {
params.Filters["category"] = uint(c)
}
}
if cuisine := c.Query("cuisine"); cuisine != "" {
if b, err := strconv.ParseBool(cuisine); err == nil {
params.Filters["cuisine"] = b
}
}
if dateFrom := c.Query("date_from"); dateFrom != "" {
if t, err := time.Parse("2006-01-02", dateFrom); err == nil {
params.Filters["date_from"] = t
}
}
if dateTo := c.Query("date_to"); dateTo != "" {
if t, err := time.Parse("2006-01-02", dateTo); err == nil {
params.Filters["date_to"] = t
}
}
if comandaID := c.Query("comanda_id"); comandaID != "" {
if c, err := strconv.ParseUint(comandaID, 10, 64); err == nil {
params.Filters["comanda_id"] = uint(c)
}
}
if canceled := c.Query("canceled"); canceled != "" {
if b, err := strconv.ParseBool(canceled); err == nil {
params.Filters["canceled"] = b
}
}
if delivered := c.Query("delivered"); delivered != "" {
if b, err := strconv.ParseBool(delivered); err == nil {
params.Filters["delivered"] = b
}
}
return params
}
func (h *Handler) GetProducts(c *gin.Context) {
products, err := h.svc.GetProducts()
params := parsePagination(c)
result, err := h.svc.GetProducts(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, products)
c.JSON(http.StatusOK, result)
}
func (h *Handler) CreateProduct(c *gin.Context) {
@@ -65,21 +135,23 @@ func (h *Handler) UpdateProduct(c *gin.Context) {
}
func (h *Handler) GetMesas(c *gin.Context) {
mesas, err := h.svc.GetMesas()
params := parsePagination(c)
result, err := h.svc.GetMesas(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, mesas)
c.JSON(http.StatusOK, result)
}
func (h *Handler) GetCategories(c *gin.Context) {
categories, err := h.svc.GetCategories()
params := parsePagination(c)
result, err := h.svc.GetCategories(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, categories)
c.JSON(http.StatusOK, result)
}
func (h *Handler) CreateCategory(c *gin.Context) {
@@ -120,30 +192,33 @@ func (h *Handler) UpdateCategory(c *gin.Context) {
}
func (h *Handler) GetTypePayments(c *gin.Context) {
types, err := h.svc.GetTypePayments()
params := parsePagination(c)
result, err := h.svc.GetTypePayments(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, types)
c.JSON(http.StatusOK, result)
}
func (h *Handler) GetClients(c *gin.Context) {
clients, err := h.svc.GetClients()
params := parsePagination(c)
result, err := h.svc.GetClients(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, clients)
c.JSON(http.StatusOK, result)
}
func (h *Handler) GetOrders(c *gin.Context) {
orders, err := h.svc.GetOrders()
params := parsePagination(c)
result, err := h.svc.GetOrders(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, orders)
c.JSON(http.StatusOK, result)
}
func (h *Handler) CreateOrder(c *gin.Context) {
@@ -244,12 +319,16 @@ func (h *Handler) SetOrderCanceled(c *gin.Context) {
}
func (h *Handler) GetPayments(c *gin.Context) {
payments, err := h.svc.GetPayments()
params := parsePagination(c)
if c.Query("sort") == "" {
params.Sort = "id DESC"
}
result, err := h.svc.GetPayments(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, payments)
c.JSON(http.StatusOK, result)
}
func (h *Handler) CreateComanda(c *gin.Context) {
@@ -400,12 +479,16 @@ func (h *Handler) PagarComanda(c *gin.Context) {
}
func (h *Handler) GetComandas(c *gin.Context) {
comandas, err := h.svc.GetComandas()
params := parsePagination(c)
if c.Query("sort") == "" {
params.Sort = "id DESC"
}
result, err := h.svc.GetComandas(params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, comandas)
c.JSON(http.StatusOK, result)
}
func (h *Handler) GetComandaByID(c *gin.Context) {
@@ -455,3 +538,12 @@ func (h *Handler) GetCurrentUser(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{"user_id": userID})
}
func (h *Handler) ResyncProducts(c *gin.Context) {
if h.syncer == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "syncer not configured"})
return
}
go h.syncer.SyncProductsOnly()
c.JSON(http.StatusOK, gin.H{"message": "Product resync started in background"})
}