From 818151b0b02ccc15caec9ce2e571a0c16b01a10e Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 8 Apr 2024 22:17:32 -0400 Subject: [PATCH] Add some classes and bootstrap the routes in the controller class. --- app/index.ts | 4 ++ app/src/api/controller.ts | 5 ++ app/src/api/user-controller.ts | 55 +++++++++++++++++++ app/src/api/user-routes.ts | 11 ---- app/src/domain/repository.ts | 13 +++++ app/src/domain/repository/image-repository.ts | 14 +++-- app/src/domain/repository/user-repository.ts | 36 +++++++++--- .../{database-pool.ts => connection-pool.ts} | 0 app/src/server.ts | 9 ++- 9 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 app/index.ts create mode 100644 app/src/api/controller.ts create mode 100644 app/src/api/user-controller.ts delete mode 100644 app/src/api/user-routes.ts rename app/src/infrastructure/{database-pool.ts => connection-pool.ts} (100%) diff --git a/app/index.ts b/app/index.ts new file mode 100644 index 0000000..5070c88 --- /dev/null +++ b/app/index.ts @@ -0,0 +1,4 @@ +import { server } from '@slovo/server'; +server.listen(3000, () => { + console.log("Running server on port 3000"); +}); diff --git a/app/src/api/controller.ts b/app/src/api/controller.ts new file mode 100644 index 0000000..a77268b --- /dev/null +++ b/app/src/api/controller.ts @@ -0,0 +1,5 @@ +import type { Express } from 'express'; + +export interface Controller { + bootstrapRoutes(app: Express): void; +} diff --git a/app/src/api/user-controller.ts b/app/src/api/user-controller.ts new file mode 100644 index 0000000..88feb16 --- /dev/null +++ b/app/src/api/user-controller.ts @@ -0,0 +1,55 @@ +import type { + Express, + Request, + Response +} from "express"; +import type { Controller } from '@slovo/api/controller'; +import { UserRepository } from '@slovo/domain/repository/user-repository'; +import type { Pool } from 'mariadb'; + +export class UserController implements Controller { + private userRepository: UserRepository; + + public constructor(pool: Pool) { + this.userRepository = new UserRepository(pool); + } + + public bootstrapRoutes(app: Express): void { + app.get('/users', this.getUsers); + app.get('/user/:id', this.getUser); + app.post('/user', this.createUser); + app.put('/user', this.updateUser); + app.put('/login', this.loginUser); + } + + public async getUsers(req: Request, res: Response): Promise { + const users = await this.userRepository.getAll(); + + if (!users.size) { + res.status(404); + } + + return res.json({ users }); + } + + public async getUser(req: Request, res: Response): Promise { + + return res; + } + + public async createUser(req: Request, res: Response): Promise { + + return res; + } + + public async updateUser(req: Reqst, res: Response): Promise { + + return res; + } + + public async loginUser(req: Request, res: Response): Promise { + + return res; + } +} + diff --git a/app/src/api/user-routes.ts b/app/src/api/user-routes.ts deleted file mode 100644 index e577234..0000000 --- a/app/src/api/user-routes.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { Request, Response, RequestHandler } from "express"; - -const getUsers: RequestHandler = function (req: Request, res: Response) { - console.log(req.method); - res.json({ users: [] }); -} - -export { - getUsers -} - diff --git a/app/src/domain/repository.ts b/app/src/domain/repository.ts index 7d0ce9c..31bf756 100644 --- a/app/src/domain/repository.ts +++ b/app/src/domain/repository.ts @@ -1,4 +1,5 @@ import { Entity, type EntityId } from '@slovo/domain/entity'; +import type { Pool } from 'mariadb'; export interface Repository { get(id: EntityId): Promise>; @@ -7,3 +8,15 @@ export interface Repository { delete(model: Entity): Promise; } +export abstract class BaseRepository implements Repository { + protected pool: Pool; + + public constructor(pool: Pool) { + this.pool = pool; + } + + public abstract get(id: EntityId): Promise>; + public abstract save(model: T): Promise>; + public abstract update(model: Entity): Promise>; + public abstract delete(model: Entity): Promise; +} diff --git a/app/src/domain/repository/image-repository.ts b/app/src/domain/repository/image-repository.ts index 6910d74..e7db633 100644 --- a/app/src/domain/repository/image-repository.ts +++ b/app/src/domain/repository/image-repository.ts @@ -1,12 +1,16 @@ -import { type Repository } from '@slovo/domain/repository'; +import { BaseRepository } from '@slovo/domain/repository'; import { Entity, type EntityId } from '@slovo/domain/entity'; -import { Image } from '@slovo/models/image'; -import connectionPool from '@slovo/infrastructure/connection-pool'; +import type { Image } from '@slovo/models/image'; +import type { PoolConnection } from 'mariadb'; -export class ImageRepository implements Repository { +export class ImageRepository extends BaseRepository { public async get(id: EntityId): Promise> { - const conn = await connectionPool.getConnection(); + const conn = await this.pool.getConnection(); await conn.release(); } + + public async create(image: Image): Promise> { + + } } diff --git a/app/src/domain/repository/user-repository.ts b/app/src/domain/repository/user-repository.ts index d313070..e025c07 100644 --- a/app/src/domain/repository/user-repository.ts +++ b/app/src/domain/repository/user-repository.ts @@ -1,12 +1,11 @@ -import { type Repository } from '@slovo/domain/repository'; +import { BaseRepository } from '@slovo/domain/repository'; import { Entity, type EntityId } from '@slovo/domain/entity'; import { type User } from '@slovo/models/user'; import { type PoolConnection } from 'mariadb'; -import connectionPool from '@slovo/infrastructure/database-pool'; -export class UserRepository implements Repository { +export class UserRepository extends BaseRepository { public async get(id: EntityId): Promise> { - const conn: PoolConnection = await connectionPool.getConnection(); + const conn: PoolConnection = await this.pool.getConnection(); const query = `SELECT * FROM users WHERE id = ?`; const rows = await conn.query(query, [ id ]); @@ -27,8 +26,31 @@ export class UserRepository implements Repository { return new Entity(id, user); } + public async getAll(): Promise>> { + const conn: PoolConnection = await this.pool.getConnection(); + + const query = `SELECT * FROM users`; + const rows = await conn.query(query); + + const users = new Set>(); + + if (!rows.length) { + return users; + } + + for (const row of rows) { + const user: Entity = new Entity(row.id, { + email: row.email, + name: row.name, + }); + users.add(user); + } + + return users; + } + public async save(user: User): Promise> { - const conn: PoolConnection = await connectionPool.getConnection(); + const conn: PoolConnection = await this.pool.getConnection(); const query = `INSERT INTO users (name, email, password) VALUES (?, ?, ?)`; const rows = await conn.query(query, [ user.name, user.email, user.password ]); @@ -42,7 +64,7 @@ export class UserRepository implements Repository { } public async update(user: Entity): Promise> { - const conn: PoolConnection = await connectionPool.getConnection(); + const conn: PoolConnection = await this.pool.getConnection(); const query = `UPDATE user SET name = ?, email = ?, password = ? WHERE id = ?`; const results = await conn.query(query, [ @@ -61,7 +83,7 @@ export class UserRepository implements Repository { } public async delete(user: Entity): Promise { - const conn: PoolConnection = await connectionPool.getConnection(); + const conn: PoolConnection = await this.pool.getConnection(); await conn.release(); } diff --git a/app/src/infrastructure/database-pool.ts b/app/src/infrastructure/connection-pool.ts similarity index 100% rename from app/src/infrastructure/database-pool.ts rename to app/src/infrastructure/connection-pool.ts diff --git a/app/src/server.ts b/app/src/server.ts index ba4a8b8..a051841 100644 --- a/app/src/server.ts +++ b/app/src/server.ts @@ -3,14 +3,17 @@ import express, { type Request, type Response } from "express"; +import { connectionPool } from '@slovo/infrastructure/connection-pool'; const server: Express = express(); +// bootstrap controllers +import { UserController } from '@slovo/api/user-controller'; +new UserController(connectionPool).bootstrapRoutes(server); + server.get("/", async (req: Request, res: Response) => { console.log(req); return res.json({ message: "Hello!" }); }); -server.listen(3000, () => { - console.log("Running on 3000"); -}); +export { server };