92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
import { DatabaseInterface } from "../../../database/DatabaseInterface";
|
|
import { AccountEntity } from "../entity/AccountEntity";
|
|
import { AccountRepositoryInterface } from "./AccountRepositoryInterface";
|
|
|
|
export default class AccountRepository implements AccountRepositoryInterface {
|
|
constructor(private readonly database: DatabaseInterface) {}
|
|
|
|
async findByEmail(email: string): Promise<AccountEntity | null> {
|
|
const sql = `
|
|
SELECT id, email, password, role_id, created_at, updated_at
|
|
FROM accounts
|
|
WHERE email = $1
|
|
`;
|
|
|
|
const result = await this.database.fetchOne<{
|
|
id: string;
|
|
email: string;
|
|
password: string;
|
|
role_id: number;
|
|
created_at: Date;
|
|
updated_at: Date;
|
|
}>(sql, [email]);
|
|
|
|
if (!result) {
|
|
return null;
|
|
}
|
|
|
|
return new AccountEntity(
|
|
result.id,
|
|
result.email,
|
|
result.password,
|
|
result.role_id,
|
|
result.created_at,
|
|
result.updated_at,
|
|
);
|
|
}
|
|
|
|
async save(account: AccountEntity): Promise<string> {
|
|
const sql = `
|
|
INSERT INTO accounts (id, email, password, role_id, created_at, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
email = EXCLUDED.email,
|
|
password = EXCLUDED.password,
|
|
role_id = EXCLUDED.role_id,
|
|
updated_at = EXCLUDED.updated_at
|
|
RETURNING id
|
|
`;
|
|
|
|
const result = await this.database.fetchOne<{ id: string }>(sql, [
|
|
account.id,
|
|
account.email,
|
|
account.hashedPassword,
|
|
account.roleId,
|
|
account.createdAt,
|
|
account.updatedAt,
|
|
]);
|
|
|
|
return result!.id;
|
|
}
|
|
|
|
async findById(id: string): Promise<AccountEntity | null> {
|
|
const sql = `
|
|
SELECT id, email, password, role_id, created_at, updated_at
|
|
FROM accounts
|
|
WHERE id = $1
|
|
`;
|
|
|
|
const result = await this.database.fetchOne<{
|
|
id: string;
|
|
email: string;
|
|
password: string;
|
|
role_id: number;
|
|
created_at: Date;
|
|
updated_at: Date;
|
|
}>(sql, [id]);
|
|
|
|
if (!result) {
|
|
return null;
|
|
}
|
|
|
|
return new AccountEntity(
|
|
result.id,
|
|
result.email,
|
|
result.password,
|
|
result.role_id,
|
|
result.created_at,
|
|
result.updated_at,
|
|
);
|
|
}
|
|
}
|