veza/veza-backend-api/internal/handlers/marketplace_handler.go

200 lines
5.1 KiB
Go

package handlers
import (
"errors"
"net/http"
"veza-backend-api/internal/core/marketplace"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"go.uber.org/zap"
)
// MarketplaceExtHandler handles wishlist and cart operations
type MarketplaceExtHandler struct {
service *marketplace.Service
commonHandler *CommonHandler
}
// NewMarketplaceExtHandler creates a new MarketplaceExtHandler
func NewMarketplaceExtHandler(service *marketplace.Service, logger *zap.Logger) *MarketplaceExtHandler {
return &MarketplaceExtHandler{
service: service,
commonHandler: NewCommonHandler(logger),
}
}
// --- Wishlist handlers ---
// GetWishlist returns the authenticated user's wishlist
func (h *MarketplaceExtHandler) GetWishlist(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
items, err := h.service.GetWishlist(c.Request.Context(), userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get wishlist"})
return
}
RespondSuccess(c, http.StatusOK, gin.H{"items": items})
}
// AddToWishlistRequest is the DTO for adding to wishlist
type AddToWishlistRequest struct {
ProductID string `json:"product_id" binding:"required" validate:"required,uuid"`
}
// AddToWishlist adds a product to the user's wishlist
func (h *MarketplaceExtHandler) AddToWishlist(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
var req AddToWishlistRequest
if appErr := h.commonHandler.BindAndValidateJSON(c, &req); appErr != nil {
RespondWithAppError(c, appErr)
return
}
productID, err := uuid.Parse(req.ProductID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
return
}
item, err := h.service.AddToWishlist(c.Request.Context(), userID, productID)
if err != nil {
if errors.Is(err, marketplace.ErrProductNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add to wishlist"})
return
}
RespondSuccess(c, http.StatusCreated, item)
}
// RemoveFromWishlist removes a product from the user's wishlist
func (h *MarketplaceExtHandler) RemoveFromWishlist(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
productID, err := uuid.Parse(c.Param("productId"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
return
}
if err := h.service.RemoveFromWishlist(c.Request.Context(), userID, productID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to remove from wishlist"})
return
}
RespondSuccess(c, http.StatusOK, gin.H{"message": "Removed from wishlist"})
}
// --- Cart handlers ---
// GetCart returns the authenticated user's cart
func (h *MarketplaceExtHandler) GetCart(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
items, err := h.service.GetCart(c.Request.Context(), userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get cart"})
return
}
RespondSuccess(c, http.StatusOK, gin.H{"items": items})
}
// AddToCartRequest is the DTO for adding to cart
type AddToCartRequest struct {
ProductID string `json:"product_id" binding:"required" validate:"required,uuid"`
Quantity int `json:"quantity"`
}
// AddToCart adds a product to the user's cart
func (h *MarketplaceExtHandler) AddToCart(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
var req AddToCartRequest
if appErr := h.commonHandler.BindAndValidateJSON(c, &req); appErr != nil {
RespondWithAppError(c, appErr)
return
}
productID, err := uuid.Parse(req.ProductID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"})
return
}
quantity := req.Quantity
if quantity < 1 {
quantity = 1
}
item, err := h.service.AddToCart(c.Request.Context(), userID, productID, quantity)
if err != nil {
if errors.Is(err, marketplace.ErrProductNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "Product not found"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add to cart"})
return
}
RespondSuccess(c, http.StatusCreated, item)
}
// RemoveFromCart removes an item from the user's cart
func (h *MarketplaceExtHandler) RemoveFromCart(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
itemID, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid item ID"})
return
}
if err := h.service.RemoveFromCart(c.Request.Context(), userID, itemID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to remove from cart"})
return
}
RespondSuccess(c, http.StatusOK, gin.H{"message": "Removed from cart"})
}
// Checkout converts the user's cart into an order
func (h *MarketplaceExtHandler) Checkout(c *gin.Context) {
userID, ok := GetUserIDUUID(c)
if !ok {
return
}
resp, err := h.service.Checkout(c.Request.Context(), userID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Checkout failed: " + err.Error()})
return
}
RespondSuccess(c, http.StatusCreated, resp)
}