hiling_go/api/menu_items.go
2026-06-14 05:52:04 +03:00

46 lines
1.1 KiB
Go

package api
import (
"net/http"
db "git.nochill.in/nochill/hiling_go/db/repository"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgtype"
)
type getMenuItemsReq struct {
LocationID *int32 `form:"location_id"`
}
// @Summary Get menu items
// @Tags menu
// @Produce json
// @Param location_id query int false "Filter by location ID (omit for all)"
// @Success 200 {array} db.GetMenuItemsRow
// @Failure 400 {object} map[string]any
// @Router /menu-items [get]
func (server *Server) getMenuItems(ctx *gin.Context) {
var req getMenuItemsReq
if err := ctx.ShouldBindQuery(&req); err != nil {
ctx.JSON(http.StatusBadRequest, ValidationErrorResponse(err))
return
}
locationID := pgtype.Int4{Valid: false}
if req.LocationID != nil {
locationID = pgtype.Int4{Int32: *req.LocationID, Valid: true}
}
items, err := server.Store.GetMenuItems(ctx, locationID)
if err != nil {
ctx.JSON(http.StatusInternalServerError, ErrorResponse(err, "Something went wrong while retrieving menu items"))
return
}
if items == nil {
items = []db.GetMenuItemsRow{}
}
ctx.JSON(http.StatusOK, items)
}