Compare commits
3 Commits
b55bf67e57
...
adac366896
| Author | SHA1 | Date | |
|---|---|---|---|
|
adac366896
|
|||
|
b14deeb24f
|
|||
|
d5c3d7fe75
|
@@ -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{}
|
||||
@@ -480,6 +482,72 @@ func (rc *cartItemController) OrderHandler(c *gin.Context) {
|
||||
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))
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
type PrintController interface {
|
||||
PrintVariantView(*gin.Context)
|
||||
PrintCartView(*gin.Context)
|
||||
PrintOrderView(*gin.Context)
|
||||
PrintHandler(*gin.Context)
|
||||
}
|
||||
|
||||
@@ -79,6 +80,33 @@ func (rc *printController) PrintCartView(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
||||
}
|
||||
|
||||
func (rc *printController) PrintOrderView(c *gin.Context) {
|
||||
order, err := repositories.Orders.GetByToken(c.Param("token"))
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "printvariant.html", data)
|
||||
}
|
||||
|
||||
func (rc *printController) PrintHandler(c *gin.Context) {
|
||||
variantIds := c.PostFormArray("variant-id[]")
|
||||
variantAmounts := c.PostFormArray("variant-amount[]")
|
||||
|
||||
@@ -90,6 +90,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
|
||||
|
||||
dstPdf := ""
|
||||
pdf, err := ctx.FormFile("pdf")
|
||||
fmt.Println(pdf)
|
||||
|
||||
if err == nil {
|
||||
dstPdf = filepath.Join("static/uploads", pdf.Filename)
|
||||
|
||||
5
main.go
5
main.go
@@ -95,8 +95,11 @@ func main() {
|
||||
viewRoutes.POST("/cart/edit", cartItemController.EditItemHandler)
|
||||
viewRoutes.GET("/checkout", cartItemController.CheckoutView)
|
||||
viewRoutes.POST("/checkout", cartItemController.CheckoutHandler)
|
||||
viewRoutes.GET("/order/:token", cartItemController.OrderView)
|
||||
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)
|
||||
|
||||
//write middleware that redirects to homescreen on register/login/reset for logged in users
|
||||
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="/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>
|
||||
<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="/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>
|
||||
|
||||
Reference in New Issue
Block a user