From 040e3203a0de9c373bfa9d7c683ad980094241f3 Mon Sep 17 00:00:00 2001 From: Cheyu Tu Date: Fri, 4 Oct 2024 11:56:26 -0400 Subject: [PATCH] restructured controllers/write/topics WIP --- public/openapi/write/topics/tid/favorite.yaml | 14 +------- src/api/topics.js | 6 ---- src/controllers/write/topics.js | 36 +++---------------- src/routes/helpers.js | 5 +++ src/topics/{favorites.js => favorite.js} | 0 src/topics/index.js | 1 + 6 files changed, 11 insertions(+), 51 deletions(-) rename src/topics/{favorites.js => favorite.js} (100%) diff --git a/public/openapi/write/topics/tid/favorite.yaml b/public/openapi/write/topics/tid/favorite.yaml index b147d7d0e8..e0c895c1f5 100644 --- a/public/openapi/write/topics/tid/favorite.yaml +++ b/public/openapi/write/topics/tid/favorite.yaml @@ -21,15 +21,9 @@ put: properties: status: $ref: ../../../components/schemas/Status.yaml#/Status - message: - type: string - description: Success message - example: "Topic favorited successfully" response: type: object properties: {} - required: - - status delete: tags: @@ -54,12 +48,6 @@ delete: properties: status: $ref: ../../../components/schemas/Status.yaml#/Status - message: - type: string - description: Success message - example: "Topic unfavorited successfully" response: type: object - properties: {} - required: - - status \ No newline at end of file + properties: {} \ No newline at end of file diff --git a/src/api/topics.js b/src/api/topics.js index 8b000e34a8..83442d6dc8 100644 --- a/src/api/topics.js +++ b/src/api/topics.js @@ -175,16 +175,10 @@ topicsAPI.unfollow = async function (caller, data) { topicsAPI.favorite = async function (caller, data) { await topics.favorite(data.tid, caller.uid); - - const response = { status: "success", message: "Topic favorited successfully" }; - return response; }; topicsAPI.unfavorite = async function (caller, data) { await topics.unfavorite(data.tid, caller.uid); - - const response = { status: "success", message: "Topic favorited successfully" }; - return response; }; topicsAPI.updateTags = async (caller, { tid, tags }) => { diff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.js index 1d89cb1f68..bf8001614d 100644 --- a/src/controllers/write/topics.js +++ b/src/controllers/write/topics.js @@ -101,41 +101,13 @@ Topics.unfollow = async (req, res) => { }; Topics.favorite = async (req, res) => { - const { tid } = req.params; // Topic ID from the request - const { uid } = req.user; // User ID from the session - - try { - // Add the topic to the user's favorites in the database - await db.setAdd(`uid:${uid}:favorites`, tid); - await db.sortedSetAdd(`tid:${tid}:favoritedBy`, Date.now(), uid); - - helpers.formatApiResponse(200, res, { - status: 'success', - message: 'Topic added to favorites successfully', - response: {}, - }); - } catch (err) { - helpers.formatApiResponse(500, res, { status: 'error', message: err.message }); - } + await api.topics.favorite(req, req.params); + helpers.formatApiResponse(200, res); }; Topics.unfavorite = async (req, res) => { - const { tid } = req.params; // Topic ID from the request - const { uid } = req.user; // User ID from the session - - try { - // Remove the topic from the user's favorites in the database - await db.setRemove(`uid:${uid}:favorites`, tid); - await db.sortedSetRemove(`tid:${tid}:favoritedBy`, uid); - - helpers.formatApiResponse(200, res, { - status: 'success', - message: 'Topic removed from favorites successfully', - response: {}, - }); - } catch (err) { - helpers.formatApiResponse(500, res, { status: 'error', message: err.message }); - } + await api.topics.unfavorite(req, req.params); + helpers.formatApiResponse(200, res); }; diff --git a/src/routes/helpers.js b/src/routes/helpers.js index 34a455076e..7fa8eb3021 100644 --- a/src/routes/helpers.js +++ b/src/routes/helpers.js @@ -55,6 +55,11 @@ helpers.setupApiRoute = function (...args) { let middlewares = args.length > 4 ? args[args.length - 2] : []; const controller = args[args.length - 1]; + console.log("args: ", args); + console.log(`Setting up API route: ${verb.toUpperCase()} ${name}`); + console.log(`Controller: ${controller}`); + console.log(`Middlewares: ${middlewares}`); + middlewares = [ middleware.autoLocale, middleware.applyBlacklist, diff --git a/src/topics/favorites.js b/src/topics/favorite.js similarity index 100% rename from src/topics/favorites.js rename to src/topics/favorite.js diff --git a/src/topics/index.js b/src/topics/index.js index 5724d8a276..1189efa38e 100644 --- a/src/topics/index.js +++ b/src/topics/index.js @@ -25,6 +25,7 @@ require('./user')(Topics); require('./fork')(Topics); require('./posts')(Topics); require('./follow')(Topics); +require('./favorite')(Topics); require('./tags')(Topics); require('./teaser')(Topics); Topics.scheduled = require('./scheduled');