add orders edit view
This commit is contained in:
@@ -26,6 +26,8 @@ type CartItemController interface {
|
||||
CheckoutHandler(*gin.Context)
|
||||
OrderView(*gin.Context)
|
||||
OrderHandler(*gin.Context)
|
||||
OrdersView(*gin.Context)
|
||||
OrdersHandler(*gin.Context)
|
||||
}
|
||||
|
||||
type cartItemController struct{}
|
||||
@@ -496,3 +498,64 @@ func (rc *cartItemController) OrderHandler(c *gin.Context) {
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user