Skip to content

Commit 2598a6f

Browse files
committed
Added source code files
1 parent 59f79c2 commit 2598a6f

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

src/app.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import express from "express";
2+
import { env } from "config/env";
3+
import Controller from "interfaces/controller.interface";
4+
5+
class App {
6+
private app: express.Application;
7+
8+
constructor(controllers: Controller[]) {
9+
this.app = express();
10+
11+
this.initializeMiddlewares();
12+
this.initializeControllers(controllers);
13+
}
14+
15+
private initializeMiddlewares() {
16+
this.app.use(express.json());
17+
}
18+
19+
private initializeControllers(controllers: Controller[]) {
20+
controllers.forEach((controller) => {
21+
this.app.use("/api/v1", controller.router);
22+
});
23+
}
24+
25+
public listen() {
26+
this.app.listen(env.PORT, () => {
27+
console.log(`App listening on the port ${env.PORT}`);
28+
});
29+
}
30+
}
31+
32+
export default App;

src/config/database.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Pool } from "pg";
2+
import { env } from "./env";
3+
4+
const pool = new Pool({
5+
host: env.POSTGRES_HOST,
6+
user: env.POSTGRES_USER,
7+
password: env.POSTGRES_PASSWORD,
8+
database: env.POSTGRES_DB,
9+
port: env.POSTGRES_PORT,
10+
idleTimeoutMillis: 30000,
11+
});
12+
13+
export default pool;

src/config/env.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { config } from "dotenv";
2+
import { cleanEnv, port, str } from "envalid";
3+
4+
config();
5+
6+
export const env = cleanEnv(process.env, {
7+
POSTGRES_USER: str(),
8+
POSTGRES_PASSWORD: str(),
9+
POSTGRES_HOST: str(),
10+
POSTGRES_PORT: port({ default: 5432 }),
11+
POSTGRES_DB: str(),
12+
13+
PORT: port({ default: 5000 }),
14+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Router } from "express";
2+
3+
interface Controller {
4+
path: string;
5+
router: Router;
6+
}
7+
8+
export default Controller;

src/server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import App from "./app";
2+
import UsersController from "users/users.controller";
3+
4+
const app = new App([new UsersController()]);
5+
app.listen();

src/users/users.controller.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pool from "config/database";
2+
import { Router, Request, Response, NextFunction } from "express";
3+
4+
class UsersController {
5+
public path = "/users";
6+
public router = Router();
7+
8+
constructor() {
9+
this.initializeRoutes();
10+
}
11+
12+
public initializeRoutes() {
13+
this.router.get(this.path, this.getUsers);
14+
this.router.post(this.path, this.createUser);
15+
}
16+
17+
private createUser = async (
18+
request: Request,
19+
response: Response,
20+
next: NextFunction
21+
) => {
22+
const { username, email } = request.body;
23+
try {
24+
const insertUser =
25+
"INSERT INTO users (username, email) VALUES ($1, $2) RETURNING *";
26+
const result = await pool.query(insertUser, [username, email]);
27+
28+
const createdUser = result.rows[0];
29+
return response.json(createdUser);
30+
} catch (err) {
31+
next(err);
32+
}
33+
};
34+
35+
private getUsers = async (
36+
_: Request,
37+
response: Response,
38+
next: NextFunction
39+
) => {
40+
try {
41+
const result = await pool.query("SELECT id, username, email FROM users");
42+
const users = result.rows;
43+
return response.json(users);
44+
} catch (err) {
45+
next(err);
46+
}
47+
};
48+
}
49+
50+
export default UsersController;

0 commit comments

Comments
 (0)