busybar / internal / controllers / analytics.controller.go
analytics.controller.go
Raw
package controllers

import (
	"net/http"

	"github.com/danielrhuynh/busybar/internal/models"
	"github.com/danielrhuynh/busybar/internal/services"
	"github.com/labstack/echo/v4"
)

func GetBusiestDays(c echo.Context) error {
	busiestDays, err := services.GetBusiestDays()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, envelope{
			"error": "Failed to retrieve busiest days",
		})
	}

	return c.JSON(http.StatusOK, envelope{
		"busiest_days": busiestDays,
	})
}

func GetBusiestBar(c echo.Context) error {
	busiestBar, err := services.GetBusiestBar()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, envelope{
			"error": "Failed to retrieve busiest bar",
		})
	}

	if busiestBar == nil {
		return c.JSON(http.StatusOK, envelope{
			"message": "No bars have reports in the last 3 hours",
		})
	}

	return c.JSON(http.StatusOK, envelope{
		"busiest_bar": busiestBar,
	})
}

func GetTrendingBars(c echo.Context) error {
	trendingBars, err := services.GetTrendingBars()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, envelope{
			"error": "Failed to retrieve trending bars",
		})
	}

	if len(trendingBars) == 0 {
		return c.JSON(http.StatusOK, envelope{
			"message": "No bars have reports in the last 3 hours",
		})
	}

	return c.JSON(http.StatusOK, envelope{
		"trending_bars": trendingBars,
	})
}

func GetShortestWaitBar(c echo.Context) error {
	shortestWaitBar, err := services.GetShortestWaitBar()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, envelope{
			"error": "Failed to retrieve bar with shortest wait time",
		})
	}

	if shortestWaitBar == nil {
		return c.JSON(http.StatusOK, envelope{
			"message": "No bars have reports in the last 3 hours",
		})
	}

	return c.JSON(http.StatusOK, envelope{
		"shortest_wait_bar": shortestWaitBar,
	})
}

func GetBarsList(c echo.Context) error {
	var request models.BarRequest
	if err := c.Bind(&request); err != nil {
		return c.JSON(http.StatusBadRequest, envelope{"error": "Invalid request"})
	}

	bars, err := services.GetAllBars(request.UserLatitude, request.UserLongitude)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, envelope{
			"error": "Failed to retrieve bars",
		})
	}

	return c.JSON(http.StatusOK, envelope{
		"bars": bars,
	})
}