feat(back): blueprints added + example simple module
This commit is contained in:
@@ -12,6 +12,7 @@ if (config.enableCors) {
|
||||
}
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
// Global middleware
|
||||
app.use(authorizationMiddleware as any); // TODO: move out of here
|
||||
app.use((req, _res, next) => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { configDotenv } from "dotenv";
|
||||
configDotenv();
|
||||
|
||||
const config = {
|
||||
enableCors: false,
|
||||
enableCors: true,
|
||||
port: process.env.PORT || 3000,
|
||||
baseRoute: "/api/v1",
|
||||
};
|
||||
|
||||
@@ -2,11 +2,11 @@ import mysql, { QueryError } from "mysql2/promise";
|
||||
import { ResponseError } from "./utils/response/response-error.model";
|
||||
|
||||
const db_pool = mysql.createPool({
|
||||
host: "localhost",
|
||||
port: 3307,
|
||||
user: "dbuser",
|
||||
password: "securepassword",
|
||||
database: "path",
|
||||
host: process.env.DB_HOST ?? "localhost",
|
||||
port: parseInt(process.env.DB_PORT ?? "3307"),
|
||||
user: process.env.DB_USERNAME ?? "dbuser",
|
||||
password: process.env.DB_PASSWORD ?? "securepassword",
|
||||
database: process.env.DB_MAIN ?? "path",
|
||||
});
|
||||
|
||||
async function DB_Query<T>(query: string): Promise<Partial<T>[]> {
|
||||
|
||||
@@ -14,6 +14,7 @@ async function authorization(req: Request, res: Response, next: NextFunction) {
|
||||
permission.roles.includes(Role.Public)
|
||||
);
|
||||
});
|
||||
|
||||
if (isPublic) {
|
||||
return next();
|
||||
}
|
||||
@@ -74,6 +75,11 @@ const authorizationTable: Permissions[] = [
|
||||
resource: "^/users.*",
|
||||
actions: ["GET"],
|
||||
},
|
||||
{
|
||||
roles: [Role.Public],
|
||||
resource: "^/example.*",
|
||||
actions: ["GET", "OPTIONS"],
|
||||
},
|
||||
];
|
||||
|
||||
export { authorization as authorizationMiddleware };
|
||||
|
||||
@@ -1,12 +1,40 @@
|
||||
import { Router } from "express";
|
||||
|
||||
import ExampleService from "./example.service";
|
||||
import { ResponseSuccess } from "../../utils/response/response-success.model";
|
||||
import { ResponseError } from "../../utils/response/response-error.model";
|
||||
|
||||
export const exampleRoutes = Router();
|
||||
|
||||
exampleRoutes.get("/", (req, res) => {
|
||||
res.status(400);
|
||||
res.send("Something");
|
||||
const exampleService = new ExampleService();
|
||||
|
||||
exampleRoutes.get("/", async (_, res) => {
|
||||
try {
|
||||
const response = await exampleService.getAllExamples();
|
||||
res.status(200);
|
||||
res.send(new ResponseSuccess(response));
|
||||
} catch (e) {
|
||||
res.status(500);
|
||||
res.send(e);
|
||||
}
|
||||
});
|
||||
|
||||
exampleRoutes.post("/new", (req, res) => {
|
||||
res.send("Something with " + JSON.stringify(req.body));
|
||||
exampleRoutes.get("/:id", async (req, res) => {
|
||||
try {
|
||||
const response = await exampleService.getExampleById(
|
||||
parseInt(req.params.id),
|
||||
);
|
||||
if (response) {
|
||||
res.status(200);
|
||||
res.send(new ResponseSuccess(response));
|
||||
} else {
|
||||
res.status(404);
|
||||
res.send(new ResponseError({ code: "NOT_FOUND", number: 404 }));
|
||||
}
|
||||
} catch (e) {
|
||||
res.status(500);
|
||||
res.send(e);
|
||||
}
|
||||
});
|
||||
|
||||
export { exampleService };
|
||||
|
||||
23
back-express/src/modules/example/example.service.ts
Normal file
23
back-express/src/modules/example/example.service.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { db_query } from "../../db";
|
||||
import { Example } from "./example.types";
|
||||
|
||||
class ExampleService {
|
||||
constructor(private info?: string) {}
|
||||
|
||||
async getAllExamples(): Promise<Example[]> {
|
||||
const examples = await db_query("select * from example");
|
||||
return examples as Example[];
|
||||
}
|
||||
|
||||
async getExampleById(id: number): Promise<Example | null> {
|
||||
const example = await db_query(
|
||||
`select * from example as example WHERE id = ${id};`,
|
||||
);
|
||||
if (example.length) {
|
||||
return example[0] as Example;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export default ExampleService;
|
||||
7
back-express/src/modules/example/example.types.ts
Normal file
7
back-express/src/modules/example/example.types.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export type Example = {
|
||||
id: number;
|
||||
name: string;
|
||||
description: string;
|
||||
image: string;
|
||||
created_at: string;
|
||||
};
|
||||
@@ -1,9 +1,9 @@
|
||||
import express from "express";
|
||||
|
||||
import { defaultRoute } from "./default.route";
|
||||
import { exampleRoutes } from "./example/example.routes";
|
||||
import { userRoutes } from "./users/users.routes";
|
||||
import { authRoutes } from "./auth/auth.routes";
|
||||
import { exampleRoutes } from "./example/example.routes";
|
||||
|
||||
export const routes = express.Router();
|
||||
|
||||
@@ -11,6 +11,6 @@ export const routes = express.Router();
|
||||
* Routes
|
||||
* */
|
||||
routes.use("/", defaultRoute);
|
||||
routes.use("/example", exampleRoutes);
|
||||
routes.use("/users", userRoutes);
|
||||
routes.use("/auth", authRoutes);
|
||||
routes.use("/example", exampleRoutes);
|
||||
|
||||
@@ -12,7 +12,7 @@ const code_error_mapping: Record<number, BasicError> = {
|
||||
status: 400,
|
||||
},
|
||||
401: {
|
||||
code: "Unauthorized",
|
||||
code: "UNAUTHORIZED",
|
||||
status: 401,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user