diff --git a/app/bun.lockb b/app/bun.lockb index 68b9299..0ba25ec 100755 Binary files a/app/bun.lockb and b/app/bun.lockb differ diff --git a/app/package.json b/app/package.json index 0b8f5b6..b07ebb0 100644 --- a/app/package.json +++ b/app/package.json @@ -10,6 +10,8 @@ }, "dependencies": { "@types/express": "^4.17.21", - "express": "^4.19.2" + "bcrypt": "^5.1.1", + "express": "^4.19.2", + "mariadb": "^3.3.0" } } \ No newline at end of file diff --git a/app/src/domain/entity.ts b/app/src/domain/entity.ts new file mode 100644 index 0000000..e4c64cb --- /dev/null +++ b/app/src/domain/entity.ts @@ -0,0 +1,19 @@ +export type EntityId = number|string; + +export class Entity { + protected readonly id: EntityId; + protected model: T; + + public constructor(id: EntityId, model: T) { + this.id = id; + this.model = model; + } + + public getId(): EntityId { + return this.id; + } + + public getModel(): T { + return this.model; + } +} diff --git a/app/src/domain/repository.ts b/app/src/domain/repository.ts index 61fdf82..7d0ce9c 100644 --- a/app/src/domain/repository.ts +++ b/app/src/domain/repository.ts @@ -1,7 +1,9 @@ +import { Entity, type EntityId } from '@slovo/domain/entity'; + export interface Repository { - get(id: any): Promise; - save(model: T): Promise; - update(model: T): Promise; - delete(model: T): Promise; + get(id: EntityId): Promise>; + save(model: T): Promise>; + update(model: Entity): Promise>; + delete(model: Entity): Promise; } diff --git a/app/src/domain/repository/user-repository.ts b/app/src/domain/repository/user-repository.ts new file mode 100644 index 0000000..9bb9f81 --- /dev/null +++ b/app/src/domain/repository/user-repository.ts @@ -0,0 +1,63 @@ +import { type Repository } 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 { + public async get(id: EntityId): Promise> { + const conn: PoolConnection = await connectionPool.getConnection(); + + const query = `SELECT * FROM users WHERE id = ?`; + const rows = await conn.query(query, [ id ]); + + if (!rows.length) { + throw Error("Unable to find User"); + } + + const row = rows.shift(); + + const user: User = { + name: row.name, + email: row.email, + password: row.password + }; + + await conn.release(); + return new Entity(id, user); + } + + public async save(user: User): Promise> { + const conn: PoolConnection = await connectionPool.getConnection(); + + const query = `INSERT INTO users (name, email, password) VALUES (?, ?, ?)`; + const rows = await conn.query(query, [ user.name, user.email, user.password ]); + + if (!rows.length) { + throw Error("Unable to create User"); + } + + await conn.release(); + return new Entity(rows.insertId, user); + } + + public async update(user: Entity): Promise> { + const conn: PoolConnection = await connectionPool.getConnection(); + + const query = `UPDATE user SET name = ?, email = ?, password = ? WHERE id = ?`; + const results = await conn.query(query, [ + user.getModel().name, + user.getModel().email, + user.getModel().password, + user.getId() + ]); + + await conn.release(); + } + + public async delete(user: Entity): Promise { + const conn: PoolConnection = await connectionPool.getConnection(); + + await conn.release(); + } +} diff --git a/app/src/infrastructure/database-pool.ts b/app/src/infrastructure/database-pool.ts new file mode 100644 index 0000000..d7b96e8 --- /dev/null +++ b/app/src/infrastructure/database-pool.ts @@ -0,0 +1,10 @@ +import { createPool } from 'mariadb'; + +const connectionPool = createPool({ + host: process.env.DB_HOST, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_SCHEMA +}); + +export default connectionPool; diff --git a/app/src/models/image.ts b/app/src/models/image.ts index 47a8147..2426101 100644 --- a/app/src/models/image.ts +++ b/app/src/models/image.ts @@ -1,6 +1,6 @@ export type Image = { url: URL, - title: string, + title?: string, width: number, height: number, }; diff --git a/app/src/server.ts b/app/src/server.ts index 6c55812..ba4a8b8 100644 --- a/app/src/server.ts +++ b/app/src/server.ts @@ -1,4 +1,8 @@ -import express, { Express, type Request, type Response } from "express"; +import express, { + type Express, + type Request, + type Response +} from "express"; const server: Express = express();