Skip to content

Commit 9a9b5b2

Browse files
committed
backend
1 parent 044ce90 commit 9a9b5b2

File tree

5 files changed

+150
-74
lines changed

5 files changed

+150
-74
lines changed

models/socket.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ const chatSchema = new mongoose.Schema({
99
});
1010
mongoose.models = {};
1111

12-
module.exports = mongoose.model("QAs",chatSchema);
12+
module.exports = mongoose.model("qas",chatSchema);

pages/api/cart/[id].js

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import connectDb from "../../../middleware/db";
2-
import cart from "../../../models/cart";
2+
import carts from "../../../models/cart";
3+
import mongoose from "mongoose";
34

45
const handler = async (req, res) => {
56
if (req.method === "GET") {
7+
if(!mongoose.connections[0].readyState){
8+
await mongoose.connection(`mongodb+srv://umangar34:[email protected]/prosquad?retryWrites=true&w=majority`)
9+
}
610
// GETTING CART COURSES FOR USERS BY THEIR USER ID
711
const { id } = req.query;
812
try {
9-
const cart_courses = await cart
10-
.find({ userID: id })
11-
.populate({ path: "courseID" });
12-
if (cart_courses) {
13+
const cart_courses = await carts.find({ userID: id }).populate({ path: "courseID", model: "courses" });
14+
console.log(cart_courses);
15+
if (cart_courses.length>1) {
1316
var total = 0;
1417
for (var i = 0; i < cart_courses.length; i++) {
1518
total += cart_courses[i].courseID.selling_price;
@@ -18,15 +21,27 @@ const handler = async (req, res) => {
1821
cart: cart_courses,
1922
total_payment: total,
2023
});
21-
} else {
22-
res.status(404).json({
23-
message: "This userID doesn't exist",
24-
});
25-
}
24+
}
25+
// res.status(404).json({
26+
// message: "This userID doesn't exist",
27+
// });
28+
// }
2629
} catch (error) {
27-
res.status(404).json({
28-
error: "Please correct enter id in params",
29-
});
30+
console.log(error);
31+
if(!mongoose.connections[0].readyState){
32+
await mongoose.connection(`mongodb+srv://umangar34:[email protected]/prosquad?retryWrites=true&w=majority`)
33+
}
34+
const cart_courses = await carts.find({ userID: id }).populate({ path: "courseID", model: "courses" });
35+
console.log(cart_courses);
36+
if (cart_courses.length>1) {
37+
var total = 0;
38+
for (var i = 0; i < cart_courses.length; i++) {
39+
total += cart_courses[i].courseID.selling_price;
40+
}
41+
res.status(200).json({
42+
cart: cart_courses,
43+
total_payment: total,
44+
});}
3045
}
3146
}
3247

@@ -35,7 +50,7 @@ const handler = async (req, res) => {
3550

3651
try {
3752
const { id } = req.query;
38-
const courses = await cart.findByIdAndDelete({ _id: id });
53+
const courses = await carts.findByIdAndDelete({ _id: id });
3954
res.status(202).json({
4055
message: "The course is deleted successfully",
4156
});

pages/cart.js

+116-55
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,132 @@
11
import {
22
Box,
33
Button,
4+
Flex,
45
FormControl,
56
FormLabel,
7+
Heading,
68
Image,
7-
Input,
9+
Input,
810
Text,
911
} from "@chakra-ui/react";
12+
import { useEffect, useState } from "react";
1013
import { getItem } from "../redux/localStorage";
1114

12-
export default function Cart(cart) {
15+
export default function Cart() {
16+
const [cart,setCart] = useState([]);
1317
console.log(cart);
18+
const handleData = async()=>{
19+
const userID = getItem("userid");
20+
let res = await fetch(`http://localhost:3000/api/cart/${userID}`);
21+
let data = await res.json();
22+
setCart(data);
23+
}
24+
25+
useEffect(()=>{
26+
handleData();
27+
28+
},[])
1429

1530
return (
1631
<>
17-
<Box
18-
display={"flex"}
19-
padding={3}
20-
gap={10}
21-
justifyContent={"space-around"}
22-
>
23-
<Box display={"flex"} width={"50%"} alignItems={"center"}>
24-
<Box width={"80%"}>
25-
{cart !==
26-
{
27-
error: "Please correct enter id in params",
28-
}
29-
? cart.cart?.map((ele) => {
30-
return (
31-
<Box border={"1px solid black"} width={"300px"} padding={2}>
32-
<Image src={ele.courseID.image} alt="Linux" />
33-
<Text>{ele.courseID.course_name}</Text>
34-
<Button margin={"auto auto"}>Delete</Button>
35-
</Box>
36-
);
37-
})
38-
: ""}
39-
</Box>
40-
</Box>
41-
<Box width={"30%"}>
42-
<Text fontSize={30}>Total:</Text>
43-
<FormControl isRequired>
44-
<FormLabel>Card Number</FormLabel>
45-
<Input type="number" />
46-
<FormLabel>CVV</FormLabel>
47-
<Input type="number" />
48-
<FormLabel>Expire Date</FormLabel>
49-
<Input size="md" type="date" />
50-
</FormControl>
51-
<Button mt={4} background="#45F3FF" type="submit">
52-
Submit
53-
</Button>
54-
<Button mt={4} background="#45F3FF" type="submit">
55-
Pay with RazorPay
56-
</Button>
57-
</Box>
58-
</Box>
59-
</>
60-
);
32+
<Box
33+
borderRadius={"15px"}
34+
mt={"10px"}
35+
position="absolute"
36+
height="600px"
37+
display={"inline-block"}
38+
ml={"10px"}
39+
width={"55%"}
40+
>
41+
<Box display={"flex"} w={""} justifyContent="space-between">
42+
<Heading
43+
ml="100px"
44+
mt="10px"
45+
fontFamily={"Ubuntu, sans-serif"}
46+
fontSize="26px"
47+
color={"blackAlpha.500"}
48+
>
49+
{cart.cart
50+
? `Your Cart (${cart.cart.length})`
51+
: `Your Cart (${0}))`}
52+
</Heading>
53+
<Text
54+
mt="15px"
55+
mr="118px"
56+
fontFamily={"Ubuntu, sans-serif"}
57+
color={"blackAlpha.700"}
58+
fontSize="20px"
59+
>
60+
Price
61+
</Text>
62+
</Box>
63+
<Flex
64+
flexDirection={"column"}
65+
height={"500px"}
66+
mt={{ base: "-15px", lg: "auto" }}
67+
overflow="scroll"
68+
>
69+
70+
{cart.cart ? cart.cart.map((el, i) => (
71+
<>
72+
<Flex justifyContent={"space-between"}>
73+
<Box
74+
display={"flex"}
75+
flexDirection="column"
76+
key={i}
77+
justify="right"
78+
alignItems="right"
79+
height="auto"
80+
w={{ base: "auto", lg: "auto" }}
81+
mt={{ base: "20px", lg: "20px" }}
82+
ml={{ base: "-480px", lg: "40px" }}
83+
gap="2"
84+
>
85+
<Flex gap={5}>
86+
<Image
87+
className="imageFromPayment"
88+
h="180px"
89+
maxW={"180px"}
90+
src={el.courseID.image}
91+
/>
92+
</Flex>
93+
<Box>
94+
<Text
95+
h="auto"
96+
fontFamily={"Ubuntu, sans-serif"}
97+
color="teal"
98+
w="350px"
99+
fontSize={"18px"}
100+
>
101+
{el.courseID.course_name}
102+
</Text>
103+
</Box>
104+
</Box>
105+
<Text
106+
mt={{ lg: "130px" }}
107+
fontSize="24px"
108+
fontFamily={"Ubuntu, sans-serif;"}
109+
mr={{ lg: "100px" }}
110+
color="teal"
111+
>
112+
{el.courseID.selling_price}
113+
</Text>
114+
</Flex>
115+
{/* </Box> */}
116+
</>
117+
)):""}
118+
</Flex>
119+
</Box>
120+
</>)
61121
}
62122

63-
export async function getServerSideProps(context) {
64-
const userID = getItem("userid");
65-
let res = await fetch(`http://localhost:3000/api/cart/${userID}`);
66-
let data = await res.json();
67-
let courses = data;
68-
return {
69-
props: courses,
70-
};
71-
}
123+
// export async function getServerSideProps(context) {
124+
// const userID = getItem("userid");
125+
// let res = await fetch(`http://localhost:3000/api/cart/${userID}`);
126+
// let data = await res.json();
127+
// let courses = {data,userID};
128+
129+
// return {
130+
// props: courses,
131+
// };
132+
// }

pages/courses/[course].js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const socket = io();
1818

1919
function course({ data, data_chatting }) {
2020
const userID = getItem("userid");
21+
console.log(userID)
2122
const [state, setState] = useState("");
2223
const [message, setMessage] = useState([]);
2324
const [added, setAdded] = useState(false);
@@ -49,6 +50,7 @@ function course({ data, data_chatting }) {
4950
courseID: cart_id,
5051
});
5152
const data = res.data;
53+
console.log(data);
5254
if (data) {
5355
toast({
5456
title: "Course have been added to Cart",

pages/courses/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ const index = ({ All_Courses }) => {
6868
export default index;
6969

7070
export async function getServerSideProps(context) {
71-
let res = await fetch(`https://data-base.onrender.com/courses`);
71+
let res = await fetch(`http://localhost:3000/api/courses`);
7272
let data = await res.json();
7373
let courses = data;
7474

7575
return {
76-
props: {
77-
All_Courses: courses,
78-
},
76+
props: courses
7977
};
8078
}

0 commit comments

Comments
 (0)