Skip to content

Commit 2cca108

Browse files
committed
[js][data_repository][02_repository] Extract repository
1 parent b60e7fa commit 2cca108

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export class MySqlUserRepository {
2+
mysqlHost = "localhost";
3+
mysqlUser = "root";
4+
mysqlPassword = "";
5+
mysqlDatabase = "super_project";
6+
mysqlPort = 3306;
7+
8+
constructor() {
9+
const Mysql = require('sync-mysql');
10+
this.connection = new Mysql({
11+
host: this.mysqlHost,
12+
user: this.mysqlUser,
13+
password: this.mysqlPassword,
14+
database: this.mysqlDatabase,
15+
port: this.mysqlPort
16+
});
17+
}
18+
19+
searchAll() {
20+
return this.connection.query('SELECT * FROM users');
21+
}
22+
}

examples/js/js-data_repository-02_repository/src/UserSearcher.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class UsersSearcher {
2+
constructor(repository) {
3+
this.repository = repository;
4+
}
5+
6+
searchAll() {
7+
return this.repository.searchAll();
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class FakeUserRepository {
2+
constructor(users) {
3+
this.users = users;
4+
}
5+
6+
searchAll() {
7+
return this.users;
8+
}
9+
}

examples/js/js-data_repository-02_repository/tests/UserSearcher.test.js

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {FakeUserRepository} from "./FakeUserRepository";
2+
import {UsersSearcher} from "../src/UsersSearcher";
3+
4+
describe('UsersSearcher should', () => {
5+
it('search all existing users', () => {
6+
const existingUsers = [{"name": "javi"}, {"name": "rafa"}];
7+
8+
const repository = new FakeUserRepository(existingUsers);
9+
const searcher = new UsersSearcher(repository);
10+
11+
expect(searcher.searchAll()).toBe(existingUsers);
12+
});
13+
});

0 commit comments

Comments
 (0)