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

추천기능 controller

parent 02b3814f
...@@ -303,6 +303,7 @@ function Payment({ match, location }) { ...@@ -303,6 +303,7 @@ function Payment({ match, location }) {
<div className="my-1 pt-2 border-top font-weight-bold"> <div className="my-1 pt-2 border-top font-weight-bold">
결제금액<span className="float-right">{finalPrice + 2500}</span> 결제금액<span className="float-right">{finalPrice + 2500}</span>
</div> </div>
</div>
<div> <div>
<h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>결제수단</h5> <h5 className="font-weight-bold py-3 border-top border-bottom text-center" style={{ background: '#F7F3F3' }}>결제수단</h5>
<div className="text-center m-3"> <div className="text-center m-3">
...@@ -314,11 +315,6 @@ function Payment({ match, location }) { ...@@ -314,11 +315,6 @@ function Payment({ match, location }) {
<div className="text-center"> <div className="text-center">
<Button type="button" onClick={paymentCompleted} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} block>결제완료</Button> <Button type="button" onClick={paymentCompleted} className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} block>결제완료</Button>
</div> </div>
{paymentWay}
</div>
<div className="text-center">
<Button className="px-5" style={{ background: "#91877F", borderColor: '#91877F' }} onClick={paymentCompleted} block>결제완료</Button>
</div>
</Container> </Container>
) )
} }
......
...@@ -25,8 +25,21 @@ function Product({ match, location }) { ...@@ -25,8 +25,21 @@ function Product({ match, location }) {
pushOptions() pushOptions()
// console.log(cart) // console.log(cart)
} }
getRecommend()
}, [size, color]) }, [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) { function handleClick(e) {
const box = e.target.parentNode.parentNode const box = e.target.parentNode.parentNode
box.style.display = "none" box.style.display = "none"
...@@ -142,6 +155,7 @@ function Product({ match, location }) { ...@@ -142,6 +155,7 @@ function Product({ match, location }) {
return ( return (
<div> <div>
{console.log(product)}
<style type="text/css"> <style type="text/css">
{` {`
.btn { .btn {
......
...@@ -20,6 +20,7 @@ const changeCart = async (req, res) => { ...@@ -20,6 +20,7 @@ const changeCart = async (req, res) => {
console.log(products) console.log(products)
try { try {
const cart = await Cart.findOne({ userId: userId }) const cart = await Cart.findOne({ userId: userId })
console.log(cart)
await Cart.updateOne( await Cart.updateOne(
{ _id: cart._id }, { _id: cart._id },
{ $set: { products: products } } { $set: { products: products } }
...@@ -38,7 +39,7 @@ const showCart = async (req, res) => { ...@@ -38,7 +39,7 @@ const showCart = async (req, res) => {
model: 'Product' model: 'Product'
}) })
res.status(200).json(cart.products) res.status(200).json(cart.products)
console.log("cart-products : ", cart.products); console.log("cart-products : ", cart);
} catch (error) { } catch (error) {
console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
console.log(error) console.log(error)
......
import Order from "../schemas/Order.js"; import Order from "../schemas/Order.js";
import User from "../schemas/User.js"; import User from "../schemas/User.js";
import mongoose from 'mongoose'
import Product from "../schemas/Product.js";
const addorder = async (req, res) => { const addorder = async (req, res) => {
const { userId, products, receiverInfo, total } = req.body const { userId, products, receiverInfo, total } = req.body
...@@ -17,8 +19,8 @@ const addorder = async (req, res) => { ...@@ -17,8 +19,8 @@ const addorder = async (req, res) => {
const Ordered = async (req, res) => { const Ordered = async (req, res) => {
const { db } = req.body const { db } = req.body
try { try {
const ordered = await req.body.findOne({}, { _id: 0}).select(`${db}`) const ordered = await req.body.findOne({}, { _id: 0 }).select(`${db}`)
console.log("sub= ",ordered); console.log("sub= ", ordered);
res.json(ordered); res.json(ordered);
} catch (error) { } catch (error) {
res.status(500).send('카테고리를 불러오지 못했습니다.') res.status(500).send('카테고리를 불러오지 못했습니다.')
...@@ -27,7 +29,7 @@ const Ordered = async (req, res) => { ...@@ -27,7 +29,7 @@ const Ordered = async (req, res) => {
const showorder = async (req, res) => { const showorder = async (req, res) => {
try { 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', path: 'products.productId',
model: 'Product' model: 'Product'
}) })
...@@ -54,4 +56,46 @@ const orderById = async (req, res, next, id) => { ...@@ -54,4 +56,46 @@ const orderById = async (req, res, next, id) => {
} }
} }
export default { addorder, showorder, orderById , Ordered } const recommendPro = async (req, res) => {
\ No newline at end of file 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') ...@@ -8,6 +8,9 @@ router.route('/addorder')
.post(orderCtrl.addorder) .post(orderCtrl.addorder)
// .get() // .get()
router.route('/recommend')
.get(orderCtrl.recommendPro)
router.route('/showorder/:userId') router.route('/showorder/:userId')
.get(orderCtrl.showorder) .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