cafe-plaetschwiesle/internal/controller/routesBill.go
2023-07-04 23:07:23 +02:00

105 lines
3 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 bills
// @Description gets all bills as array
// @Tags bills
// @Produce json
// @Param year query int true "year"
// @Param month query int true "month (1-12)"
// @Param day query int true "day (1-31)"
// @Success 200 {array} Bill
// @Router /bills [get]
func (c *Controller) GetBills(ctx echo.Context) error {
year := ctx.QueryParam("year")
month := ctx.QueryParam("month")
day := ctx.QueryParam("day")
if year == "" || month == "" || day == "" {
return echo.NewHTTPError(http.StatusInternalServerError, types.MissingInformation.String())
}
bills, err := c.GetAllBills(year, month, day)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusOK, bills)
}
// @Schemes
// @Summary get all billItems
// @Description gets all billItems for bill
// @Tags bills
// @Produce json
// @Param bill query int true "Bill ID"
// @Success 200 {array} BillItem
// @Router /bills/items [get]
func (c *Controller) GetBillItems(ctx echo.Context) error {
bill, err := c.DoesBillExist(ctx.QueryParam("bill"))
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
billItems, err := c.GetAllBillItems(bill.ID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusOK, billItems)
}
// @Schemes
// @Summary create new bill
// @Description creates a new bill and returns it
// @Tags bills
// @Produce json
// @Param table query int true "Table ID"
// @Param filter query string false "filter"
// @Success 201 {object} Bill
// @Failure 404 "Not Found"
// @Failure 500 "Internal Server Error"
// @Router /bills [post]
func (c *Controller) CreateBill(ctx echo.Context) error {
table, tableErr := strconv.ParseUint(ctx.QueryParam("table"), 10, 64)
if tableErr != nil {
return echo.NewHTTPError(http.StatusBadRequest, types.MissingInformation.String())
}
stringFiler := ctx.QueryParam("filter")
var filter []string
if stringFiler != "" {
filter = strings.Split(stringFiler, ",")
}
bill, err := c.createBill(GetOrderOptions{TableId: table, Grouped: true, Filter: filter})
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.JSON(http.StatusCreated, bill)
}
// @Schemes
// @Summary delete a bill
// @Description deletes a bill
// @Tags bills
// @Produce json
// @Param id path int true "Bill ID"
// @Success 200 "OK"
// @Failure 404 "Not Found"
// @Failure 500 "Internal Server Error"
// @Router /bills/{id} [delete]
func (c *Controller) DeleteBill(ctx echo.Context) error {
id := ctx.Param("id")
bill, err := c.DoesBillExist(id)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, err.Error())
}
err = c.deleteBill(&bill)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return ctx.NoContent(http.StatusOK)
}