Commit 9e604666 authored by 이재연's avatar 이재연
Browse files

Merge remote-tracking branch 'origin/jiwon' into jaeyeon

parents d8de2d3f 506fb316
......@@ -8,8 +8,7 @@ const login = async (req, res) => {
console.log(req.body)
try {
const user = await User.findOne({role:"user",id:id}).select('password name')
const user = await User.findOne({ id }).select('password role name tel email')
console.log('u=', user)
if (!user) {
return res.status(404).send(`${user.id}가 존재하지 않습니다.`)
......@@ -24,7 +23,7 @@ const login = async (req, res) => {
httpOnly: true,
secure: config.env === 'production'
})
res.json({ userId: user._id, role: user.role, name: user.name })
res.json({ userId: user._id, role: user.role, name: user.name, tel: user.tel, email:user.email })
} else {
res.status(401).send('비밀번호가 일치하지 않습니다.')
......
import Cart from "../schemas/Cart.js";
const addcart = async (req, res) => {
const { userId, products} = req.body
const addCart = async (req, res) => {
const { userId, products } = req.body
try {
const cart = await Cart.findOne({ userId: userId })
await Cart.updateOne(
{ _id: cart._id },
{$set: {products: products}}
{ $push: { products: products } }
)
res.status(200).send('카트에 저장되었습니다.')
} catch (error) {
......@@ -15,7 +15,22 @@ const addcart = async (req, res) => {
}
}
const showcart = async (req, res) => {
const changeCart = async (req, res) => {
const { userId, products } = req.body
console.log(products)
try {
const cart = await Cart.findOne({ userId: userId })
await Cart.updateOne(
{ _id: cart._id },
{ $set: { products: products } }
)
res.send("카트에 체크가 활성화되었습니다")
} catch (error) {
res.send("카트 체인지 실패")
}
}
const showCart = async (req, res) => {
try {
const cart = await Cart.findOne({ userId: req.id }).populate({
path: 'products.productId',
......@@ -28,18 +43,48 @@ const showcart = async (req, res) => {
}
}
const deletecart = async (req, res) => {
const deleteCart = async (req, res) => {
console.log(req.body)
const { cartId } = req.body
const { userId, cartId } = req.body
try {
await Cart.deleteOne({ _id: cartId })
res.send("삭제완료")
const cart = await Cart.findOneAndUpdate(
{ userId: userId },
{ $pull: { products: { _id: cartId } } },
{ new: true }
).populate({
path: 'products.productId',
model: 'Product'
})
res.json(cart)
} catch (error) {
console.log(error)
res.status(500).send('해당 카트를 삭제하지 못했습니다.')
}
}
const deleteCart2 = async (req, res) => {
console.log(req.body)
const { userId, cartId } = req.body
try {
for( let i = 0; i < cartId.length; i++ ){
await Cart.findOneAndUpdate(
{ userId: userId },
{ $pull: { products: { _id: cartId[i] } } },
{ new: true }
).populate({
path: 'products.productId',
model: 'Product'
})
}
res.send("주문완료 및 쇼핑카트에서 삭제")
// res.json(cart)
} catch (error) {
console.log(error)
res.status(500).send('해당 카트를 삭제하지 못했습니다.')
}
}
const userById = async (req, res, next, id) => {
try {
const cart = await Cart.findOne({ userId: id })
......@@ -55,4 +100,4 @@ const userById = async (req, res, next, id) => {
}
export default { addcart, showcart, deletecart, userById }
\ No newline at end of file
export default { addCart, changeCart, showCart, deleteCart,deleteCart2, userById }
\ No newline at end of file
import Category from "../schemas/Category.js";
const getCategory = async (req, res) => {
console.log("dsadd=")
try {
const category = await Category.find({}, {_id: 0})
const category = await Category.find({}, { _id: 0 })
// console.log("main= ", category);
res.json(category)
} catch (error) {
console.log(error)
......@@ -11,15 +11,60 @@ const getCategory = async (req, res) => {
}
}
// const getSubCategory=(req,res)=>{
const getSubCategory = async (req, res) => {
// console.log("req.params=", req.params);
const { sub } = req.params
try {
const subcategory = await Category.findOne({}, { _id: 0}).select(`${sub}`)
// console.log("sub= ",subcategory);
res.json(subcategory);
} catch (error) {
res.status(500).send('카테고리를 불러오지 못했습니다.')
}
}
// }
const getToHome = async (res, req) => {
try {
const bestProduct = await Product.find({}).sort({ purchase: 1 }).limit(6)
const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
// console.log("best=", bestProduct)
// console.log("new=", newProduct)
res.json(bestProduct, newProduct)
} catch {
res.status(500).send('상품을 불러오지 못했습니다.')
}
}
// const getsubId=(req,res,next,sub)=>{
// console.log('sub=',sub)
const getsubId = async (req, res, next, ele) => {
try {
const sub = await Category.find({ele})
if (!sub) {
res.status(404).send('카테고리가 존재하지 않습니다.')
} req.category = sub
req.subcategory = sub
next()
}
catch (error) {
console.log(error);
res.status(500).send('카테고리를 불러오지 못했습니다.')
}
next()
}
// const userById = async (req, res, next, id) => {
// try {
// const user = await User.findById(id)
// if (!user) {
// res.status(404).send('사용자를 찾을 수 없습니다')
// }
// req.account = user
// next()
// } catch (error) {
// console.log(error);
// res.status(500).send('사용자 아이디 검색 실패')
// }
// }
export default { getCategory }
\ No newline at end of file
export default { getCategory, getsubId, getSubCategory, getToHome }
import Order from "../schemas/Order.js";
import User from "../schemas/User.js";
const addorder = async (req, res) => {
const { userId, products, receiverInfo, total } = req.body
try {
const newOrder = await new Order({
userId, products, receiverInfo, total
}).save()
res.status(200).send('Order DB에 저장 완료')
} catch (error) {
console.log(error)
res.status(500).send('Order DB에 저장 실패')
}
}
const showorder = async (req, res) => {
try {
const order = await Order.find({ userId: req.userId }).sort({_id:-1}).limit(1).populate({
path: 'products.productId',
model: 'Product'
})
console.log(order)
res.status(200).json(order[0])
} catch (error) {
console.log(error)
res.status(500).send('쇼핑카트를 불러오지 못했습니다.')
}
}
const orderById = async (req, res, next, id) => {
try {
const user = await User.findById(id)
if (!user) {
res.status(404).send('사용자를 찾을 수 없습니다')
}
req.userId = user
next()
} catch (error) {
console.log(error);
res.status(500).send('사용자 아이디 검색 실패')
}
}
export default { addorder, showorder, orderById }
\ No newline at end of file
......@@ -29,29 +29,25 @@ const regist = async (req, res) => {
}
}
const getToHome = async (res, req) => {
const getToHome = async (req, res) => {
try {
const bestProduct = await Product.find({}).sort({ purchase: 1 }).limit(6)
const bestProduct = await Product.find({}).sort({ purchase: -1 }).limit(6)
const newProduct = await Product.find({}).sort({ createdAt: -1 }).limit(6)
console.log("best=", bestProduct)
console.log("new=", newProduct)
res.json(bestProduct, newProduct)
// console.log("best=", bestProduct)
// console.log("new=", newProduct)
res.json({ bestProduct, newProduct })
} catch {
res.status(500).send('상품을 불러오지 못했습니다.')
}
}
const Sortlist = async (res, req) => {
const getAll = async (req, res) => {
try {
const newlist = await Product.find({}).sort({ createdAt: -1 })
const bestlist = await Product.find({}).sort({ purchase: 1 })
console.log('bestsort',bestlist)
console.log('newlist',newlist)
res.json(newlist, bestlist)
} catch {
const productslist = await Product.find({}).sort({ createdAt: -1 })
res.json(productslist)
} catch (error) {
res.status(500).send('상품을 불러오지 못했습니다.')
}
}
const getlist = (req, res) => {
......@@ -62,7 +58,6 @@ const getlist = (req, res) => {
}
}
const categoryId = async (req, res, next, category) => {
try {
const productslist = await Product.find({ main_category: category })
......@@ -83,6 +78,7 @@ const subgetlist = (req, res) => {
res.status(500).send('상품을 불러오지 못했습니다.')
}
}
const subcategoryId = async (req, res, next, subcategory) => {
try {
const subproductslist = await Product.find({ sub_category: subcategory })
......@@ -96,4 +92,32 @@ const subcategoryId = async (req, res, next, subcategory) => {
}
}
export default { imageUpload, regist, categoryId, getlist, subcategoryId, subgetlist, getToHome , Sortlist}
const plusPurchase = async (req, res) => {
const { products } = req.body
// console.log(products)
try {
for (let i = 0; i < products.length; i++) {
const count = products[i].count
const product = await Product.findOne(
{ _id: products[i].productId._id }
)
const purchase = product.purchase
const stock = product.stock
await Product.updateOne(
{ _id: products[i].productId._id },
{ $set:
{
purchase: count + purchase,
stock: stock - count
}
}
)
// console.log("i=", i)
}
res.send("구매수 늘리기, 재고수 줄이기 성공")
} catch (error) {
res.status(500).send('구매숫자를 늘리지 못함')
}
}
export default { imageUpload, regist, getToHome, getAll, categoryId, getlist, subcategoryId, subgetlist, plusPurchase }
......@@ -31,10 +31,8 @@ const userById = async (req, res, next, id) => {
const signup = async (req, res) => {
const { name, number1, number2, id, password, tel } = req.body
console.log(req.body)
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자리로 입력해주세요.')
......@@ -53,8 +51,9 @@ const signup = async (req, res) => {
id,
password: hash,
tel,
email
}).save()
await new Cart({ userId: newUser._id,role}).save()
await new Cart({ userId: newUser._id }).save()
console.log(newUser)
res.json(newUser)
......
......@@ -5,15 +5,20 @@ import cartCtrl from '../controllers/cart.controller.js';
const router = express.Router()
router.route('/addcart')
.put(cartCtrl.addcart)
.put(cartCtrl.addCart)
// .get()
router.route('/showcart/:userId')
.get(cartCtrl.showcart)
router.param('userId', cartCtrl.userById)
.get(cartCtrl.showCart)
router.route('/changecart')
.post(cartCtrl.changeCart)
router.route('/deletecart')
.post(cartCtrl.deletecart)
.post(cartCtrl.deleteCart)
router.route('/deletecart2')
.post(cartCtrl.deleteCart2)
router.param('userId', cartCtrl.userById)
export default router
\ No newline at end of file
import express from "express";
import orderCtrl from '../controllers/order.controller.js';
const router = express.Router()
router.route('/addorder')
.post(orderCtrl.addorder)
// .get()
router.route('/showorder/:userId')
.get(orderCtrl.showorder)
router.param('userId', orderCtrl.orderById)
export default router
\ No newline at end of file
......@@ -13,14 +13,19 @@ router.route('/regist')
router.route('/getproduct')
.get(productCtrl.getToHome)
router.route('/getproduct/all')
.get(productCtrl.getAll)
router.route('/getproduct/:category')
.get(productCtrl.getlist)
router.route('/getproduct/:subcategory')
.get(productCtrl.subgetlist)
router.param('category', productCtrl.categoryId)
router.route('/pluspurchase')
.post(productCtrl.plusPurchase)
router.param('category', productCtrl.categoryId)
router.param('subcategory',productCtrl.subcategoryId)
export default router
\ No newline at end of file
......@@ -10,8 +10,6 @@ router.route('/account/:userId')
.get(userCtrl.username)
.put(userCtrl.imgUpload, userCtrl.update)
router.param('userId', userCtrl.userById)
export default router
\ No newline at end of file
......@@ -17,11 +17,14 @@ const CartSchema = new mongoose.Schema({
type: ObjectId,
ref: 'Product'
},
sizes: {
size: {
type: String
},
colors: {
color: {
type: String
},
checked: {
type: Boolean
}
}
]
......
import mongoose from 'mongoose'
const { ObjectId, Number, String } = mongoose.Schema.Types
const OrderSchema = new mongoose.Schema({
userId: {
type: ObjectId,
ref: 'User'
},
products: [
{
productId: {
type: ObjectId,
ref: 'Product'
},
count: {
type: Number,
required: true
},
size: {
type: String,
required: true
},
color: {
type: String,
required: true
},
checked: {
type: Boolean
}
}
],
receiverInfo:
{
name: {
type: String,
required: true
},
tel: {
type: String,
required: true
},
postalCode: {
type: String,
required: true
},
address: {
type: String,
required: true
},
address2: {
type: String,
required: true
}
}
,
total: {
type: Number,
required: true
}
}, {
timestamps: true
})
export default mongoose.models.Order || mongoose.model('Order', OrderSchema)
\ No newline at end of file
import mongoose from 'mongoose'
const { String, Number } = mongoose.Schema.Types
const { String, Array, Number } = mongoose.Schema.Types
const ProductSchema = new mongoose.Schema({
pro_name: {
......@@ -21,11 +21,11 @@ const ProductSchema = new mongoose.Schema({
default: 0
},
sizes: {
type: Array,
type: [String],
required: true
},
colors: {
type: Array,
type: [String],
required: true
},
main_category: {
......@@ -33,7 +33,7 @@ const ProductSchema = new mongoose.Schema({
required: true,
},
sub_category: {
type: [String],
type: Array,
required: true,
},
description: {
......@@ -45,7 +45,7 @@ const ProductSchema = new mongoose.Schema({
required: true
},
detail_imgUrls: {
type: [String]
type: Array
}
}, {
timestamps: true
......
......@@ -34,6 +34,10 @@ const UserSchema = new mongoose.Schema({
default: 'user',
enum: ['user', 'admin', 'root']
},
email : {
type : String,
required: true,
},
avatarUrl: {
type: String
}
......
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