Skip to content

Commit

Permalink
new API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kotru21 committed Jan 10, 2025
1 parent 8f8118b commit 087a8e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import messageLike from "./util/messageLike.js";
import config from "./config.js";
import webServer from "./util/webServer.js";
import websocket from "./util/webSocket.js";
import API from "./util/server.js";
import API from "./util/API.js";

const initServers = () => {
if (config.WebServer) {
Expand Down
17 changes: 16 additions & 1 deletion util/server.js → util/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function API(port) {
const app = express();
app.use(cors());
app.options("*", cors());

// leaderboard endpoint to fetch the leaderboard json data
app.get("/api/leaderboard", async (req, res) => {
try {
const rows = await db.getLeaderboard();
Expand All @@ -16,6 +16,21 @@ export default function API(port) {
console.error(err);
}
});
app.get("/api/cat/:id", async (req, res) => {
try {
const catId = req.params.id;
const catData = await db.getCatById(catId);

if (!catData) {
return res.status(404).json({ error: "Cat not found" });
}

res.json(catData);
} catch (err) {
res.status(500).json({ error: "Failed to fetch cat data" });
console.error(err);
}
});

app.listen(port);
}
17 changes: 17 additions & 0 deletions util/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ class Database {
);
});
}
async getCatById(catId) {
return new Promise((resolve, reject) => {
this.db.get(
`SELECT id, breed_name, image_url, description, wikipedia_url, count
FROM msg
WHERE id = ?`,
[catId],
(err, row) => {
if (err) {
console.error("Ошибка при получении данных кота:", err);
reject(err);
}
resolve(row);
}
);
});
}
}

export default new Database();

0 comments on commit 087a8e4

Please sign in to comment.