Add some classes and bootstrap the routes in the controller class.
This commit is contained in:
parent
82e1512ccb
commit
818151b0b0
4
app/index.ts
Normal file
4
app/index.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { server } from '@slovo/server';
|
||||
server.listen(3000, () => {
|
||||
console.log("Running server on port 3000");
|
||||
});
|
5
app/src/api/controller.ts
Normal file
5
app/src/api/controller.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import type { Express } from 'express';
|
||||
|
||||
export interface Controller {
|
||||
bootstrapRoutes(app: Express): void;
|
||||
}
|
55
app/src/api/user-controller.ts
Normal file
55
app/src/api/user-controller.ts
Normal file
@ -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<Response> {
|
||||
const users = await this.userRepository.getAll();
|
||||
|
||||
if (!users.size) {
|
||||
res.status(404);
|
||||
}
|
||||
|
||||
return res.json({ users });
|
||||
}
|
||||
|
||||
public async getUser(req: Request, res: Response): Promise<Response> {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async createUser(req: Request, res: Response): Promise<Response> {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async updateUser(req: Reqst, res: Response): Promise<Response> {
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
public async loginUser(req: Request, res: Response): Promise<Response> {
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { Entity, type EntityId } from '@slovo/domain/entity';
|
||||
import type { Pool } from 'mariadb';
|
||||
|
||||
export interface Repository<T> {
|
||||
get(id: EntityId): Promise<Entity<T>>;
|
||||
@ -7,3 +8,15 @@ export interface Repository<T> {
|
||||
delete(model: Entity<T>): Promise<boolean>;
|
||||
}
|
||||
|
||||
export abstract class BaseRepository<T> implements Repository<T> {
|
||||
protected pool: Pool;
|
||||
|
||||
public constructor(pool: Pool) {
|
||||
this.pool = pool;
|
||||
}
|
||||
|
||||
public abstract get(id: EntityId): Promise<Entity<T>>;
|
||||
public abstract save(model: T): Promise<Entity<T>>;
|
||||
public abstract update(model: Entity<T>): Promise<Entity<T>>;
|
||||
public abstract delete(model: Entity<T>): Promise<boolean>;
|
||||
}
|
||||
|
@ -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<Image> {
|
||||
export class ImageRepository extends BaseRepository<Image> {
|
||||
public async get(id: EntityId): Promise<Entity<Image>> {
|
||||
const conn = await connectionPool.getConnection();
|
||||
const conn = await this.pool.getConnection();
|
||||
|
||||
await conn.release();
|
||||
}
|
||||
|
||||
public async create(image: Image): Promise<Entity<Image>> {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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<User> {
|
||||
export class UserRepository extends BaseRepository<User> {
|
||||
public async get(id: EntityId): Promise<Entity<User>> {
|
||||
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<User> {
|
||||
return new Entity(id, user);
|
||||
}
|
||||
|
||||
public async getAll(): Promise<Set<Entity<User>>> {
|
||||
const conn: PoolConnection = await this.pool.getConnection();
|
||||
|
||||
const query = `SELECT * FROM users`;
|
||||
const rows = await conn.query(query);
|
||||
|
||||
const users = new Set<Entity<User>>();
|
||||
|
||||
if (!rows.length) {
|
||||
return users;
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
const user: Entity<User> = new Entity(row.id, {
|
||||
email: row.email,
|
||||
name: row.name,
|
||||
});
|
||||
users.add(user);
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
public async save(user: User): Promise<Entity<User>> {
|
||||
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<User> {
|
||||
}
|
||||
|
||||
public async update(user: Entity<User>): Promise<Entity<User>> {
|
||||
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<User> {
|
||||
}
|
||||
|
||||
public async delete(user: Entity<User>): Promise<boolean> {
|
||||
const conn: PoolConnection = await connectionPool.getConnection();
|
||||
const conn: PoolConnection = await this.pool.getConnection();
|
||||
|
||||
await conn.release();
|
||||
}
|
||||
|
@ -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 };
|
||||
|
Loading…
Reference in New Issue
Block a user