Added second external API endpoint and corrosponding file.
This commit is contained in:
21
backend/routes/apiV2.js
Normal file
21
backend/routes/apiV2.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import express from "express";
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
import { getItemsFromDatabaseV2 } from "../services/database.js";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/items/:id", async (req, res) => {
|
||||||
|
if (req.params.id === process.env.ADMIN_ID) {
|
||||||
|
const result = await getItemsFromDatabaseV2();
|
||||||
|
if (result.success) {
|
||||||
|
res.status(200).json(result.data);
|
||||||
|
} else {
|
||||||
|
res.status(500).json({ message: "Failed to fetch items" });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(403).json({ message: "Access denied" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
@@ -1,6 +1,8 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import env from "dotenv";
|
import env from "dotenv";
|
||||||
|
import apiRouter from "./routes/api.js";
|
||||||
|
import apiRouterV2 from "./routes/apiV2.js";
|
||||||
env.config();
|
env.config();
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 8002;
|
const port = 8002;
|
||||||
@@ -11,10 +13,8 @@ app.use(express.urlencoded({ extended: true, limit: "10mb" }));
|
|||||||
app.set("view engine", "ejs");
|
app.set("view engine", "ejs");
|
||||||
app.use(express.json({ limit: "10mb" }));
|
app.use(express.json({ limit: "10mb" }));
|
||||||
|
|
||||||
// Import API router
|
|
||||||
import apiRouter from "./routes/api.js";
|
|
||||||
|
|
||||||
app.use("/api", apiRouter);
|
app.use("/api", apiRouter);
|
||||||
|
app.use("/apiV2", apiRouterV2);
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.render("index.ejs");
|
res.render("index.ejs");
|
||||||
|
@@ -2,7 +2,6 @@ import mysql from "mysql2";
|
|||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
// Ein einzelner Pool reicht; der zweite Pool benutzte fälschlich DB_TABLE als Datenbank
|
|
||||||
const pool = mysql
|
const pool = mysql
|
||||||
.createPool({
|
.createPool({
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
@@ -21,6 +20,14 @@ export const loginFunc = async (username, password) => {
|
|||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const getItemsFromDatabaseV2 = async () => {
|
||||||
|
const [rows] = await pool.query("SELECT * FROM items;");
|
||||||
|
if (rows.length > 0) {
|
||||||
|
return { success: true, data: rows };
|
||||||
|
}
|
||||||
|
return { success: false };
|
||||||
|
};
|
||||||
|
|
||||||
export const getItemsFromDatabase = async (role) => {
|
export const getItemsFromDatabase = async (role) => {
|
||||||
const sql =
|
const sql =
|
||||||
role == 0
|
role == 0
|
||||||
@@ -34,4 +41,3 @@ export const getItemsFromDatabase = async (role) => {
|
|||||||
}
|
}
|
||||||
return { success: false };
|
return { success: false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user