Compare commits
2 Commits
a9170b63b7
...
8a7d66f815
| Author | SHA1 | Date | |
|---|---|---|---|
|
8a7d66f815
|
|||
|
fa561c921d
|
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
@@ -36,6 +37,22 @@ func NewCartItemController() CartItemController {
|
||||
return &cartItemController{}
|
||||
}
|
||||
|
||||
// getSetCookieValue retrieves the value of a cookie from the Set-Cookie header
|
||||
func getSetCookieValue(c *gin.Context, cookieName string) string {
|
||||
// Check the Set-Cookie headers
|
||||
cookies := c.Writer.Header()["Set-Cookie"]
|
||||
for _, cookie := range cookies {
|
||||
if strings.HasPrefix(cookie, cookieName+"=") {
|
||||
// Extract the cookie value
|
||||
parts := strings.SplitN(cookie, ";", 2)
|
||||
if len(parts) > 0 {
|
||||
return strings.TrimPrefix(parts[0], cookieName+"=")
|
||||
}
|
||||
}
|
||||
}
|
||||
return "" // Return empty string if cookie is not found
|
||||
}
|
||||
|
||||
func generateSessionId(length int) string {
|
||||
bytes := make([]byte, length) // 16 bytes = 128 bits
|
||||
_, err := rand.Read(bytes)
|
||||
@@ -49,6 +66,13 @@ func GetSessionId(ctx *gin.Context) string {
|
||||
sessionId, err := ctx.Cookie("session_id")
|
||||
|
||||
if err != nil {
|
||||
//we need to check if we already set cookie in the response so that we dont do this multiple times
|
||||
responseCookie := getSetCookieValue(ctx, "session_id")
|
||||
|
||||
if len(responseCookie) != 0 {
|
||||
return responseCookie
|
||||
}
|
||||
|
||||
sessionId = generateSessionId(16)
|
||||
ctx.SetCookie("session_id", sessionId, 3600, "/", "", false, true)
|
||||
}
|
||||
@@ -170,7 +194,7 @@ func (rc *cartItemController) NewOrderFromForm(ctx *gin.Context) (models.Order,
|
||||
return models.Order{}, err
|
||||
}
|
||||
|
||||
cartItem := models.Order{
|
||||
order := models.Order{
|
||||
SessionId: sessionId,
|
||||
Status: status,
|
||||
Token: token,
|
||||
@@ -186,7 +210,7 @@ func (rc *cartItemController) NewOrderFromForm(ctx *gin.Context) (models.Order,
|
||||
CartItems: cartItems,
|
||||
}
|
||||
|
||||
return cartItem, nil
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func (rc *cartItemController) Create(c *gin.Context) {
|
||||
@@ -360,21 +384,31 @@ func (rc *cartItemController) CheckoutHandler(c *gin.Context) {
|
||||
existingOrder, err := repositories.Orders.GetBySession(order.SessionId)
|
||||
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
_, err = repositories.Orders.Create(order)
|
||||
fmt.Println("Creating Order")
|
||||
createdOrder, err := repositories.Orders.Create(order)
|
||||
|
||||
if err != nil {
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": err,
|
||||
"success": "",
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "error.html", data)
|
||||
return
|
||||
}
|
||||
|
||||
for _, cartItem := range order.CartItems {
|
||||
cartItem.OrderID = createdOrder.ID
|
||||
repositories.CartItems.Update(cartItem)
|
||||
}
|
||||
} else if err == nil {
|
||||
fmt.Println("Updating Order")
|
||||
order.ID = existingOrder.ID
|
||||
order.CreatedAt = existingOrder.CreatedAt
|
||||
repositories.Orders.Update(order)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": err,
|
||||
"success": "",
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "cart.html", data)
|
||||
return
|
||||
_, err := repositories.Orders.Update(order)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
shipping, err := models.GetShippingMethod(order.Shipping)
|
||||
@@ -384,7 +418,7 @@ func (rc *cartItemController) CheckoutHandler(c *gin.Context) {
|
||||
"success": "",
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "cart.html", data)
|
||||
c.HTML(http.StatusOK, "error.html", data)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -395,7 +429,7 @@ func (rc *cartItemController) CheckoutHandler(c *gin.Context) {
|
||||
"success": "",
|
||||
})
|
||||
|
||||
c.HTML(http.StatusOK, "cart.html", data)
|
||||
c.HTML(http.StatusOK, "error.html", data)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user