Commit 25a9bb54 authored by Jiwon Yoon's avatar Jiwon Yoon
Browse files

Merge branch 'ourMaster' into jiwon

parents 8d8a98af 221883bb
import mongoose from 'mongoose';
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"; 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
try { try {
const newOrder = await new Order({ const newOrder = await new Order({ userId, products, receiverInfo, total }).save()
userId, products, receiverInfo, total
}).save()
res.status(200).send('Order DB에 저장 완료') res.status(200).send('Order DB에 저장 완료')
} catch (error) { } catch (error) {
console.log(error) console.log(error)
...@@ -20,7 +18,6 @@ const Ordered = async (req, res) => { ...@@ -20,7 +18,6 @@ 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);
res.json(ordered); res.json(ordered);
} catch (error) { } catch (error) {
res.status(500).send('카테고리를 불러오지 못했습니다.') res.status(500).send('카테고리를 불러오지 못했습니다.')
...@@ -33,7 +30,6 @@ const showorder = async (req, res) => { ...@@ -33,7 +30,6 @@ const showorder = async (req, res) => {
path: 'products.productId', path: 'products.productId',
model: 'Product' model: 'Product'
}) })
console.log(order)
res.status(200).json(order[0]) res.status(200).json(order[0])
} catch (error) { } catch (error) {
console.log(error) console.log(error)
...@@ -41,7 +37,6 @@ const showorder = async (req, res) => { ...@@ -41,7 +37,6 @@ const showorder = async (req, res) => {
} }
} }
const orderById = async (req, res, next, id) => { const orderById = async (req, res, next, id) => {
try { try {
const user = await User.findById(id) const user = await User.findById(id)
...@@ -71,36 +66,32 @@ const recommendPro = async (req, res) => { ...@@ -71,36 +66,32 @@ const recommendPro = async (req, res) => {
_id: "$products.productId", _id: "$products.productId",
count: { $sum: 1 } count: { $sum: 1 }
} }
},
{
$sort: {
count: -1
}
},
{ $limit: 5 },
{
$lookup:
{
from: "products",
localField: "_id",
foreignField: "_id",
as: "product"
}
} }
]) ])
console.log('recommend=', recommend) console.log('recommend=', recommend)
const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId)) const filteredRecommend = recommend.filter((el) => String(el._id) !== String(productId))
console.log('filtering=', filteredRecommend) console.log('filtering=', filteredRecommend)
// const array = filteredRecommend.map(async (el) => { filteredRecommend.sort(function (a, b) {
// const aa = await Product.findById(el._id) if (a.count > b.count) {
// return aa return -1;
// }) }
// const bb = await Promise.all(array) if (a.count < b.count) {
res.json(filteredRecommend) return 1;
}
// a must be equal to b
return 0;
});
console.log('sort=',filteredRecommend)
const finalrecommend= filteredRecommend.slice(0, 4)
const array = finalrecommend.map(async (el) => {
const aa = await Product.findById(el._id)
return aa
})
const bb = await Promise.all(array)
res.json(bb)
} catch (error) { } catch (error) {
console.log('error in order ', error) console.log('error in order ', error)
} }
} }
export default { addorder, showorder, orderById, Ordered, recommendPro } export default { addorder, showorder, orderById, Ordered, recommendPro }
\ No newline at end of file
...@@ -5,7 +5,7 @@ const upload = multer({ dest: 'uploads/' }) ...@@ -5,7 +5,7 @@ const upload = multer({ dest: 'uploads/' })
const imageUpload = upload.fields([ const imageUpload = upload.fields([
{ name: 'main_image' }, { name: 'main_image' },
{ name: 'detail_image'} { name: 'detail_image' }
]) ])
const regist = async (req, res) => { const regist = async (req, res) => {
......
...@@ -13,7 +13,6 @@ const imgUpload = uploadimg.fields([ ...@@ -13,7 +13,6 @@ const imgUpload = uploadimg.fields([
const username = (req, res) => { const username = (req, res) => {
res.json(req.account) res.json(req.account)
console.log(req.account)
} }
const userById = async (req, res, next, id) => { const userById = async (req, res, next, id) => {
...@@ -30,10 +29,8 @@ const userById = async (req, res, next, id) => { ...@@ -30,10 +29,8 @@ const userById = async (req, res, next, id) => {
} }
} }
const signup = async (req, res) => { const signup = async (req, res) => {
const { name, number1, number2, id, password, tel, email } = req.body const { name, number1, number2, id, password, tel, email } = req.body
console.log("whatup", req.body)
try { try {
if (!isLength(password, { min: 8, max: 15 })) { if (!isLength(password, { min: 8, max: 15 })) {
return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.') return res.status(422).send('비밀번호는 8-15자리로 입력해주세요.')
...@@ -42,9 +39,7 @@ const signup = async (req, res) => { ...@@ -42,9 +39,7 @@ const signup = async (req, res) => {
if (user) { if (user) {
return res.status(422).send(`${id}가 이미 사용중입니다.`) return res.status(422).send(`${id}가 이미 사용중입니다.`)
} }
const hash = await bcrypt.hash(password, 10) const hash = await bcrypt.hash(password, 10)
const newUser = await new User({ const newUser = await new User({
name, name,
number1, number1,
...@@ -57,7 +52,6 @@ const signup = async (req, res) => { ...@@ -57,7 +52,6 @@ const signup = async (req, res) => {
await new Cart({ userId: newUser._id }).save() await new Cart({ userId: newUser._id }).save()
console.log(newUser) console.log(newUser)
res.json(newUser) res.json(newUser)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
res.status(500).send('죄송합니다. 다시 입력해 주십시오.') res.status(500).send('죄송합니다. 다시 입력해 주십시오.')
...@@ -65,7 +59,6 @@ const signup = async (req, res) => { ...@@ -65,7 +59,6 @@ const signup = async (req, res) => {
} }
const update = async (req, res) => { const update = async (req, res) => {
console.log("req", req.body)
try { try {
if (req.body.avatar == '') { if (req.body.avatar == '') {
const user = req.account const user = req.account
...@@ -79,7 +72,6 @@ const update = async (req, res) => { ...@@ -79,7 +72,6 @@ const update = async (req, res) => {
const updateUser = await user.save() const updateUser = await user.save()
res.json(updateUser) res.json(updateUser)
} }
} catch (error) { } catch (error) {
console.log(error); console.log(error);
res.status(500).send('이미지 업데이트 실패') res.status(500).send('이미지 업데이트 실패')
...@@ -87,13 +79,12 @@ const update = async (req, res) => { ...@@ -87,13 +79,12 @@ const update = async (req, res) => {
} }
const addorder = async (req, res) => { const addorder = async (req, res) => {
const {userId}=req.body const { userId } = req.body
try { try {
const order = await Order.find({ userId: userId }).populate({ const order = await Order.find({ userId: userId }).populate({
path: 'products.productId', path: 'products.productId',
model: 'Product' model: 'Product'
}) })
console.log("hey", order)
res.status(200).json(order) res.status(200).json(order)
} catch (error) { } catch (error) {
console.log(error) console.log(error)
...@@ -101,5 +92,4 @@ const addorder = async (req, res) => { ...@@ -101,5 +92,4 @@ const addorder = async (req, res) => {
} }
} }
export default { signup, username, imgUpload, userById, update, addorder }
export default { signup, username, imgUpload, userById,update, addorder } \ No newline at end of file
\ No newline at end of file
import express from "express"; import express from "express";
import cartCtrl from '../controllers/cart.controller.js'; import cartCtrl from '../controllers/cart.controller.js';
const router = express.Router() const router = express.Router()
router.route('/addcart') router.route('/addcart')
.put(cartCtrl.addCart) .put(cartCtrl.addCart)
// .get()
router.route('/showcart/:userId') router.route('/showcart/:userId')
.get(cartCtrl.showCart) .get(cartCtrl.showCart)
......
import express from "express"; import express from "express";
import orderCtrl from '../controllers/order.controller.js'; import orderCtrl from '../controllers/order.controller.js';
const router = express.Router() const router = express.Router()
router.route('/addorder') router.route('/addorder')
.post(orderCtrl.addorder) .post(orderCtrl.addorder)
// .get()
router.route('/recommend') router.route('/recommend')
.get(orderCtrl.recommendPro) .get(orderCtrl.recommendPro)
...@@ -14,6 +12,9 @@ router.route('/recommend') ...@@ -14,6 +12,9 @@ router.route('/recommend')
router.route('/showorder/:userId') router.route('/showorder/:userId')
.get(orderCtrl.showorder) .get(orderCtrl.showorder)
router.route('/recommend')
.post(orderCtrl.recommendPro)
router.param('userId', orderCtrl.orderById) router.param('userId', orderCtrl.orderById)
export default router export default router
\ No newline at end of file
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