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