mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-13 09:06:10 +08:00
added tag functions to users dao
This commit is contained in:
parent
871273c455
commit
ef008758b0
1 changed files with 38 additions and 0 deletions
|
@ -22,6 +22,44 @@ class UsersDAO {
|
|||
if (!user) throw new MonkeyError(404, "User not found");
|
||||
return user;
|
||||
}
|
||||
|
||||
static async addTag(uid, name) {
|
||||
return await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne({ uid }, { $push: { tags: { name } } });
|
||||
}
|
||||
|
||||
static async editTag(uid, id, name) {
|
||||
const user = await mongoDB().collection("users").findOne({ uid });
|
||||
if (!user) throw new MonkeyError(404, "User not found");
|
||||
if (
|
||||
user.tags === undefined ||
|
||||
user.tags.filter((t) => t._id === id).length === 0
|
||||
)
|
||||
throw new MonkeyError(404, "Tag not found");
|
||||
return await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne(
|
||||
{
|
||||
uid: uid,
|
||||
"tags._id": id,
|
||||
},
|
||||
{ $set: { tags: { name } } }
|
||||
);
|
||||
}
|
||||
|
||||
static async removeTag(uid, id) {
|
||||
const user = await mongoDB().collection("users").findOne({ uid });
|
||||
if (!user) throw new MonkeyError(404, "User not found");
|
||||
if (
|
||||
user.tags === undefined ||
|
||||
user.tags.filter((t) => t._id === id).length === 0
|
||||
)
|
||||
throw new MonkeyError(404, "Tag not found");
|
||||
return await mongoDB()
|
||||
.collection("users")
|
||||
.updateOne({ uid }, { $pull: { id } });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UsersDAO;
|
||||
|
|
Loading…
Add table
Reference in a new issue