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,11 +1,13 @@
import mongoose from "mongoose";
import { createPollByData, deletePollById, findPollById, findPollsByCreatorId } from "../repositories/poll.repo.js";
import { createPollByData, deletePollById, findPollById, findPollsByCreatorId, updatePollVoteCount } from "../repositories/poll.repo.js";
import { createVote, findVoteByPollIdAndUserId } from "../repositories/vote.repo.js";
export async function createPollService(title, description, options, userId) {
try {
const optionsData = options.map(option => ({
name: option,
_id : new mongoose.Types.ObjectId()
_id : new mongoose.Types.ObjectId(),
voteCount : 0
}));
const data = {
@@ -71,6 +73,35 @@ export async function deletePollService(pollId, user) {
return deletedPoll;
}
catch(err){
throw err;
}
}
export async function createVoteService(pollId, userId, optionId) {
try {
// vote already voted
const vote = await findVoteByPollIdAndUserId(pollId, userId);
if (vote) {
throw {
statusCode: 400,
message: "You have already voted on this poll"
}
}
console.log(pollId, userId, optionId);
const poll = await findPollById(pollId);
if (!poll) {
throw {
statusCode: 404,
message: "Poll not found"
}
}
const updatedPollData = await updatePollVoteCount(pollId, optionId);
const createdVote = await createVote(pollId, userId, optionId);
console.log(updatedPollData)
return createVote;
}
catch(err){
throw err;
}