allow printing/editing orders
This commit is contained in:
@@ -515,47 +515,50 @@ func (rc *cartItemController) OrdersView(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (rc *cartItemController) OrdersHandler(c *gin.Context) {
|
||||
confirmation := c.PostForm("confirm-order")
|
||||
token := c.Param("token")
|
||||
|
||||
if confirmation == "" {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
||||
if token == "" {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "EmptyToken"})
|
||||
return
|
||||
}
|
||||
|
||||
if confirmation != "true" {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Order was not confirmed."})
|
||||
return
|
||||
action := c.PostForm("action")
|
||||
|
||||
if action == "update" {
|
||||
order, err := repositories.Orders.GetByToken(token)
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "No order with given token found"})
|
||||
return
|
||||
}
|
||||
|
||||
status := c.PostForm("order-status")
|
||||
|
||||
//TODO validate status
|
||||
if status == "" {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Invalid Order Status"})
|
||||
return
|
||||
}
|
||||
|
||||
order.Status = models.OrderStatus(status)
|
||||
|
||||
_, err = repositories.Orders.Update(order)
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
sessionId := GetSessionId(c)
|
||||
order, err := repositories.Orders.GetBySession(sessionId)
|
||||
if action == "delete" {
|
||||
fmt.Println("Deleting Order ", token)
|
||||
err := repositories.Orders.DeleteByToken(token)
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
||||
return
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
order.Status = models.AwaitingPayment
|
||||
|
||||
err = order.Validate()
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
|
||||
for idx := range order.CartItems {
|
||||
order.CartItems[idx].SessionId = "0"
|
||||
repositories.CartItems.Update(order.CartItems[idx])
|
||||
}
|
||||
|
||||
_, err = repositories.Orders.Update(order)
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
|
||||
//TODO: cartItemRepository delete all by session - otherwise items stay in cart after completing order..
|
||||
|
||||
c.Redirect(http.StatusFound, fmt.Sprintf("/order/%s", order.Token))
|
||||
c.Redirect(http.StatusFound, "/orders")
|
||||
}
|
||||
|
||||
@@ -63,18 +63,8 @@ func (rc *printController) PrintCartView(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
type ShopItemVariantPair struct {
|
||||
ShopItem models.ShopItem
|
||||
ItemVariant models.ItemVariant
|
||||
}
|
||||
|
||||
var items []ShopItemVariantPair
|
||||
for _, cartItem := range cartItems {
|
||||
items = append(items, ShopItemVariantPair{ShopItem: cartItem.ShopItem, ItemVariant: cartItem.ItemVariant})
|
||||
}
|
||||
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"itemVariants": items,
|
||||
"cartItems": cartItems,
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
||||
@@ -90,18 +80,8 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
|
||||
|
||||
cartItems := order.CartItems
|
||||
|
||||
type ShopItemVariantPair struct {
|
||||
ShopItem models.ShopItem
|
||||
ItemVariant models.ItemVariant
|
||||
}
|
||||
|
||||
var items []ShopItemVariantPair
|
||||
for _, cartItem := range cartItems {
|
||||
items = append(items, ShopItemVariantPair{ShopItem: cartItem.ShopItem, ItemVariant: cartItem.ItemVariant})
|
||||
}
|
||||
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"itemVariants": items,
|
||||
"cartItems": cartItems,
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
||||
|
||||
3
main.go
3
main.go
@@ -98,8 +98,9 @@ func main() {
|
||||
viewRoutes.POST("/order", cartItemController.OrderHandler)
|
||||
viewRoutes.GET("/order/:token", cartItemController.OrderView)
|
||||
viewRoutes.GET("/order/:token/print", authValidator.RequireAuth, printController.PrintOrderView)
|
||||
|
||||
viewRoutes.GET("/orders", authValidator.RequireAuth, cartItemController.OrdersView)
|
||||
viewRoutes.POST("/orders", authValidator.RequireAuth, cartItemController.OrdersHandler)
|
||||
viewRoutes.POST("/order/:token/edit", authValidator.RequireAuth, cartItemController.OrdersHandler)
|
||||
|
||||
//write middleware that redirects to homescreen on register/login/reset for logged in users
|
||||
viewRoutes.GET("/login", userController.LoginView)
|
||||
|
||||
@@ -15,6 +15,7 @@ type OrderRepository interface {
|
||||
GetByToken(string) (models.Order, error)
|
||||
Update(models.Order) (models.Order, error)
|
||||
DeleteById(string) error
|
||||
DeleteByToken(string) error
|
||||
}
|
||||
|
||||
type GORMOrderRepository struct {
|
||||
@@ -95,3 +96,8 @@ func (r *GORMOrderRepository) DeleteById(id string) error {
|
||||
result := r.DB.Delete(&models.Order{}, orderId)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (r *GORMOrderRepository) DeleteByToken(token string) error {
|
||||
result := r.DB.Where("token = ?", token).Delete(&models.Order{})
|
||||
return result.Error
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
<form action="/print" method="POST">
|
||||
<table class="w-full text-left font-medium text-gray-900 dark:text-white">
|
||||
<tbody class="divide-y divide-gray-200 dark:divide-gray-800">
|
||||
{{ range .data.itemVariants }}
|
||||
{{ range .data.cartItems }}
|
||||
<tr>
|
||||
<input type="hidden" name="variant-id[]" value="{{ .ItemVariant.ID }}" required>
|
||||
<td class="whitespace-nowrap py-4">
|
||||
@@ -49,7 +49,7 @@
|
||||
<td class="p-4 text-right text-base font-bold text-gray-900 dark:text-white">
|
||||
<div>
|
||||
<div class="mt-2">
|
||||
<input type="number" name="variant-amount[]" step="1" min="0" required class="block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6">
|
||||
<input type="number" name="variant-amount[]" value="{{ .Quantity }}" step="1" min="0" required class="block w-full rounded-md bg-white px-3 py-1.5 text-base text-gray-900 outline outline-1 -outline-offset-1 outline-gray-300 placeholder:text-gray-400 focus:outline focus:outline-2 focus:-outline-offset-2 focus:outline-indigo-600 sm:text-sm/6">
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user