Skip to content

Commit 2fb8a00

Browse files
committed
Release 1.0.2
1 parent 8a9db2d commit 2fb8a00

79 files changed

Lines changed: 6979 additions & 1165 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/controllers/auth.controller.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ export const login = async (req, res) => {
113113

114114
const isPasswordValid = await bcryptjs.compare(password, user.password);
115115
if (!isPasswordValid) {
116-
return res.status(403).json({ error: "Invalid credentials" });
116+
console.log("Password: ", user.password, password);
117+
return res.status(403).json({ error: "Invalid credentials: " });
117118
}
118119

119120
// Create JWT token
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Comment from "../model/comment.model.js";
2+
import { Song } from "../model/song.model.js";
3+
import { User } from "../model/user.model.js";
4+
5+
export const addComments = async (req, res) => {
6+
try {
7+
const { trackId, userId, text } = req.body;
8+
9+
const song = await Song.findById(trackId);
10+
if (!song) {
11+
return res.status(404).json({ error: "Song not found" });
12+
}
13+
14+
const newComment = new Comment({
15+
user: userId,
16+
text,
17+
song: trackId,
18+
});
19+
20+
await newComment.save();
21+
22+
song.comments.push(newComment._id);
23+
await song.save();
24+
25+
// Populate the user information for the response
26+
await newComment.populate("user", "username");
27+
28+
res.status(201).json({ comment: newComment });
29+
} catch (error) {
30+
console.error("Error adding comment:", error);
31+
res.status(500).json({ error: "Internal server error" });
32+
}
33+
};
34+
35+
export const getComments = async (req, res) => {
36+
try {
37+
const { trackId } = req.params;
38+
39+
const comments = await Comment.find({ song: trackId })
40+
.populate("user", "username")
41+
.sort({ createdAt: -1 });
42+
43+
res.status(200).json({ comments });
44+
} catch (error) {
45+
console.error("Error getting comments:", error);
46+
res.status(500).json({ error: "Internal server error" });
47+
}
48+
};
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import paypal from "../utils/paypal";
2+
import { Order } from "../model/order.model.js";
3+
const Course = require("../../models/Course");
4+
const StudentCourses = require("../../models/StudentCourses");
5+
6+
const createOrder = async (req, res) => {
7+
try {
8+
const {
9+
userId,
10+
userName,
11+
userEmail,
12+
orderStatus,
13+
paymentMethod,
14+
paymentStatus,
15+
orderDate,
16+
paymentId,
17+
payerId,
18+
instructorId,
19+
instructorName,
20+
courseImage,
21+
courseTitle,
22+
courseId,
23+
coursePricing,
24+
} = req.body;
25+
26+
const create_payment_json = {
27+
intent: "sale",
28+
payer: {
29+
payment_method: "paypal",
30+
},
31+
redirect_urls: {
32+
return_url: `${process.env.CLIENT_URL}/payment-return`,
33+
cancel_url: `${process.env.CLIENT_URL}/payment-cancel`,
34+
},
35+
transactions: [
36+
{
37+
item_list: {
38+
items: [
39+
{
40+
name: courseTitle,
41+
sku: courseId,
42+
price: coursePricing,
43+
currency: "USD",
44+
quantity: 1,
45+
},
46+
],
47+
},
48+
amount: {
49+
currency: "USD",
50+
total: coursePricing.toFixed(2),
51+
},
52+
description: courseTitle,
53+
},
54+
],
55+
};
56+
57+
paypal.payment.create(create_payment_json, async (error, paymentInfo) => {
58+
if (error) {
59+
console.log(error);
60+
return res.status(500).json({
61+
success: false,
62+
message: "Error while creating paypal payment!",
63+
});
64+
} else {
65+
const newlyCreatedCourseOrder = new Order({
66+
userId,
67+
userName,
68+
userEmail,
69+
orderStatus,
70+
paymentMethod,
71+
paymentStatus,
72+
orderDate,
73+
paymentId,
74+
payerId,
75+
instructorId,
76+
instructorName,
77+
courseImage,
78+
courseTitle,
79+
courseId,
80+
coursePricing,
81+
});
82+
83+
await newlyCreatedCourseOrder.save();
84+
85+
const approveUrl = paymentInfo.links.find(
86+
(link) => link.rel == "approval_url"
87+
).href;
88+
89+
res.status(201).json({
90+
success: true,
91+
data: {
92+
approveUrl,
93+
orderId: newlyCreatedCourseOrder._id,
94+
},
95+
});
96+
}
97+
});
98+
} catch (err) {
99+
console.log(err);
100+
res.status(500).json({
101+
success: false,
102+
message: "Some error occured!",
103+
});
104+
}
105+
};
106+
107+
const capturePaymentAndFinalizeOrder = async (req, res) => {
108+
try {
109+
const { paymentId, payerId, orderId } = req.body;
110+
111+
let order = await Order.findById(orderId);
112+
113+
if (!order) {
114+
return res.status(404).json({
115+
success: false,
116+
message: "Order can not be found",
117+
});
118+
}
119+
120+
order.paymentStatus = "paid";
121+
order.orderStatus = "confirmed";
122+
order.paymentId = paymentId;
123+
order.payerId = payerId;
124+
125+
await order.save();
126+
127+
//update out student course model
128+
const studentCourses = await StudentCourses.findOne({
129+
userId: order.userId,
130+
});
131+
132+
if (studentCourses) {
133+
studentCourses.courses.push({
134+
courseId: order.courseId,
135+
title: order.courseTitle,
136+
instructorId: order.instructorId,
137+
instructorName: order.instructorName,
138+
dateOfPurchase: order.orderDate,
139+
courseImage: order.courseImage,
140+
});
141+
142+
await studentCourses.save();
143+
} else {
144+
const newStudentCourses = new StudentCourses({
145+
userId: order.userId,
146+
courses: [
147+
{
148+
courseId: order.courseId,
149+
title: order.courseTitle,
150+
instructorId: order.instructorId,
151+
instructorName: order.instructorName,
152+
dateOfPurchase: order.orderDate,
153+
courseImage: order.courseImage,
154+
},
155+
],
156+
});
157+
158+
await newStudentCourses.save();
159+
}
160+
161+
//update the course schema students
162+
await Course.findByIdAndUpdate(order.courseId, {
163+
$addToSet: {
164+
students: {
165+
studentId: order.userId,
166+
studentName: order.userName,
167+
studentEmail: order.userEmail,
168+
paidAmount: order.coursePricing,
169+
},
170+
},
171+
});
172+
173+
res.status(200).json({
174+
success: true,
175+
message: "Order confirmed",
176+
data: order,
177+
});
178+
} catch (err) {
179+
console.log(err);
180+
res.status(500).json({
181+
success: false,
182+
message: "Some error occured!",
183+
});
184+
}
185+
};
186+
187+
module.exports = { createOrder, capturePaymentAndFinalizeOrder };

backend/controllers/playlists.controller.js

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const createPlaylist = async (req, res) => {
2828
const playlist = await Playlist.create(playlistData);
2929
return res.status(200).send(playlist);
3030
} catch (error) {
31+
console.log(currentUser);
3132
console.error("Error creating playlist:", error);
3233
return res.status(500).send({ err: "Failed to create playlist" });
3334
}
@@ -80,10 +81,26 @@ export const updatePlaylistById = async (req, res) => {
8081
};
8182

8283
export const getPlaylistCurrentUser = async (req, res) => {
83-
const artistId = req.user._id;
84+
try {
85+
const artistId = req.user._id;
86+
console.log("Artist ID:", artistId);
8487

85-
const playlists = await Playlist.find({ owner: artistId }).populate("owner");
86-
return res.status(200).json({ data: playlists });
88+
// Find playlists associated with the artist
89+
const playlists = await Playlist.find({ owner: artistId }).populate(
90+
"owner"
91+
);
92+
93+
if (!playlists || playlists.length === 0) {
94+
return res.status(404).json({ err: "No playlists found for the user" });
95+
}
96+
97+
return res.status(200).json({ data: playlists });
98+
} catch (error) {
99+
console.error("Error fetching playlists for user:", error);
100+
return res
101+
.status(500)
102+
.json({ err: "Server error while fetching playlists" });
103+
}
87104
};
88105

89106
export const getPlaylistByArtistId = async (req, res) => {
@@ -100,60 +117,34 @@ export const getPlaylistByArtistId = async (req, res) => {
100117
};
101118

102119
export const addSongToPlaylist = async (req, res) => {
103-
try {
104-
const currentUser = req.user;
105-
const { playlistId, songId } = req.params;
106-
107-
// Step 0: Check if playlistId and songId are valid ObjectIds
108-
if (
109-
!mongoose.Types.ObjectId.isValid(playlistId) ||
110-
!mongoose.Types.ObjectId.isValid(songId)
111-
) {
112-
return res.status(400).json({ error: "Invalid playlist or song ID" });
113-
}
120+
const { playlistId, songId } = req.params;
114121

115-
// Step 1: Get the playlist if valid
116-
const playlist = await Playlist.findById(playlistId);
117-
if (!playlist) {
118-
return res.status(404).json({ error: "Playlist does not exist" });
119-
}
120-
121-
// Step 2: Check if currentUser owns the playlist or is a collaborator
122-
if (
123-
!playlist.owner.equals(currentUser._id) &&
124-
!playlist.collaborators.includes(currentUser._id)
125-
) {
126-
return res
127-
.status(403)
128-
.json({ error: "You are not allowed to modify this playlist" });
129-
}
122+
if (!playlistId || !songId) {
123+
return res.status(400).json({ error: "Playlist ID or Song ID is missing" });
124+
}
130125

131-
// Step 3: Check if the song is a valid song
132-
const song = await Song.findById(songId);
133-
if (!song) {
134-
return res.status(404).json({ error: "Song does not exist" });
135-
}
126+
// Fetch playlist and song by ID
127+
const playlist = await Playlist.findById(playlistId);
128+
const song = await Song.findById(songId);
136129

137-
// Step 4: Avoid duplicate entries in the playlist
138-
if (playlist.songs.includes(songId)) {
139-
return res
140-
.status(400)
141-
.json({ error: "Song already exists in the playlist" });
142-
}
130+
if (!playlist || !song) {
131+
return res.status(404).json({ error: "Playlist or Song not found" });
132+
}
143133

144-
// Step 5: Add the song to the playlist
145-
playlist.songs.push(songId);
134+
// Add the song to the playlist
135+
try {
136+
playlist.songs.push(songId); // Assuming there's a 'songs' array in the Playlist schema
146137
await playlist.save();
147-
148138
return res
149139
.status(200)
150-
.json({ message: "Song added successfully", playlist });
140+
.json({ message: "Song added to playlist successfully" });
151141
} catch (error) {
152142
console.error("Error adding song to playlist:", error);
153-
return res.status(500).json({ error: "An unexpected error occurred" });
143+
return res
144+
.status(500)
145+
.json({ error: "Server error while adding song to playlist" });
154146
}
155147
};
156-
157148
export const getPlaylistByName = async (req, res) => {
158149
const { playlistName } = req.params;
159150

0 commit comments

Comments
 (0)