209 lines
6.4 KiB
Go
209 lines
6.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
|
)
|
|
|
|
// @Schemes
|
|
// @Summary get all orders
|
|
// @Description gets all orders as array
|
|
// @Tags orders
|
|
// @Produce json
|
|
// @Param table query int false "Table ID"
|
|
// @Param grouping query bool false "grouping"
|
|
// @Param filter query string false "filter"
|
|
// @Success 200 {array} Order
|
|
// @Router /orders [get]
|
|
func (c *Controller) GetOrders(ctx echo.Context) error {
|
|
table, _ := strconv.ParseUint(ctx.QueryParam("table"), 10, 64)
|
|
grouping, _ := strconv.ParseBool(ctx.QueryParam("grouping"))
|
|
stringFiler := ctx.QueryParam("filter")
|
|
var filter []string
|
|
if stringFiler != "" {
|
|
filter = strings.Split(stringFiler, ",")
|
|
}
|
|
options := GetOrderOptions{TableId: table, Grouped: grouping, Filter: filter}
|
|
var orders []Order
|
|
if options.TableId == 0 {
|
|
orders = c.getAllActiveOrders()
|
|
} else {
|
|
orders = c.getAllOrdersForTable(options)
|
|
}
|
|
return ctx.JSON(http.StatusOK, orders)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary create new order
|
|
// @Description creates a new order and returns it
|
|
// @Tags orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param item query int true "OrderItem ID"
|
|
// @Param table query int true "Table ID"
|
|
// @Success 201 {object} Order
|
|
// @Failure 400
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders [post]
|
|
func (c *Controller) CreateOrder(ctx echo.Context) error {
|
|
table, err1 := strconv.ParseUint(ctx.QueryParam("table"), 10, 64)
|
|
item, err2 := strconv.ParseUint(ctx.QueryParam("item"), 10, 64)
|
|
if err1 != nil || err2 != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, types.MissingInformation.String())
|
|
}
|
|
order := Order{TableID: table, OrderItemID: item, IsServed: false}
|
|
err := c.createOrder(&order)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusCreated, order)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary delete an order
|
|
// @Description deletes an order from the database
|
|
// @Tags orders
|
|
// @Produce json
|
|
// @Param item query int true "OrderItem ID"
|
|
// @Param table query int true "Table ID"
|
|
// @Success 200 "OK"
|
|
// @Failure 400 "Bad Request"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders [delete]
|
|
func (c *Controller) DeleteOrder(ctx echo.Context) error {
|
|
item := ctx.QueryParam("item")
|
|
table := ctx.QueryParam("table")
|
|
if table == "" || item == "" {
|
|
return echo.NewHTTPError(http.StatusBadRequest, types.MissingInformation.String())
|
|
}
|
|
err := c.deleteOrder(table, item)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary update an order
|
|
// @Description updates an order with provided information
|
|
// @Tags orders
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param order body Order true "updated Order"
|
|
// @Success 200 {object} Order
|
|
// @Failure 400 "Bad Request"
|
|
// @Failure 404 "Not Found"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders [put]
|
|
func (c *Controller) UpdateOrder(ctx echo.Context) error {
|
|
var newOrder Order
|
|
err := ctx.Bind(&newOrder)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
oldOrder, err := c.doesOrderExist(strconv.Itoa(int(newOrder.ID)))
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
}
|
|
err = c.updateOrder(&oldOrder, &newOrder)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusOK, newOrder)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary get all orderItems
|
|
// @Description gets all orderItems as array
|
|
// @Tags orderItems
|
|
// @Produce json
|
|
// @Param type query int true "ItemType"
|
|
// @Success 200 {array} OrderItem
|
|
// @Router /orders/items [get]
|
|
func (c *Controller) GetOrderItems(ctx echo.Context) error {
|
|
orderType := ctx.QueryParam("type")
|
|
if orderType == "" {
|
|
return echo.NewHTTPError(http.StatusBadRequest, types.MissingInformation.String())
|
|
}
|
|
return ctx.JSON(http.StatusOK, c.getOrderItemsForType(orderType))
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary create new orderItem
|
|
// @Description creates a new orderItem and returns it
|
|
// @Tags orderItems
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param order body OrderItem true "OrderItem ID"
|
|
// @Success 201 {object} OrderItem
|
|
// @Failure 400 "Bad Request"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders/items [post]
|
|
func (c *Controller) CreateOrderItem(ctx echo.Context) error {
|
|
var orderItem OrderItem
|
|
err := ctx.Bind(&orderItem)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
err = c.createOrderItem(&orderItem)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusCreated, orderItem)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary update a orderItem
|
|
// @Description updates a orderItem with provided information
|
|
// @Tags orderItems
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param orderItem body OrderItem true "updated OrderItem"
|
|
// @Success 200 {object} OrderItem
|
|
// @Failure 400 "Bad Request"
|
|
// @Failure 404 "Not Found"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders/items [put]
|
|
func (c *Controller) UpdateOrderItem(ctx echo.Context) error {
|
|
var newOrderItem OrderItem
|
|
err := ctx.Bind(&newOrderItem)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
oldOrderItem, err := c.doesOrderItemExist(strconv.Itoa(int(newOrderItem.ID)))
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
}
|
|
err = c.updateOrderItem(&oldOrderItem, &newOrderItem)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.JSON(http.StatusOK, newOrderItem)
|
|
}
|
|
|
|
// @Schemes
|
|
// @Summary delete an orderItem
|
|
// @Description deletes an orderItem from the database
|
|
// @Tags orderItems
|
|
// @Produce json
|
|
// @Param id path int true "OrderItem ID"
|
|
// @Success 200 "OK"
|
|
// @Failure 404 "Not Found"
|
|
// @Failure 500 "Internal Server Error"
|
|
// @Router /orders/items/{id} [delete]
|
|
func (c *Controller) DeleteOrderItem(ctx echo.Context) error {
|
|
id := ctx.Param("id")
|
|
orderItem, err := c.doesOrderItemExist(id)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusNotFound, err.Error())
|
|
}
|
|
err = c.deleteOrderItem(&orderItem)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return ctx.NoContent(http.StatusOK)
|
|
}
|