Skip to content

Commit

Permalink
restructured controllers/write/topics WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tu2463 committed Oct 10, 2024
1 parent 15eceae commit 040e320
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 51 deletions.
14 changes: 1 addition & 13 deletions public/openapi/write/topics/tid/favorite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
properties: {}
6 changes: 0 additions & 6 deletions src/api/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
Expand Down
36 changes: 4 additions & 32 deletions src/controllers/write/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
5 changes: 5 additions & 0 deletions src/routes/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/topics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 040e320

Please sign in to comment.