Closed Pratik1374 closed 11 months ago
Issue #250
@lugenx @Pratik1374 shouldn't the getAnswerByDate
function be called in a new and separate function that gets all of the users answers? This way if a date is passed it will filter those results.
@lugenx @Pratik1374 shouldn't the
getAnswerByDate
function be called in a new and separate function that gets all of the users answers? This way if a date is passed it will filter those results.
as mentioned in the issue , the date will be provided as a query parameter so I did a check in same controller which returns the answers
@lugenx @Pratik1374 shouldn't the
getAnswerByDate
function be called in a new and separate function that gets all of the users answers? This way if a date is passed it will filter those results.as mentioned in the issue , the date will be provided as a query parameter so I did a check in same controller which returns the answers
That function returns a single answer based on the id passed. Shouldn't the date be passed as a query parameter to the endpoint GET /answer?date=YYYY-MM-DD
not GET /answer/:id?date=YYYY-MM-DD
. So the check would be made inside of a function that returns all of the answers. Currently there is no function to get all of the answers, and no endpoint GET answer/
.
thank you for your effort @Pratik1374, as @tsimian mentioned, could you please make a standalone
answers
endpoint instead of using the already existing one. getAnswer function should only return an answer with an id. please refer to the issue body, and let me know if you have a question. thanks again for your contribution.
thank you for the clarification @lugenx and @tsimian , I will update the code and will add ### GET /answer route which will return all the answers of that particular user ,also if date parameter is provided then I will return answers of that user on that particular date
I have updated the code, please check it out and revert back if any changes are there
There are dedicated controllers in controller file thats why I did that in same function only, if you want getAnswersByDate function I can do that also, please confirm me once @tsimian @lugenx
@Pratik1374, i noticed an issue with fetching data by date. for example, when I add data for today and then try to fetch yesterday's data, it still brings up today's data. this might be a timezone issue. can you check it out? thanks!
@tsimian, after merging this, i think we need to reorganize our API endpoints. as complexity increases, adding each method to the URL path will help with clarity. also, we should rename the
answer
endpoint toanswers
to better accommodate other methods.
ok, I will check it
@Pratik1374, i noticed an issue with fetching data by date. for example, when I add data for today and then try to fetch yesterday's data, it still brings up today's data. this might be a timezone issue. can you check it out? thanks!
@tsimian, after merging this, i think we need to reorganize our API endpoints. as complexity increases, adding each method to the URL path will help with clarity. also, we should rename the
answer
endpoint toanswers
to better accommodate other methods.
const getUserAnswers = async (req, res) => {
// Check if the user is authenticated
if (!req.user) {
return res.status(401).json({ error: "Unauthorized User" });
}
try {
const userId = req.user.id;
const { date } = req.query;
//If date parameter is provided then user's answer of that particular date will be returned
if (date) {
// Find all answers for the specified date and user ID
const startDate = new Date(date);
const endDate = new Date(date);
endDate.setUTCHours(23, 59, 59, 999);
const answers = await Answer.find({
user: userId,
createdAt: {
$gte: startDate,
$lt: endDate,
},
});
return res.status(200).json(answers);
}
// Find all answers for the authenticated user
const answers = await Answer.find({ user: userId });
res.status(200).json(answers);
} catch (err) {
res.status(400).json({ error: err.message });
}
};
Try replacing getUserAnswers controller with above one , you are right the issue is with timezone. Code is working fine on my side. I think this should work ,but I am not sure about it , please try it once. Sorry for inconvenience
@Pratik1374, thanks for this, can you push the changes to the PR instead of a code snippet. it's safer and easier to track that way. after that, I will test and let you know how it works on my side.
@Pratik1374, thanks for this, can you push the changes to the PR instead of a code snippet. it's safer and easier to track that way. after that, I will test and let you know how it works on my side.
@lugenx I have pushed changes with new commit, please check it
I have updated the getAnswer controller to include a check for the presence of a date parameter. If a date parameter is provided, it will execute the getAnswerByDate method; otherwise, it will proceed with the previous getAnswer operation.