Skip to content

Commit

Permalink
close #57
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffrey-wu committed Aug 23, 2022
1 parent 3ba2df1 commit ff2b030
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
10 changes: 10 additions & 0 deletions client/api-info.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@
</li>
</ul>
</li> -->
<li class="list-group-item">
<div>POST <code>qbreader.org/api/report-question</code></div>
<div>Parameters:</div>
<ul>
<li><code>_id</code> - the id of the question being reported.</li>
<li>
<b>returns</b> - a 200 status code if the operation was successful and a 500 status code if there was an error.
</li>
</ul>
</li>
<li class="list-group-item">
<div>GET <code>qbreader.org/api/set-list</code></div>
<i>Takes no parameters.</i>
Expand Down
38 changes: 34 additions & 4 deletions client/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,34 @@ const createTossupCard = (function () {

questionCounter++;
const { question, answer, category, subcategory, packetNumber, questionNumber, _id } = tossup;

// append a card containing the question to the history element
let card = document.createElement('div');
card.className = 'card my-2';
card.innerHTML = `
<div class="card-header" data-bs-toggle="collapse" data-bs-target="#question-${questionCounter}" aria-expanded="true">${answer}</div>
<div class="card-header" data-bs-toggle="collapse" data-bs-target="#question-${questionCounter}" aria-expanded="true">
${answer}
</div>
<div class="card-container collapse" id="question-${questionCounter}">
<div class="card-body">
<p class="card-text">${question}</p>
<p class="card-text">
${question}
<a href="#" id="report-question-${_id}">Report Question</a>
</p>
</div>
<div class="card-footer">
<small class="text-muted">${setName} / ${category} / ${subcategory}</small>
<small class="text-muted float-end">Packet ${packetNumber} / Question ${questionNumber}</small>
</div>
</div>
`

document.getElementById('room-history').prepend(card);

document.getElementById('report-question-' + _id).addEventListener('click', function (e) {
e.preventDefault();
reportQuestion(_id);
});
}
})();

Expand Down Expand Up @@ -188,6 +198,26 @@ function rangeToArray(string, max = 0) {
}


function reportQuestion(_id) {
fetch('/api/report-question', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
_id: _id
})
}).then(response => {
if (response.status === 200) {
alert('Question has been reported.');
} else {
alert('There was an error reporting the question.');
}
}).catch(error => {
alert('There was an error reporting the question.');
});
}

/**
* Adds the given category if it is not in the list of valid categories.
* Otherwise, the category is removed.
Expand Down
10 changes: 10 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ router.post('/random-question', async (req, res) => {
});
*/

router.post('/report-question', async (req, res) => {
const _id = req.body._id;
const successful = await database.reportQuestion(_id);
if (successful) {
res.sendStatus(200);
} else {
res.sendStatus(500);
}
})

router.get('/set-list', (req, res) => {
const setList = database.getSetList(req.query.setName);
res.send(setList);
Expand Down
19 changes: 17 additions & 2 deletions server/database.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { MongoClient } = require('mongodb');
const { MongoClient, ObjectId } = require('mongodb');
const { CATEGORIES, SUBCATEGORIES_FLATTENED } = require('./quizbowl');

const ADJECTIVES = ['adaptable', 'adept', 'affectionate', 'agreeable', 'alluring', 'amazing', 'ambitious', 'amiable', 'ample', 'approachable', 'awesome', 'blithesome', 'bountiful', 'brave', 'breathtaking', 'bright', 'brilliant', 'capable', 'captivating', 'charming', 'competitive', 'confident', 'considerate', 'courageous', 'creative', 'dazzling', 'determined', 'devoted', 'diligent', 'diplomatic', 'dynamic', 'educated', 'efficient', 'elegant', 'enchanting', 'energetic', 'engaging', 'excellent', 'fabulous', 'faithful', 'fantastic', 'favorable', 'fearless', 'flexible', 'focused', 'fortuitous', 'frank', 'friendly', 'funny', 'generous', 'giving', 'gleaming', 'glimmering', 'glistening', 'glittering', 'glowing', 'gorgeous', 'gregarious', 'gripping', 'hardworking', 'helpful', 'hilarious', 'honest', 'humorous', 'imaginative', 'incredible', 'independent', 'inquisitive', 'insightful', 'kind', 'knowledgeable', 'likable', 'lovely', 'loving', 'loyal', 'lustrous', 'magnificent', 'marvelous', 'mirthful', 'moving', 'nice', 'optimistic', 'organized', 'outstanding', 'passionate', 'patient', 'perfect', 'persistent', 'personable', 'philosophical', 'plucky', 'polite', 'powerful', 'productive', 'proficient', 'propitious', 'qualified', 'ravishing', 'relaxed', 'remarkable', 'resourceful', 'responsible', 'romantic', 'rousing', 'sensible', 'shimmering', 'shining', 'sincere', 'sleek', 'sparkling', 'spectacular', 'spellbinding', 'splendid', 'stellar', 'stunning', 'stupendous', 'super', 'technological', 'thoughtful', 'twinkling', 'unique', 'upbeat', 'vibrant', 'vivacious', 'vivid', 'warmhearted', 'willing', 'wondrous', 'zestful'];
Expand Down Expand Up @@ -198,4 +198,19 @@ function getSetList() {
}


module.exports = { getNextQuestion, getNumPackets, getPacket, getRandomQuestion, getSetList, getRandomName };
/**
* Report question with given id to the database.
* @param {String} _id
* @returns {Promise<Boolean>} true if successful, false otherwise.
*/
async function reportQuestion(_id) {
return await QUESTIONS.updateOne({ _id: new ObjectId(_id) }, { $inc: { reported: 1 } }).then(() => {
console.log('Reported question with id ' + _id);
return true;
}).catch(error => {
console.log('DATABASE ERROR:', error);
return false;
});
}

module.exports = { getNextQuestion, getNumPackets, getPacket, getRandomQuestion, getSetList, getRandomName, reportQuestion };

0 comments on commit ff2b030

Please sign in to comment.