Added vote route in backend

This commit is contained in:
Manik Maity
2024-11-12 19:47:58 +05:30
parent c61fe265a5
commit 451a57c438
8 changed files with 173 additions and 28 deletions

View File

@@ -1,4 +1,6 @@
import PollModel from "../models/poll.model.js";
import mongoose from "mongoose";
const {ObjectId} = mongoose.Types;
export async function createPollByData(data) {
try {
@@ -38,4 +40,21 @@ export async function deletePollById(id) {
catch(err){
throw err;
}
}
export async function updatePollVoteCount(pollId, optionId) {
try {
const pollIdObejct = new ObjectId(pollId);
const optionIdObject = new ObjectId(optionId);
console.log(pollId, optionId, pollIdObejct, optionIdObject);
const result = await PollModel.updateOne(
{ _id: pollIdObejct, "options._id": optionIdObject },
{ $inc: { "options.$.voteCount": 1 } }
);
return result;
}
catch(err){
throw err;
}
}

View File

@@ -0,0 +1,27 @@
import VoteModel from "../models/vote.model.js";
export async function findVoteByPollIdAndUserId(pollId, userId) {
try {
const vote = VoteModel.findOne({pollId : pollId, userId : userId});
return vote;
}
catch(err){
throw err;
}
}
export async function createVote(pollId, userId, optionId) {
try{
const vote = VoteModel.create({
pollId,
userId,
optionId
});
return vote;
}
catch(err){
throw err;
}
}