mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-02 03:25:22 +08:00
updating result tags
This commit is contained in:
parent
7ddf37f8a3
commit
9676e47ea0
6 changed files with 44 additions and 22 deletions
|
@ -18,6 +18,17 @@ class ResultController {
|
|||
}
|
||||
}
|
||||
|
||||
static async updateTags(req, res, next) {
|
||||
try {
|
||||
const { uid } = req.decodedToken;
|
||||
const { tags, resultid } = req.body;
|
||||
await ResultDAO.updateTags(uid, resultid, tags);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
}
|
||||
|
||||
static async addResult(req, res, next) {
|
||||
try {
|
||||
const { uid } = req.decodedToken;
|
||||
|
@ -149,15 +160,13 @@ class ResultController {
|
|||
|
||||
let addedResult = await ResultDAO.addResult(uid, result);
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({
|
||||
message: "Result saved",
|
||||
isPb,
|
||||
name: result.name,
|
||||
tagPbs,
|
||||
insertedId: addedResult.insertedId,
|
||||
});
|
||||
return res.status(200).json({
|
||||
message: "Result saved",
|
||||
isPb,
|
||||
name: result.name,
|
||||
tagPbs,
|
||||
insertedId: addedResult.insertedId,
|
||||
});
|
||||
} catch (e) {
|
||||
next(e);
|
||||
}
|
||||
|
|
|
@ -8,4 +8,6 @@ router.get("/", authenticateRequest, ResultController.getResults);
|
|||
|
||||
router.post("/add", authenticateRequest, ResultController.addResult);
|
||||
|
||||
router.post("/updateTags", authenticateRequest, ResultController.updateTags);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
const { ObjectID } = require("mongodb");
|
||||
const MonkeyError = require("../handlers/error");
|
||||
const { mongoDB } = require("../init/mongodb");
|
||||
const UserDAO = require("./user");
|
||||
|
@ -18,19 +19,22 @@ class ResultDAO {
|
|||
};
|
||||
}
|
||||
|
||||
static async editResultTags(uid, id, tags) {
|
||||
const result = await mongoDB().collection("results").findOne({ id, uid });
|
||||
static async updateTags(uid, resultid, tags) {
|
||||
const result = await mongoDB()
|
||||
.collection("results")
|
||||
.findOne({ _id: ObjectID(resultid), uid });
|
||||
if (!result) throw new MonkeyError(404, "Result not found");
|
||||
const userTags = await UserDAO.getTags(uid);
|
||||
const userTagIds = userTags.map((tag) => tag._id.toString());
|
||||
let validTags = true;
|
||||
tags.forEach((tagId) => {
|
||||
if (!userTags.includes(tagId)) validTags = false;
|
||||
if (!userTagIds.includes(tagId)) validTags = false;
|
||||
});
|
||||
if (!validTags)
|
||||
throw new MonkeyError(400, "One of the tag id's is not vaild");
|
||||
return await mongoDB()
|
||||
.collection("results")
|
||||
.updateOne({ id, uid }, { $set: { tags } });
|
||||
.updateOne({ _id: ObjectID(resultid), uid }, { $set: { tags } });
|
||||
}
|
||||
|
||||
static async getResult(uid, id) {
|
||||
|
@ -45,7 +49,7 @@ class ResultDAO {
|
|||
const result = await mongoDB()
|
||||
.collection("results")
|
||||
.find({ uid })
|
||||
.sort({ timestamp: -1 })
|
||||
.sort({ timestamp: 1 })
|
||||
.skip(start)
|
||||
.limit(end)
|
||||
.toArray(); // this needs to be changed to later take patreon into consideration
|
||||
|
|
|
@ -248,13 +248,13 @@ function loadMoreLines(lineIndex) {
|
|||
restags = JSON.stringify(result.tags);
|
||||
}
|
||||
|
||||
let tagIcons = `<span id="resultEditTags" resultId="${result.id}" tags='${restags}' aria-label="no tags" data-balloon-pos="up" style="opacity: .25"><i class="fas fa-fw fa-tag"></i></span>`;
|
||||
let tagIcons = `<span id="resultEditTags" resultId="${result._id}" tags='${restags}' aria-label="no tags" data-balloon-pos="up" style="opacity: .25"><i class="fas fa-fw fa-tag"></i></span>`;
|
||||
|
||||
if (tagNames !== "") {
|
||||
if (result.tags !== undefined && result.tags.length > 1) {
|
||||
tagIcons = `<span id="resultEditTags" resultId="${result.id}" tags='${restags}' aria-label="${tagNames}" data-balloon-pos="up"><i class="fas fa-fw fa-tags"></i></span>`;
|
||||
tagIcons = `<span id="resultEditTags" resultId="${result._id}" tags='${restags}' aria-label="${tagNames}" data-balloon-pos="up"><i class="fas fa-fw fa-tags"></i></span>`;
|
||||
} else {
|
||||
tagIcons = `<span id="resultEditTags" resultId="${result.id}" tags='${restags}' aria-label="${tagNames}" data-balloon-pos="up"><i class="fas fa-fw fa-tag"></i></span>`;
|
||||
tagIcons = `<span id="resultEditTags" resultId="${result._id}" tags='${restags}' aria-label="${tagNames}" data-balloon-pos="up"><i class="fas fa-fw fa-tag"></i></span>`;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -126,6 +126,7 @@ export async function getUserResults() {
|
|||
try {
|
||||
let results = await axiosInstance.get("/results");
|
||||
dbSnapshot.results = results.data;
|
||||
await TodayTracker.addAllFromToday();
|
||||
return true;
|
||||
} catch (e) {
|
||||
Notifications.add("Error getting results", -1);
|
||||
|
|
|
@ -86,13 +86,16 @@ $("#resultEditTagsPanel .confirmButton").click((e) => {
|
|||
Loader.show();
|
||||
hide();
|
||||
axiosInstance
|
||||
.post("/updateResultTags", {
|
||||
.post("/results/updateTags", {
|
||||
tags: newtags,
|
||||
resultid: resultid,
|
||||
})
|
||||
.then((r) => {
|
||||
.then((response) => {
|
||||
Loader.hide();
|
||||
if (r.data.resultCode === 1) {
|
||||
|
||||
if (response.status !== 200) {
|
||||
Notifications.add(response.data.message);
|
||||
} else {
|
||||
Notifications.add("Tags updated.", 1, 2);
|
||||
DB.getSnapshot().results.forEach((result) => {
|
||||
if (result.id === resultid) {
|
||||
|
@ -143,8 +146,11 @@ $("#resultEditTagsPanel .confirmButton").click((e) => {
|
|||
"no tags"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Notifications.add("Error updating tags: " + r.data.message, -1);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
Loader.hide();
|
||||
let msg = e?.response?.data?.message ?? e.message;
|
||||
Notifications.add("Failed to update result tags: " + msg, -1);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue