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) {
|
func (rc *cartItemController) OrdersHandler(c *gin.Context) {
|
||||||
confirmation := c.PostForm("confirm-order")
|
token := c.Param("token")
|
||||||
|
|
||||||
if confirmation == "" {
|
if token == "" {
|
||||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "EmptyToken"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if confirmation != "true" {
|
action := c.PostForm("action")
|
||||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Order was not confirmed."})
|
|
||||||
return
|
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)
|
if action == "delete" {
|
||||||
order, err := repositories.Orders.GetBySession(sessionId)
|
fmt.Println("Deleting Order ", token)
|
||||||
|
err := repositories.Orders.DeleteByToken(token)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||||
return
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
order.Status = models.AwaitingPayment
|
c.Redirect(http.StatusFound, "/orders")
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,18 +63,8 @@ func (rc *printController) PrintCartView(c *gin.Context) {
|
|||||||
return
|
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{
|
data := CreateSessionData(c, gin.H{
|
||||||
"itemVariants": items,
|
"cartItems": cartItems,
|
||||||
})
|
})
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
c.HTML(http.StatusOK, "printvariant.html", data)
|
||||||
@@ -90,18 +80,8 @@ func (rc *printController) PrintOrderView(c *gin.Context) {
|
|||||||
|
|
||||||
cartItems := order.CartItems
|
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{
|
data := CreateSessionData(c, gin.H{
|
||||||
"itemVariants": items,
|
"cartItems": cartItems,
|
||||||
})
|
})
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
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.POST("/order", cartItemController.OrderHandler)
|
||||||
viewRoutes.GET("/order/:token", cartItemController.OrderView)
|
viewRoutes.GET("/order/:token", cartItemController.OrderView)
|
||||||
viewRoutes.GET("/order/:token/print", authValidator.RequireAuth, printController.PrintOrderView)
|
viewRoutes.GET("/order/:token/print", authValidator.RequireAuth, printController.PrintOrderView)
|
||||||
|
|
||||||
viewRoutes.GET("/orders", authValidator.RequireAuth, cartItemController.OrdersView)
|
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
|
//write middleware that redirects to homescreen on register/login/reset for logged in users
|
||||||
viewRoutes.GET("/login", userController.LoginView)
|
viewRoutes.GET("/login", userController.LoginView)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type OrderRepository interface {
|
|||||||
GetByToken(string) (models.Order, error)
|
GetByToken(string) (models.Order, error)
|
||||||
Update(models.Order) (models.Order, error)
|
Update(models.Order) (models.Order, error)
|
||||||
DeleteById(string) error
|
DeleteById(string) error
|
||||||
|
DeleteByToken(string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type GORMOrderRepository struct {
|
type GORMOrderRepository struct {
|
||||||
@@ -95,3 +96,8 @@ func (r *GORMOrderRepository) DeleteById(id string) error {
|
|||||||
result := r.DB.Delete(&models.Order{}, orderId)
|
result := r.DB.Delete(&models.Order{}, orderId)
|
||||||
return result.Error
|
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">
|
<form action="/print" method="POST">
|
||||||
<table class="w-full text-left font-medium text-gray-900 dark:text-white">
|
<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">
|
<tbody class="divide-y divide-gray-200 dark:divide-gray-800">
|
||||||
{{ range .data.itemVariants }}
|
{{ range .data.cartItems }}
|
||||||
<tr>
|
<tr>
|
||||||
<input type="hidden" name="variant-id[]" value="{{ .ItemVariant.ID }}" required>
|
<input type="hidden" name="variant-id[]" value="{{ .ItemVariant.ID }}" required>
|
||||||
<td class="whitespace-nowrap py-4">
|
<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">
|
<td class="p-4 text-right text-base font-bold text-gray-900 dark:text-white">
|
||||||
<div>
|
<div>
|
||||||
<div class="mt-2">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user