① 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');
};
'쇼핑몰 구현 프로젝트' 카테고리의 다른 글
[쇼핑몰 구현 프로젝트] 09. 사용자 관련 기능 (0) | 2024.06.23 |
---|---|
[쇼핑몰 구현 프로젝트] 08. 리뷰 관련 기능 (0) | 2024.06.23 |
[쇼핑몰 구현 프로젝트] 06. 상품 관련 기능 (0) | 2024.06.23 |
[쇼핑몰 구현 프로젝트] 05. 회원가입/로그인 (0) | 2024.06.23 |
[쇼핑몰 구현 프로젝트] 04. Backend, Database 틀잡기 (0) | 2024.06.22 |