Skip to content

Commit d221555

Browse files
authored
Merge pull request #103 from BinaryStudioAcademy/task/thjs-56-move-functional-routes-to-controllers
thjs-56: Move functional routes to controllers
2 parents b8d7d72 + 2c71629 commit d221555

File tree

48 files changed

+555
-387
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+555
-387
lines changed

server/knexfile.js

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,3 @@
1-
import { knexSnakeCaseMappers } from 'objection';
1+
import { database } from '#libs/packages/database/database.js';
22

3-
import { AppEnvironment } from '#libs/enums/enums.js';
4-
5-
import { config } from './src/libs/packages/config/config.js';
6-
7-
const {
8-
DATABASE: database,
9-
TEST_DATABASE: testDatabase,
10-
USERNAME: username,
11-
PASSWORD: password,
12-
HOST: host,
13-
PORT: port,
14-
CLIENT: client,
15-
DEBUG: debug
16-
} = config.ENV.DB;
17-
18-
const DEFAULT_ENV_CONFIG = {
19-
client,
20-
connection: {
21-
user: username,
22-
port,
23-
host,
24-
database,
25-
password
26-
},
27-
migrations: {
28-
directory: './src/db/migrations',
29-
tableName: 'knex_migrations'
30-
},
31-
seeds: {
32-
directory: './src/db/seeds'
33-
},
34-
debug,
35-
...knexSnakeCaseMappers({ underscoreBetweenUppercaseLetters: true })
36-
};
37-
38-
const knexConfig = {
39-
[AppEnvironment.DEVELOPMENT]: DEFAULT_ENV_CONFIG,
40-
[AppEnvironment.PRODUCTION]: DEFAULT_ENV_CONFIG,
41-
[AppEnvironment.TEST]: {
42-
...DEFAULT_ENV_CONFIG,
43-
connection: {
44-
...DEFAULT_ENV_CONFIG.connection,
45-
database: testDatabase
46-
}
47-
}
48-
};
49-
50-
export default knexConfig;
3+
export default database.environmentsConfig;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getJoinedNormalizedPath } from '#libs/packages/path/path.js';
2+
3+
class Controller {
4+
#apiPath;
5+
6+
#routes = [];
7+
8+
get routes() {
9+
return this.#routes;
10+
}
11+
12+
constructor({ apiPath }) {
13+
this.#apiPath = apiPath;
14+
}
15+
16+
addRoute = ({ url, ...options }) => {
17+
this.#routes.push({
18+
url: getJoinedNormalizedPath([this.#apiPath, url]),
19+
...options
20+
});
21+
};
22+
}
23+
24+
export { Controller };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export { Controller } from './controller.api.js';
12
export { ControllerHook } from './libs/enums/enums.js';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
import { config } from '#libs/packages/config/config.js';
2+
3+
import { Database } from './database.package.js';
4+
5+
const database = new Database({ config });
6+
7+
export { database };
18
export { Abstract as AbstractModel } from './abstract.model.js';
29
export { Abstract as AbstractRepository } from './abstract.repository.js';
310
export { DatabaseTableName } from './libs/enums/enums.js';
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import Knex from 'knex';
2+
import { knexSnakeCaseMappers, Model } from 'objection';
3+
4+
import { AppEnvironment } from '#libs/enums/enums.js';
5+
6+
class Database {
7+
#config;
8+
9+
#knex;
10+
11+
constructor({ config }) {
12+
this.#config = config;
13+
}
14+
15+
get knex() {
16+
return this.#knex;
17+
}
18+
19+
connect() {
20+
const knex = Knex(this.environmentConfig);
21+
Model.knex(knex);
22+
23+
this.#knex = knex;
24+
}
25+
26+
get environmentsConfig() {
27+
const { TEST_DATABASE: testDatabase } = this.#config.ENV.DB;
28+
29+
return {
30+
[AppEnvironment.DEVELOPMENT]: this.initialConfig,
31+
[AppEnvironment.PRODUCTION]: this.initialConfig,
32+
[AppEnvironment.TEST]: {
33+
...this.initialConfig,
34+
connection: {
35+
...this.initialConfig.connection,
36+
database: testDatabase
37+
}
38+
}
39+
};
40+
}
41+
42+
get initialConfig() {
43+
const {
44+
DATABASE: database,
45+
USERNAME: username,
46+
PASSWORD: password,
47+
HOST: host,
48+
PORT: port,
49+
CLIENT: client,
50+
DEBUG: debug
51+
} = this.#config.ENV.DB;
52+
53+
return {
54+
client,
55+
connection: {
56+
user: username,
57+
port,
58+
host,
59+
database,
60+
password
61+
},
62+
migrations: {
63+
directory: './src/db/migrations',
64+
tableName: 'knex_migrations'
65+
},
66+
seeds: {
67+
directory: './src/db/seeds'
68+
},
69+
debug,
70+
...knexSnakeCaseMappers({ underscoreBetweenUppercaseLetters: true })
71+
};
72+
}
73+
74+
get environmentConfig() {
75+
return this.environmentsConfig[this.#config.ENV.APP.ENVIRONMENT];
76+
}
77+
}
78+
79+
export { Database };

server/tests/libs/packages/path/libs/helpers/get-joined-normalized-path/get-joined-normalized-path.helper.js renamed to server/src/libs/packages/path/libs/helpers/get-joined-normalized-path/get-joined-normalized-path.helper.js

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { getJoinedNormalizedPath } from './get-joined-normalized-path/get-joined-normalized-path.helper.js';
2+
export { joinPath } from './join-path/join-path.helper.js';
3+
export { normalizeTrailingSlash } from './normalize-trailing-slash/normalize-trailing-slash.helper.js';

server/tests/libs/packages/path/libs/helpers/join-path/join-path.helper.js renamed to server/src/libs/packages/path/libs/helpers/join-path/join-path.helper.js

File renamed without changes.

server/tests/libs/packages/path/libs/helpers/normalize-trailing-slash/normalize-trailing-slash.helper.js renamed to server/src/libs/packages/path/libs/helpers/normalize-trailing-slash/normalize-trailing-slash.helper.js

File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
getJoinedNormalizedPath,
3+
joinPath,
4+
normalizeTrailingSlash
5+
} from './libs/helpers/helpers.js';

0 commit comments

Comments
 (0)