Added vote route in backend
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
27
backend/src/repositories/vote.repo.js
Normal file
27
backend/src/repositories/vote.repo.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user