add orders edit view
This commit is contained in:
@@ -26,6 +26,8 @@ type CartItemController interface {
|
|||||||
CheckoutHandler(*gin.Context)
|
CheckoutHandler(*gin.Context)
|
||||||
OrderView(*gin.Context)
|
OrderView(*gin.Context)
|
||||||
OrderHandler(*gin.Context)
|
OrderHandler(*gin.Context)
|
||||||
|
OrdersView(*gin.Context)
|
||||||
|
OrdersHandler(*gin.Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type cartItemController struct{}
|
type cartItemController struct{}
|
||||||
@@ -496,3 +498,64 @@ func (rc *cartItemController) OrderHandler(c *gin.Context) {
|
|||||||
|
|
||||||
c.Redirect(http.StatusFound, fmt.Sprintf("/order/%s", order.Token))
|
c.Redirect(http.StatusFound, fmt.Sprintf("/order/%s", order.Token))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rc *cartItemController) OrdersView(c *gin.Context) {
|
||||||
|
orders, err := repositories.Orders.GetAll()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Orders doe not exist."})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.HTML(http.StatusOK, "editorders.html", CreateSessionData(c, gin.H{
|
||||||
|
"error": "",
|
||||||
|
"success": "",
|
||||||
|
"orders": orders,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rc *cartItemController) OrdersHandler(c *gin.Context) {
|
||||||
|
confirmation := c.PostForm("confirm-order")
|
||||||
|
|
||||||
|
if confirmation == "" {
|
||||||
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if confirmation != "true" {
|
||||||
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Order was not confirmed."})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionId := GetSessionId(c)
|
||||||
|
order, err := repositories.Orders.GetBySession(sessionId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": "Something went wrong, try again later"})
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -98,6 +98,8 @@ 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.POST("/orders", 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)
|
||||||
|
|||||||
@@ -30,6 +30,8 @@
|
|||||||
<a href="/additem" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Add Item</a>
|
<a href="/additem" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Add Item</a>
|
||||||
<a href="/batchupload" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300
|
<a href="/batchupload" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300
|
||||||
hover:bg-gray-700 hover:text-white">Batch Upload</a>
|
hover:bg-gray-700 hover:text-white">Batch Upload</a>
|
||||||
|
<a href="/orders" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300
|
||||||
|
hover:bg-gray-700 hover:text-white">Orders</a>
|
||||||
<a href="/tags" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Tags</a>
|
<a href="/tags" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Tags</a>
|
||||||
<a href="/cart/print" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700
|
<a href="/cart/print" class="rounded-md bg-gray-900 m-2 px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700
|
||||||
hover:text-white">Print</a>
|
hover:text-white">Print</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user