쇼핑몰 구현 프로젝트

[쇼핑몰 구현 프로젝트] 07. 장바구니 관련 기능

binning 2024. 6. 23. 15:45

① Frontend

- 전체 삭제는 Cart페이지에, 개별 삭제는 props Component에 구현

  const deleteAllFromCart = async (evt) => {
    evt.preventDefault();
    try {
      const responseData = 
      await sendRequest(
        `${process.env.REACT_APP_BACKEND_URL}/carts/${userId}`,
        'DELETE'
      );
      alert("Delete All from a cart successfully");
      window.location.reload();
    } catch (err) {
      setIsError(true);
      setErrorMessage(err.message);
    }
  };
const deleteFromCart = async (evt) => {
    evt.preventDefault();
    try {
      await sendRequest(
        `${process.env.REACT_APP_BACKEND_URL}/carts/${userId}/${props.id}`,
        'DELETE'
      );
    } catch (err) {
      setIsError(true);
      setErrorMessage(err.message);
    }
    alert("Delete from a cart successfully");
    window.location.reload();
  };

 

② Backend

- Controller

const putInCart = async (req, res, next) => {
  const userId = req.params.uid;
  const productId = req.params.pid;
  
  let product;
  try {
    product = await Product.findById(productId);
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }
  
  if (!product) {
    const error = new Error('can not find product');
    error.code = 404;
    return next(error);
  }
  
  let user;
  try {
    user = await User.findById(userId);
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }
  
  if (!user) {
    const error = new Error('can not find user');
    error.code = 404;
    return next(error);
  }
  
  if(user.cart.includes(product.id)) {
    const error = new Error('already put in cart');
    error.code = 422;
    return next(error);
  }

  try {
    user.cart.push(product);
    await user.save();
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }

  res.status(200).json('Put in Cart');
};

const deleteFromCart = async (req, res, next) => {
  const userId = req.params.uid;
  const productId = req.params.pid;
  
  let product;
  try {
    product = await Product.findById(productId);
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }
  
  if (!product) {
    const error = new Error('can not find product');
    error.code = 404;
    return next(error);
  }
  
  let user;
  try {
    user = await User.findById(userId);
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }
  
  if (!user) {
    const error = new Error('can not find user');
    error.code = 404;
    return next(error);
  }
  
  try {
    user.cart.pull(product);
    await user.save();
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }

  res.status(200).json('Delete from Cart');
};

const deleteAllFromCart = async (req, res, next) => {
  const userId = req.params.uid;
  
  let user;
  try {
    user = await User.findById(userId);
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }
  
  if (!user) {
    const error = new Error('can not find user');
    error.code = 404;
    return next(error);
  }
  
  try {
    user.cart = [];
    await user.save();
  } catch (err) {
    const error = new Error('something went wrong');
    error.code = 500;
    return next(error);
  }

  res.status(200).json('Delete All from Cart');
};