aboutsummaryrefslogtreecommitdiff
path: root/pages/api
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-04-13 22:14:50 +0700
committerFactiven <[email protected]>2023-04-13 22:14:50 +0700
commit5007e48c8546d8138092039bf3cecc0b9904a407 (patch)
tree8b27c6fce4dcde5d21c04b65e1f2059924532cb4 /pages/api
parentUpdate index.js (diff)
downloadmoopa-5007e48c8546d8138092039bf3cecc0b9904a407.tar.xz
moopa-5007e48c8546d8138092039bf3cecc0b9904a407.zip
Update 4th
Diffstat (limited to 'pages/api')
-rw-r--r--pages/api/update-user.js10
-rw-r--r--pages/api/watched-episode.js43
2 files changed, 53 insertions, 0 deletions
diff --git a/pages/api/update-user.js b/pages/api/update-user.js
index 210d70f..67c80d0 100644
--- a/pages/api/update-user.js
+++ b/pages/api/update-user.js
@@ -9,6 +9,16 @@ export default async function handler(req, res) {
const { name, newData } = req.body; // id is the user ID and newData is the new data you want to set
try {
+ const existingData = await collection.findOne({
+ name: name,
+ "recentWatch.id": newData.recentWatch.id,
+ });
+
+ if (existingData) {
+ res.status(200).json({ message: "Data already exists" });
+ return;
+ }
+
const result = await collection.updateOne(
{ name: name },
{ $addToSet: newData }
diff --git a/pages/api/watched-episode.js b/pages/api/watched-episode.js
new file mode 100644
index 0000000..271348d
--- /dev/null
+++ b/pages/api/watched-episode.js
@@ -0,0 +1,43 @@
+// pages/api/update-user.js
+import clientPromise from "../../lib/mongodb";
+
+export default async function handler(req, res) {
+ const client = await clientPromise;
+ const db = client.db("authbase");
+ const collection = db.collection("users");
+
+ const { username, id, newData } = req.body; // id is the user ID and newData is the new data you want to set
+
+ try {
+ const result = await collection.updateOne(
+ {
+ name: username,
+ "recentWatch.id": id,
+ "recentWatch.episode.id": { $ne: newData.id },
+ },
+ { $addToSet: { "recentWatch.$.episode": newData } }
+ );
+
+ if (result.modifiedCount === 0) {
+ const updateResult = await collection.updateOne(
+ {
+ name: username,
+ "recentWatch.id": id,
+ "recentWatch.episode.id": newData.id,
+ "recentWatch.episode.time": { $ne: newData.time },
+ },
+ { $set: { "recentWatch.$.episode.$[elem].time": newData.time } },
+ { arrayFilters: [{ "elem.id": newData.id }] }
+ );
+ if (updateResult.modifiedCount === 0) {
+ console.log("The episode already exists with the same time.");
+ }
+ }
+
+ console.log("Successfully updated the recentWatch collection.");
+
+ res.status(200).json(result);
+ } catch (error) {
+ res.status(500).json({ error: "Unable to update user data", dat: newData });
+ }
+}