Fix swagger deps
This commit is contained in:
parent
28f068c890
commit
f63210272d
28 changed files with 307 additions and 310 deletions
|
@ -4,8 +4,6 @@ import (
|
|||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
||||
)
|
||||
|
||||
type (
|
||||
|
@ -17,13 +15,13 @@ type (
|
|||
}
|
||||
|
||||
BillItem struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
|
||||
BillID uint64 `json:"bill_id" validate:"required"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
Total float32 `json:"total" validate:"required"`
|
||||
Price float32 `json:"price" validate:"required"`
|
||||
Amount uint64 `json:"amount" validate:"required"`
|
||||
ItemType types.ItemType `json:"item_type" validate:"required"`
|
||||
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
|
||||
BillID uint64 `json:"bill_id" validate:"required"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
Total float32 `json:"total" validate:"required"`
|
||||
Price float32 `json:"price" validate:"required"`
|
||||
Amount uint64 `json:"amount" validate:"required"`
|
||||
ItemType ItemType `json:"item_type" validate:"required"`
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -31,7 +29,7 @@ func (c *Controller) DoesBillExist(id string) (Bill, error) {
|
|||
var bill Bill
|
||||
result := c.orm.Limit(1).Find(&bill, id)
|
||||
if result.RowsAffected == 0 {
|
||||
return bill, fmt.Errorf(types.CannotFind.String())
|
||||
return bill, fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
return bill, nil
|
||||
}
|
||||
|
@ -40,7 +38,7 @@ func (c *Controller) GetAllBillItems(billId uint64) ([]BillItem, error) {
|
|||
var billItems []BillItem
|
||||
result := c.orm.Where("bill_id = ?", billId).Find(&billItems)
|
||||
if result.RowsAffected == 0 {
|
||||
return billItems, fmt.Errorf(types.CannotFind.String())
|
||||
return billItems, fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
return billItems, nil
|
||||
}
|
||||
|
@ -48,19 +46,19 @@ func (c *Controller) GetAllBillItems(billId uint64) ([]BillItem, error) {
|
|||
func getDate(year string, month string, day string) (time.Time, error) {
|
||||
yearI, yearErr := strconv.Atoi(year)
|
||||
if yearErr != nil {
|
||||
return time.Time{}, fmt.Errorf("jahr " + types.CannotParse.String())
|
||||
return time.Time{}, fmt.Errorf("jahr " + CannotParse.String())
|
||||
}
|
||||
monthI, monthErr := strconv.Atoi(month)
|
||||
if monthErr != nil {
|
||||
return time.Time{}, fmt.Errorf("monat " + types.CannotParse.String())
|
||||
return time.Time{}, fmt.Errorf("monat " + CannotParse.String())
|
||||
}
|
||||
dayI, dayErr := strconv.Atoi(day)
|
||||
if dayErr != nil {
|
||||
return time.Time{}, fmt.Errorf("tag " + types.CannotParse.String())
|
||||
return time.Time{}, fmt.Errorf("tag " + CannotParse.String())
|
||||
}
|
||||
loc, locErr := time.LoadLocation("Local")
|
||||
if locErr != nil {
|
||||
return time.Time{}, fmt.Errorf("timezone " + types.CannotParse.String())
|
||||
return time.Time{}, fmt.Errorf("timezone " + CannotParse.String())
|
||||
}
|
||||
return time.Date(yearI, time.Month(monthI), dayI, 0, 0, 0, 0, loc), nil
|
||||
}
|
||||
|
@ -88,7 +86,7 @@ func (c *Controller) createBill(options GetOrderOptions) (Bill, error) {
|
|||
bill.Total = total
|
||||
err := c.orm.Create(&bill).Error
|
||||
if err != nil {
|
||||
return bill, fmt.Errorf(types.CannotCreate.String())
|
||||
return bill, fmt.Errorf(CannotCreate.String())
|
||||
}
|
||||
for _, order := range orders {
|
||||
billItem := BillItem{
|
||||
|
@ -104,10 +102,10 @@ func (c *Controller) createBill(options GetOrderOptions) (Bill, error) {
|
|||
ordersToDelete := c.getAllOrdersForTable(GetOrderOptions{TableId: options.TableId, Grouped: false, Filter: options.Filter})
|
||||
err = c.orm.Delete(&ordersToDelete).Error
|
||||
if err != nil {
|
||||
return bill, fmt.Errorf(types.CannotDelete.String())
|
||||
return bill, fmt.Errorf(CannotDelete.String())
|
||||
}
|
||||
c.publishMessage(StatusMessage{
|
||||
Type: types.DeleteAll,
|
||||
Type: DeleteAll,
|
||||
Payload: ordersToDelete,
|
||||
})
|
||||
return bill, nil
|
||||
|
@ -116,7 +114,7 @@ func (c *Controller) createBill(options GetOrderOptions) (Bill, error) {
|
|||
func (c *Controller) deleteBill(bill *Bill) error {
|
||||
err := c.orm.Delete(bill).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotDelete.String())
|
||||
return fmt.Errorf(CannotDelete.String())
|
||||
}
|
||||
billItemsToDelete, _ := c.GetAllBillItems(bill.ID)
|
||||
c.orm.Delete(&billItemsToDelete)
|
||||
|
|
|
@ -4,14 +4,21 @@ import (
|
|||
"encoding/json"
|
||||
|
||||
"github.com/r3labs/sse/v2"
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
||||
)
|
||||
|
||||
const ServerSideEvent = "sse"
|
||||
|
||||
type NotifierType uint
|
||||
|
||||
const (
|
||||
Create NotifierType = iota
|
||||
Delete
|
||||
DeleteAll
|
||||
)
|
||||
|
||||
type StatusMessage struct {
|
||||
Type types.NotifierType `json:"type"`
|
||||
Payload []Order `json:"payload"`
|
||||
Type NotifierType `json:"type"`
|
||||
Payload []Order `json:"payload"`
|
||||
}
|
||||
|
||||
func (c *Controller) setupEventChannel() {
|
||||
|
|
|
@ -4,7 +4,6 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -21,10 +20,10 @@ type (
|
|||
}
|
||||
|
||||
OrderItem struct {
|
||||
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
|
||||
ItemType types.ItemType `json:"item_type" validate:"required"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
Price float32 `json:"price" validate:"required"`
|
||||
ID uint64 `gorm:"primaryKey" json:"id" validate:"optional"`
|
||||
ItemType ItemType `json:"item_type" validate:"required"`
|
||||
Description string `json:"description" validate:"required"`
|
||||
Price float32 `json:"price" validate:"required"`
|
||||
}
|
||||
|
||||
GetOrderOptions struct {
|
||||
|
@ -55,7 +54,7 @@ func (c *Controller) doesOrderItemExist(id string) (OrderItem, error) {
|
|||
var orderItem OrderItem
|
||||
result := c.orm.Limit(1).Find(&orderItem, id)
|
||||
if result.RowsAffected == 0 {
|
||||
return orderItem, fmt.Errorf(types.CannotFind.String())
|
||||
return orderItem, fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
return orderItem, nil
|
||||
}
|
||||
|
@ -64,7 +63,7 @@ func (c *Controller) doesOrderExist(id string) (Order, error) {
|
|||
var order Order
|
||||
result := c.orm.Limit(1).Find(&order, id)
|
||||
if result.RowsAffected == 0 {
|
||||
return order, fmt.Errorf(types.CannotFind.String())
|
||||
return order, fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
return order, nil
|
||||
}
|
||||
|
@ -96,11 +95,11 @@ func (c *Controller) getAllOrdersForTable(options GetOrderOptions) []Order {
|
|||
func (c *Controller) createOrder(order *Order) error {
|
||||
err := c.orm.Create(order).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotCreate.String())
|
||||
return fmt.Errorf(CannotCreate.String())
|
||||
}
|
||||
c.orm.Model(&Order{}).Joins("OrderItem").First(order)
|
||||
c.publishMessage(StatusMessage{
|
||||
Type: types.Create,
|
||||
Type: Create,
|
||||
Payload: []Order{*order},
|
||||
})
|
||||
return nil
|
||||
|
@ -109,11 +108,11 @@ func (c *Controller) createOrder(order *Order) error {
|
|||
func (c *Controller) updateOrder(old *Order, new *Order) error {
|
||||
err := c.orm.First(old).Updates(new).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotUpdate.String())
|
||||
return fmt.Errorf(CannotUpdate.String())
|
||||
}
|
||||
if new.IsServed {
|
||||
c.publishMessage(StatusMessage{
|
||||
Type: types.Delete,
|
||||
Type: Delete,
|
||||
Payload: []Order{*new},
|
||||
})
|
||||
}
|
||||
|
@ -124,14 +123,14 @@ func (c *Controller) deleteOrder(tableId string, orderItemId string) error {
|
|||
var order Order
|
||||
err := c.orm.Where("table_id = ? AND order_item_id = ?", tableId, orderItemId).Last(&order).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotFind.String())
|
||||
return fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
err = c.orm.Delete(&order).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotDelete.String())
|
||||
return fmt.Errorf(CannotDelete.String())
|
||||
}
|
||||
c.publishMessage(StatusMessage{
|
||||
Type: types.Delete,
|
||||
Type: Delete,
|
||||
Payload: []Order{order},
|
||||
})
|
||||
return nil
|
||||
|
@ -139,14 +138,14 @@ func (c *Controller) deleteOrder(tableId string, orderItemId string) error {
|
|||
|
||||
func (c *Controller) getOrderItemsForType(itemType string) []OrderItem {
|
||||
var orderItems []OrderItem
|
||||
c.orm.Order("description").Where("item_type = ?", types.ParseItemType(itemType)).Find(&orderItems)
|
||||
c.orm.Order("description").Where("item_type = ?", ParseItemType(itemType)).Find(&orderItems)
|
||||
return orderItems
|
||||
}
|
||||
|
||||
func (c *Controller) createOrderItem(oderItem *OrderItem) error {
|
||||
err := c.orm.Create(oderItem).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotCreate.String())
|
||||
return fmt.Errorf(CannotCreate.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -154,7 +153,7 @@ func (c *Controller) createOrderItem(oderItem *OrderItem) error {
|
|||
func (c *Controller) updateOrderItem(old *OrderItem, new *OrderItem) error {
|
||||
err := c.orm.First(old).Updates(new).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotUpdate.String())
|
||||
return fmt.Errorf(CannotUpdate.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -162,7 +161,7 @@ func (c *Controller) updateOrderItem(old *OrderItem, new *OrderItem) error {
|
|||
func (c *Controller) deleteOrderItem(oderItem *OrderItem) error {
|
||||
err := c.orm.Delete(oderItem).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotDelete.String())
|
||||
return fmt.Errorf(CannotDelete.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -6,25 +6,24 @@ import (
|
|||
"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]
|
||||
// @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())
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, MissingInformation.String())
|
||||
}
|
||||
bills, err := c.GetAllBills(year, month, day)
|
||||
if err != nil {
|
||||
|
@ -33,14 +32,14 @@ func (c *Controller) GetBills(ctx echo.Context) 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]
|
||||
// @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 {
|
||||
|
@ -53,21 +52,21 @@ func (c *Controller) GetBillItems(ctx echo.Context) 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]
|
||||
// @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())
|
||||
return echo.NewHTTPError(http.StatusBadRequest, MissingInformation.String())
|
||||
}
|
||||
stringFiler := ctx.QueryParam("filter")
|
||||
var filter []string
|
||||
|
@ -81,16 +80,16 @@ func (c *Controller) CreateBill(ctx echo.Context) 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]
|
||||
// @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)
|
||||
|
|
|
@ -6,19 +6,18 @@ import (
|
|||
"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]
|
||||
// @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"))
|
||||
|
@ -37,23 +36,23 @@ func (c *Controller) GetOrders(ctx echo.Context) error {
|
|||
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]
|
||||
// @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())
|
||||
return echo.NewHTTPError(http.StatusBadRequest, MissingInformation.String())
|
||||
}
|
||||
order := Order{TableID: table, OrderItemID: item, IsServed: false}
|
||||
err := c.createOrder(&order)
|
||||
|
@ -63,22 +62,22 @@ func (c *Controller) CreateOrder(ctx echo.Context) 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]
|
||||
// @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())
|
||||
return echo.NewHTTPError(http.StatusBadRequest, MissingInformation.String())
|
||||
}
|
||||
err := c.deleteOrder(table, item)
|
||||
if err != nil {
|
||||
|
@ -87,18 +86,18 @@ func (c *Controller) DeleteOrder(ctx echo.Context) 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]
|
||||
// @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)
|
||||
|
@ -116,33 +115,33 @@ func (c *Controller) UpdateOrder(ctx echo.Context) 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]
|
||||
// @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 echo.NewHTTPError(http.StatusBadRequest, 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]
|
||||
// @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)
|
||||
|
@ -156,18 +155,18 @@ func (c *Controller) CreateOrderItem(ctx echo.Context) 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]
|
||||
// @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)
|
||||
|
@ -185,16 +184,16 @@ func (c *Controller) UpdateOrderItem(ctx echo.Context) 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]
|
||||
// @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)
|
||||
|
|
|
@ -3,7 +3,6 @@ package controller
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
||||
"gorm.io/plugin/soft_delete"
|
||||
)
|
||||
|
||||
|
@ -42,7 +41,7 @@ func (c *Controller) CreateNewTable() (Table, error) {
|
|||
err = c.orm.Unscoped().Save(&table).Error
|
||||
}
|
||||
if err != nil {
|
||||
return table, fmt.Errorf(types.CannotCreate.String())
|
||||
return table, fmt.Errorf(CannotCreate.String())
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
@ -61,14 +60,14 @@ func (c *Controller) DeleteLatestTable() error {
|
|||
"tables.id",
|
||||
).Last(&table).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotFind.String())
|
||||
return fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
if table.OrderCount != 0 {
|
||||
return fmt.Errorf(types.StillInUse.String())
|
||||
return fmt.Errorf(StillInUse.String())
|
||||
}
|
||||
err = c.orm.Delete(&table).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotDelete.String())
|
||||
return fmt.Errorf(CannotDelete.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,21 +1,8 @@
|
|||
package types
|
||||
package controller
|
||||
|
||||
type (
|
||||
ErrorResponses uint
|
||||
ItemType uint
|
||||
NotifierType uint
|
||||
)
|
||||
|
||||
const (
|
||||
Create NotifierType = iota
|
||||
Delete
|
||||
DeleteAll
|
||||
)
|
||||
|
||||
const (
|
||||
Food ItemType = iota
|
||||
ColdDrink
|
||||
HotDrink
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -28,17 +15,6 @@ const (
|
|||
CannotParse
|
||||
)
|
||||
|
||||
func ParseItemType(itemType string) ItemType {
|
||||
switch itemType {
|
||||
case "0":
|
||||
return Food
|
||||
case "1":
|
||||
return ColdDrink
|
||||
default:
|
||||
return HotDrink
|
||||
}
|
||||
}
|
||||
|
||||
func (e ErrorResponses) String() string {
|
||||
switch e {
|
||||
case MissingInformation:
|
||||
|
@ -57,3 +33,20 @@ func (e ErrorResponses) String() string {
|
|||
return "kann nicht verarbeitet werden"
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
Food ItemType = iota
|
||||
ColdDrink
|
||||
HotDrink
|
||||
)
|
||||
|
||||
func ParseItemType(itemType string) ItemType {
|
||||
switch itemType {
|
||||
case "0":
|
||||
return Food
|
||||
case "1":
|
||||
return ColdDrink
|
||||
default:
|
||||
return HotDrink
|
||||
}
|
||||
}
|
|
@ -2,8 +2,6 @@ package controller
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitlab.unjx.de/flohoss/cafe-plaetschwiesle/internal/types"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
|
@ -16,7 +14,7 @@ func (c *Controller) doesUserExist(username string) (User, error) {
|
|||
var user User
|
||||
result := c.orm.Limit(1).Find(&user, "username = ?", username)
|
||||
if result.RowsAffected == 0 {
|
||||
return user, fmt.Errorf(types.CannotFind.String())
|
||||
return user, fmt.Errorf(CannotFind.String())
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
@ -25,7 +23,7 @@ func (c *Controller) getUserOrCreate(username string) (User, error) {
|
|||
var user User
|
||||
err := c.orm.Where(User{Username: username}).Attrs(User{ShowHotDrinks: true, ShowColdDrinks: true}).FirstOrCreate(&user).Error
|
||||
if err != nil {
|
||||
return user, fmt.Errorf(types.CannotCreate.String())
|
||||
return user, fmt.Errorf(CannotCreate.String())
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
@ -36,7 +34,7 @@ func (c *Controller) updateUser(old *User, new *User) error {
|
|||
"ShowColdDrinks": new.ShowColdDrinks,
|
||||
"ShowHotDrinks": new.ShowHotDrinks}).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf(types.CannotUpdate.String())
|
||||
return fmt.Errorf(CannotUpdate.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue