Compare commits
2 Commits
76b5599809
...
1195e050da
Author | SHA1 | Date | |
---|---|---|---|
1195e050da | |||
2062f2074c |
@@ -0,0 +1,121 @@
|
||||
# Backend API docs
|
||||
|
||||
If you want to coorperate with me, or build something new with my backend API, feel free to reach out!
|
||||
|
||||
On this page you will learn how my API works.
|
||||
|
||||
## General information
|
||||
|
||||
When you look at my backend folder and file structure, yu can see that I have two files called `API`. The first file called `api.js` is for my web frontend. Because this file works together with my JWT token service.
|
||||
|
||||
But I have build a second API. You can see the second API file in the same directory, the file is called `apiV2.js`.
|
||||
|
||||
This is the file that you can use to build an API.
|
||||
|
||||
But first you have to get the Admin API key, stored in an .env file on my server.
|
||||
|
||||
### Current endpoints
|
||||
|
||||
- /apiV2/items/`secretKey`
|
||||
|
||||
#### /apiV2/items/
|
||||
|
||||
When you call this API you will get like this result back:
|
||||
|
||||
```
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"item_name": "DJI 1er Mikro",
|
||||
"can_borrow_role": "4",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"item_name": "DJI 2er Mikro 1",
|
||||
"can_borrow_role": "4",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"item_name": "DJI 2er Mikro 2",
|
||||
"can_borrow_role": "4",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"item_name": "Rode Richt Mikrofon",
|
||||
"can_borrow_role": "2",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"item_name": "Kamera Stativ",
|
||||
"can_borrow_role": "1",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 7,
|
||||
"item_name": "SONY Kamera - inkl. Akkus und Objektiv",
|
||||
"can_borrow_role": "1",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 8,
|
||||
"item_name": "MacBook inkl. Adapter",
|
||||
"can_borrow_role": "2",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 9,
|
||||
"item_name": "SD Karten",
|
||||
"can_borrow_role": "3",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 10,
|
||||
"item_name": "Kameragimbal",
|
||||
"can_borrow_role": "1",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 11,
|
||||
"item_name": "ATEM MINI PRO",
|
||||
"can_borrow_role": "1",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"item_name": "Handygimbal",
|
||||
"can_borrow_role": "4",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 13,
|
||||
"item_name": "Kameralüfter",
|
||||
"can_borrow_role": "1",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 14,
|
||||
"item_name": "Kleine Kamera 1 - inkl. Objektiv",
|
||||
"can_borrow_role": "2",
|
||||
"inSafe": 1
|
||||
},
|
||||
{
|
||||
"id": 15,
|
||||
"item_name": "Kleine Kamera 2 - inkl. Objektiv",
|
||||
"can_borrow_role": "2",
|
||||
"inSafe": 1
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
There you can see all items and their details.
|
||||
|
||||
Each item has the following properties:
|
||||
|
||||
- `id`: The unique identifier for the item.
|
||||
- `item_name`: The name of the item.
|
||||
- `can_borrow_role`: The role ID that is allowed to borrow the item.
|
||||
- `inSafe`: Indicates whether the item is currently in the locker (1) or not (0).
|
||||
|
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 cors from "cors";
|
||||
import env from "dotenv";
|
||||
import apiRouter from "./routes/api.js";
|
||||
import apiRouterV2 from "./routes/apiV2.js";
|
||||
env.config();
|
||||
const app = express();
|
||||
const port = 8002;
|
||||
@@ -11,10 +13,8 @@ app.use(express.urlencoded({ extended: true, limit: "10mb" }));
|
||||
app.set("view engine", "ejs");
|
||||
app.use(express.json({ limit: "10mb" }));
|
||||
|
||||
// Import API router
|
||||
import apiRouter from "./routes/api.js";
|
||||
|
||||
app.use("/api", apiRouter);
|
||||
app.use("/apiV2", apiRouterV2);
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
res.render("index.ejs");
|
||||
|
@@ -2,7 +2,6 @@ import mysql from "mysql2";
|
||||
import dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
// Ein einzelner Pool reicht; der zweite Pool benutzte fälschlich DB_TABLE als Datenbank
|
||||
const pool = mysql
|
||||
.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
@@ -21,6 +20,14 @@ export const loginFunc = async (username, password) => {
|
||||
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) => {
|
||||
const sql =
|
||||
role == 0
|
||||
@@ -34,4 +41,3 @@ export const getItemsFromDatabase = async (role) => {
|
||||
}
|
||||
return { success: false };
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user