Skip to content

Commit

Permalink
Add routes
Browse files Browse the repository at this point in the history
Co-Authored-By: Finn Kötting <69923589+finnkoetting@users.noreply.github.com>
  • Loading branch information
mezotv and finnkoetting committed May 28, 2023
1 parent b86b4d5 commit e97888d
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 35 deletions.
1 change: 1 addition & 0 deletions Ghibli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npm run start
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "src/index.ts",
"scripts": {
"test": "ts-node src/index.ts --resolveJsonModule --trace-deprication",
"start": "ts-node src/index.ts --resolveJsonModule --trace-deprication",
"format": "prettier --write \"src/**/*.ts\""
},
"repository": {
Expand Down
Binary file added src/assets/img/favicon-ghibli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions src/assets/img/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 11 additions & 15 deletions src/assets/js/swagger.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
/* eslint-disable */
window.addEventListener('load', () => {
const logoElement = document.querySelector('.topbar img');
if (logoElement)
logoElement.setAttribute(
'src',
'https://cdn.discordapp.com/attachments/1052712829028933752/1052712974927794196/Group_160.png'
);
window.addEventListener("load", () => {
const logoElement = document.querySelector(".topbar img");
if (logoElement) logoElement.setAttribute("src", "/assets/img/logo.svg");

document.head.querySelectorAll('link').forEach((elem) => {
if (elem.getAttribute('rel') == 'icon') {
elem.setAttribute('href', 'https://ticketerbot.com/logo.svg');
document.head.querySelectorAll("link").forEach((elem) => {
if (elem.getAttribute("rel") == "icon") {
elem.setAttribute("href", "/assets/img/favicon-ghibli.png");
}
});

window.scrollTo({ x: 0, y: 0, scrollBehavior: 'smooth' });
window.scrollTo({ x: 0, y: 0, scrollBehavior: "smooth" });

document.querySelectorAll('.opblock-tag').forEach((elem) => {
document.querySelectorAll(".opblock-tag").forEach((elem) => {
elem.click();
});

while (document.querySelectorAll('.opblock.is-open').length > 0) {
document.querySelectorAll('.opblock').forEach((elem) => {
if (elem.classList.contains('is-open')) elem.click();
while (document.querySelectorAll(".opblock.is-open").length > 0) {
document.querySelectorAll(".opblock").forEach((elem) => {
if (elem.classList.contains("is-open")) elem.click();
});
}
});
19 changes: 19 additions & 0 deletions src/docs/films.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// FILM API DOCUMENTATION

/**
* @swagger
* tags:
* name: Film
* description: The Film API
*/

/**
* @swagger
* /films/:id:
* get:
* summary: Sends you specific information about a film
* tags: [Film]
* responses:
* 200:
* description: Film found
*/
19 changes: 19 additions & 0 deletions src/docs/locations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// LOCATION API DOCUMENTATION

/**
* @swagger
* tags:
* name: Location
* description: The Location API
*/

/**
* @swagger
* /locations/:id:
* get:
* summary: Sends you specific information about a location
* tags: [Location]
* responses:
* 200:
* description: Location found
*/
19 changes: 19 additions & 0 deletions src/docs/people.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// PEOPLE API DOCUMENTATION

/**
* @swagger
* tags:
* name: People
* description: The People API
*/

/**
* @swagger
* /people/:id:
* get:
* summary: Sends you specific information about a person
* tags: [People]
* responses:
* 200:
* description: People found
*/
19 changes: 19 additions & 0 deletions src/docs/species.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPECIES API DOCUMENTATION

/**
* @swagger
* tags:
* name: Species
* description: The Species API
*/

/**
* @swagger
* /species/:id:
* get:
* summary: Sends you specific information about a species
* tags: [Species]
* responses:
* 200:
* description: Species found
*/
19 changes: 19 additions & 0 deletions src/docs/vehicles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// VEHICLE API DOCUMENTATION

/**
* @swagger
* tags:
* name: Vehicles
* description: The Vehicles API
*/

/**
* @swagger
* /vehicles/:id:
* get:
* summary: Sends you specific information about a vehicle
* tags: [Vehicles]
* responses:
* 200:
* description: Vehicle found
*/
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dotenv.config();

// Kill process if port is in use
try {
kill(3001, "tcp");
kill(6543, "tcp");
} catch (error) {
/* Empty */
}
Expand All @@ -21,9 +21,9 @@ dotenv.config();
// Initialize express
let api: Express = express();

// Initialize swaggerUI
require("./util/Swagger").default(api);

// Initialize Routes
require("./util/Middleware").default(api);
api.use("/", require("./util/RoutesManager").default(api));

// Initialize swaggerUI
require("./util/Swagger").default(api);
2 changes: 1 addition & 1 deletion src/routes/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default (urlName: string): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}`, (req: Request, res: Response) => {
res.send("Online");
res.redirect("/docs");
});

return api;
Expand Down
15 changes: 13 additions & 2 deletions src/routes/films/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}:id`, (req: Request, res: Response) => {
const searchData = data.films.filter((v: any) => v.id == req.params.id);
let searchData = [];
if (req.params.id.split("-").length == 5) {
searchData = data.films.filter((f: any) => f.id == req.params.id);
} else {
searchData = data.films.filter((f: any) =>
f.title.includes(req.params.id)
);
}

res.json(searchData);
if (searchData.length == 0) {
res.send("null");
} else {
res.json(searchData);
}
});

return api;
Expand Down
15 changes: 13 additions & 2 deletions src/routes/locations/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}:id`, (req: Request, res: Response) => {
const searchData = data.locations.filter((v: any) => v.id == req.params.id);
let searchData = [];
if (req.params.id.split("-").length == 5) {
searchData = data.locations.filter((l: any) => l.id == req.params.id);
} else {
searchData = data.locations.filter((l: any) =>
l.name.includes(req.params.id)
);
}

res.json(searchData);
if (searchData.length == 0) {
res.send("null");
} else {
res.json(searchData);
}
});

return api;
Expand Down
15 changes: 13 additions & 2 deletions src/routes/people/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}:id`, (req: Request, res: Response) => {
const searchData = data.people.filter((v: any) => v.id == req.params.id);
let searchData = [];
if (req.params.id.split("-").length == 5) {
searchData = data.people.filter((p: any) => p.id == req.params.id);
} else {
searchData = data.people.filter((p: any) =>
p.name.includes(req.params.id)
);
}

res.json(searchData);
if (searchData.length == 0) {
res.send("null");
} else {
res.json(searchData);
}
});

return api;
Expand Down
15 changes: 13 additions & 2 deletions src/routes/species/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}:id`, (req: Request, res: Response) => {
const searchData = data.species.filter((v: any) => v.id == req.params.id);
let searchData = [];
if (req.params.id.split("-").length == 5) {
searchData = data.species.filter((s: any) => s.id == req.params.id);
} else {
searchData = data.species.filter((s: any) =>
s.name.includes(req.params.id)
);
}

res.json(searchData);
if (searchData.length == 0) {
res.send("null");
} else {
res.json(searchData);
}
});

return api;
Expand Down
15 changes: 13 additions & 2 deletions src/routes/vehicles/.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ export default (urlName: string, data: any): Router => {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
api.get(`/${urlName}:id`, (req: Request, res: Response) => {
const searchData = data.vehicles.filter((v: any) => v.id == req.params.id);
let searchData = [];
if (req.params.id.split("-").length == 5) {
searchData = data.vehicles.filter((v: any) => v.id == req.params.id);
} else {
searchData = data.vehicles.filter((v: any) =>
v.name.includes(req.params.id)
);
}

res.json(searchData);
if (searchData.length == 0) {
res.send("null");
} else {
res.json(searchData);
}
});

return api;
Expand Down
10 changes: 7 additions & 3 deletions src/util/Middleware.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* Import packages */
const chalk = require("chalk");
import { Express, Request, Response, NextFunction } from "express";
import express, { Express, Request, Response, NextFunction } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import { join } from "path";

// Export middleware
export default (api: Express): Express => {
// Set trust proxy
api.set("trust proxy", 1);

// Static files
api.use("/assets", express.static(join(__dirname, "..", "assets")));

// Parse body
api.use(bodyParser.json());
api.use(bodyParser.urlencoded({ extended: true }));
Expand Down Expand Up @@ -38,11 +42,11 @@ export default (api: Express): Express => {
}

// Set port
api.listen(3001, () => {
api.listen(6543, () => {
console.log(
chalk.red("API") +
chalk.grey(" | ") +
chalk.white(`Listening on port ${chalk.greenBright("3001")}`)
chalk.white(`Listening on port ${chalk.greenBright("6543")}`)
);
});

Expand Down
2 changes: 0 additions & 2 deletions src/util/RoutesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const chalk = require("chalk");
export default (): Router => {
const api: Router = Router();

api.use("/assets", express.static(join(__dirname, "..", "assets")));

// Read route directory
const routeDir = readdirSync(join(__dirname, "..", "routes"));
const dataRaw = readFileSync(join(__dirname, "data", "data.json"));
Expand Down

0 comments on commit e97888d

Please sign in to comment.