Commit beb80a05 authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

추천기능 controller

parent 02b3814f
......@@ -303,6 +303,7 @@ function Payment({ match, location }) {
<div className="my-1 pt-2 border-top font-weight-bold">
결제금액<span className="float-right">{finalPrice + 2500}</span>
</div>
</div>
<div>
<h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>결제수단</h5>
<div className="text-center m-3">
......@@ -314,11 +315,6 @@ function Payment({ match, location }) {
<div className="text-center">
<Button type="button" onClick={paymentCompleted} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} block>결제완료</Button>
</div>
{paymentWay}
</div>
<div className="text-center">
<Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} onClick={paymentCompleted} block>결제완료</Button>
</div>
</Container>
)
}
......
......@@ -25,8 +25,21 @@ function Product({ match, location }) {
pushOptions()
// console.log(cart)
}
getRecommend()
}, [size, color])
async function getRecommend(){
try {
const response = await axios.get(`/api/order/recommend?products=${product.id}`)
// const response = await axios.post(`/api/order/recommend`,{
// productId: product.id
// })
console.log(response.data)
} catch (error) {
catchErrors(error,setError)
}
}
function handleClick(e) {
const box = e.target.parentNode.parentNode
box.style.display = "none"
......@@ -142,6 +155,7 @@ function Product({ match, location }) {
return (
<div>
{console.log(product)}
<style type="text/css">
{`
.btn {
......
......@@ -20,6 +20,7 @@ const changeCart = async (req, res) => {
console.log(products)
try {
const cart = await Cart.findOne({ userId: userId })
console.log(cart)
await Cart.updateOne(
{ _id: cart._id },
{ $set: { products: products } }
......@@ -38,7 +39,7 @@ const showCart = async (req, res) => {
model: 'Product'
})
res.status(200).json(cart.products)
console.log("cart-products : ", cart.products);
console.log("cart-products : ", cart);
} catch (error) {
console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
console.log(error)
......
import Order from "../schemas/Order.js";
import User from "../schemas/User.js";
import mongoose from 'mongoose'
import Product from "../schemas/Product.js";
const addorder = async (req, res) => {
const { userId, products, receiverInfo, total } = req.body
......@@ -17,8 +19,8 @@ const addorder = async (req, res) => {
const Ordered = async (req, res) => {
const { db } = req.body
try {
const ordered = await req.body.findOne({}, { _id: 0}).select(`${db}`)
console.log("sub= ",ordered);
const ordered = await req.body.findOne({}, { _id: 0 }).select(`${db}`)
console.log("sub= ", ordered);
res.json(ordered);
} catch (error) {
res.status(500).send('카테고리를 불러오지 못했습니다.')
......@@ -27,7 +29,7 @@ const Ordered = async (req, res) => {
const showorder = async (req, res) => {
try {
const order = await Order.find({ userId: req.userId }).sort({_id:-1}).limit(1).populate({
const order = await Order.find({ userId: req.userId }).sort({ _id: -1 }).limit(1).populate({
path: 'products.productId',
model: 'Product'
})
......@@ -54,4 +56,46 @@ const orderById = async (req, res, next, id) => {
}
}
export default { addorder, showorder, orderById , Ordered }
\ No newline at end of file
const recommendPro = async (req, res) => {
const productId = req.query.products
try {
const recommend = await Order.aggregate([
{
$match: {
'products.productId': mongoose.Types.ObjectId(productId)
}
},
{ "$unwind": "$products" },
{
$group: {
_id: "$products.productId",
count: { $sum: 1 }
}
}
])
console.log('recommend=', recommend)
const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId))
console.log('filtering=', filteredRecommend)
filteredRecommend.sort(function (a, b) {
if (a.count > b.count) {
return -1;
}
if (a.count < b.count) {
return 1;
}
// a must be equal to b
return 0;
});
const array = filteredRecommend.map(async (el) => {
const aa = await Product.findById(el._id)
return aa
})
const bb = await Promise.all(array)
res.json(bb)
} catch (error) {
console.log('error in order ', error)
}
}
export default { addorder, showorder, orderById, Ordered, recommendPro }
\ No newline at end of file
......@@ -8,6 +8,9 @@ router.route('/addorder')
.post(orderCtrl.addorder)
// .get()
router.route('/recommend')
.get(orderCtrl.recommendPro)
router.route('/showorder/:userId')
.get(orderCtrl.showorder)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment