154 lines
3.1 KiB
Go
154 lines
3.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
database "app/database"
|
|
"app/models"
|
|
"app/repositories"
|
|
"app/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func initCartItems() *repositories.Cart_items_repository {
|
|
db := database.Db()
|
|
cartRepository := new(repositories.Cart_items_repository)
|
|
cartRepository.DB = &db
|
|
|
|
return cartRepository
|
|
}
|
|
|
|
func Create_cart_item(c echo.Context) error {
|
|
|
|
db := database.Db()
|
|
|
|
// get user's coockie
|
|
cookie, err := c.Cookie("authorization")
|
|
if err != nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
// parse jwt token
|
|
token, err := utils.ParseToken(cookie.Value)
|
|
if err != nil {
|
|
return echo.ErrUnauthorized
|
|
}
|
|
// convert jwt content to int
|
|
jwtID, err := strconv.Atoi(token)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// check if user exists and return userID
|
|
userID, _, err := utils.CheckUserByJwt(uint(jwtID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cartRepository := new(repositories.Carts_repository)
|
|
cartRepository.DB = &db
|
|
|
|
cart, err := cartRepository.GetCart(userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cart_items := new(models.Cart_items)
|
|
|
|
cart_items.Cart_id = cart.Cart_id
|
|
//cart_items.Price = uint64(cart_items.Quantity) * cart_items.
|
|
|
|
if err := c.Bind(cart_items); err != nil {
|
|
return err
|
|
}
|
|
|
|
productRepository := new(repositories.Product_repository)
|
|
productRepository.DB = &db
|
|
product, err := productRepository.GetByID(cart_items.Product_id)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
count, err := utils.GetProductCount(int64(cart_items.Product_id), cart_items.Cart_id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cartItemRepository := initCartItems()
|
|
|
|
if count == 0 {
|
|
cart_items.Price = product.Price
|
|
cart_items.PricePerUnit = product.Price
|
|
err = cartItemRepository.Create(cart_items)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
var certainItem models.Cart_items
|
|
err := db.Where("product_id = ?", cart_items.Product_id).Where("cart_id = ?", cart.Cart_id).Where("status = ?", "pending").First(&certainItem).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if certainItem.Quantity == product.Quantity {
|
|
return c.String(http.StatusNotAcceptable, "limit reached")
|
|
}
|
|
if certainItem.Quantity < product.Quantity {
|
|
err = cartItemRepository.IncreaseQuantity(certainItem.Cart_item_id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, cart_items)
|
|
|
|
}
|
|
|
|
func GetCartItems(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cartRepository := initCartItems()
|
|
|
|
items, err := cartRepository.GetItems(uint(id), "pending")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.JSON(http.StatusAccepted, items)
|
|
}
|
|
|
|
func UpdateItemStatus(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cartRepository := initCartItems()
|
|
|
|
err = cartRepository.UpdateStatus(uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.String(http.StatusAccepted, "updated")
|
|
}
|
|
|
|
func Remove_cart_item(c echo.Context) error {
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cartRepository := initCartItems()
|
|
|
|
err = cartRepository.Remove(uint(id))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.String(http.StatusAccepted, "removed")
|
|
}
|