2024-11-12 22:21:59 +05:30
|
|
|
import UserModel from "../models/user.model.js";
|
2024-11-07 21:08:08 +05:30
|
|
|
|
|
|
|
|
export const createUser = async (username, email, password) => {
|
2024-11-12 22:21:59 +05:30
|
|
|
try {
|
|
|
|
|
const createdUser = await UserModel.create({
|
|
|
|
|
username,
|
|
|
|
|
email,
|
|
|
|
|
password,
|
|
|
|
|
});
|
2024-11-07 21:08:08 +05:30
|
|
|
|
2024-11-12 22:21:59 +05:30
|
|
|
return createdUser;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-11-08 12:51:14 +05:30
|
|
|
|
|
|
|
|
export async function findUserByEmail(email) {
|
2024-11-12 22:21:59 +05:30
|
|
|
try {
|
|
|
|
|
const user = await UserModel.findOne({ email });
|
|
|
|
|
return user;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-08 13:25:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function findUserById(id) {
|
2024-11-12 22:21:59 +05:30
|
|
|
try {
|
|
|
|
|
const user = await UserModel.findById(id);
|
|
|
|
|
return user;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function addPollIdToBookmark(userId, pollId) {
|
|
|
|
|
try {
|
|
|
|
|
const updatedData = UserModel.findOneAndUpdate(
|
|
|
|
|
{ _id: userId },
|
|
|
|
|
{ $push: { bookmarks: pollId } },
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return updatedData;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function removePollIdFromBookmark(userId, pollId) {
|
|
|
|
|
try {
|
|
|
|
|
const updatedData = UserModel.findOneAndUpdate(
|
|
|
|
|
{ _id: userId },
|
|
|
|
|
{ $pull: { bookmarks: pollId } },
|
|
|
|
|
{ new: true }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return updatedData;
|
2024-11-08 13:25:21 +05:30
|
|
|
}
|
2024-11-12 22:21:59 +05:30
|
|
|
catch{
|
2024-11-08 13:25:21 +05:30
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-12 23:39:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getUserBookmarkedPolls(userId) {
|
|
|
|
|
try {
|
|
|
|
|
const user = await UserModel.findById(userId).populate("bookmarks");
|
|
|
|
|
return user.bookmarks;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
2024-11-07 21:08:08 +05:30
|
|
|
}
|