From bccfa81c4cf3d0d370b5fe00eb49f076bb337544 Mon Sep 17 00:00:00 2001 From: Mairis Berzins Date: Sun, 14 Jul 2024 17:48:55 +0300 Subject: [PATCH] reorganize api core modules --- packages/api/src/core/app-data-source.ts | 13 --------- .../api/src/core/{ => auto-mapper}/mapper.ts | 6 ++-- packages/api/src/core/{ => config}/config.ts | 0 packages/api/src/core/configFn.ts | 4 --- .../core/{ => controllers}/base.controller.ts | 0 packages/api/src/core/db/app-data-source.ts | 13 +++++++++ .../api/src/core/{ => db}/base.entity.type.ts | 0 packages/api/src/core/inversify.config.ts | 28 ------------------- .../api/src/core/inversify/config-function.ts | 3 ++ packages/api/src/core/inversify/container.ts | 28 +++++++++++++++++++ .../error-config-function.ts} | 2 +- .../identifiers.ts} | 0 .../src/core/{ => services}/crud.service.ts | 4 +-- .../core/{ => services}/crud.service.type.ts | 0 .../api/src/features/auth/auth.controller.ts | 10 +++---- .../api/src/features/auth/auth.provider.ts | 6 ++-- .../api/src/features/auth/auth.service.ts | 4 +-- .../api/src/features/card/card.controller.ts | 2 +- .../api/src/features/card/card.repository.ts | 2 +- .../api/src/features/card/card.service.ts | 2 +- .../src/features/card/card.service.type.ts | 2 +- .../api/src/features/game/game.controller.ts | 2 +- .../api/src/features/game/game.repository.ts | 2 +- .../api/src/features/user/user.controller.ts | 2 +- .../api/src/features/user/user.repository.ts | 2 +- packages/api/src/index.ts | 16 +++++------ 26 files changed, 76 insertions(+), 77 deletions(-) delete mode 100644 packages/api/src/core/app-data-source.ts rename packages/api/src/core/{ => auto-mapper}/mapper.ts (74%) rename packages/api/src/core/{ => config}/config.ts (100%) delete mode 100644 packages/api/src/core/configFn.ts rename packages/api/src/core/{ => controllers}/base.controller.ts (100%) create mode 100644 packages/api/src/core/db/app-data-source.ts rename packages/api/src/core/{ => db}/base.entity.type.ts (100%) delete mode 100644 packages/api/src/core/inversify.config.ts create mode 100644 packages/api/src/core/inversify/config-function.ts create mode 100644 packages/api/src/core/inversify/container.ts rename packages/api/src/core/{errorConfigFn.ts => inversify/error-config-function.ts} (87%) rename packages/api/src/core/{inversify.identifiers.ts => inversify/identifiers.ts} (100%) rename packages/api/src/core/{ => services}/crud.service.ts (93%) rename packages/api/src/core/{ => services}/crud.service.type.ts (100%) diff --git a/packages/api/src/core/app-data-source.ts b/packages/api/src/core/app-data-source.ts deleted file mode 100644 index e5a0910..0000000 --- a/packages/api/src/core/app-data-source.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { DataSource } from 'typeorm'; -import { Game } from '../features/game/game.entity'; -import { User } from '../features/user/user.entity'; -import { Auth } from '../features/auth/auth.entity'; -import { Card } from '../features/card/card.entity'; - -export const appDataSource = new DataSource({ - type: 'sqlite', - database: 'sqlite.db', - entities: [User, Auth, Game, Card], - synchronize: true, - logging: false, -}); diff --git a/packages/api/src/core/mapper.ts b/packages/api/src/core/auto-mapper/mapper.ts similarity index 74% rename from packages/api/src/core/mapper.ts rename to packages/api/src/core/auto-mapper/mapper.ts index 477312e..1cf1551 100644 --- a/packages/api/src/core/mapper.ts +++ b/packages/api/src/core/auto-mapper/mapper.ts @@ -1,9 +1,9 @@ import { classes } from '@automapper/classes'; import { createMap, createMapper } from '@automapper/core'; import { CardDto, GameDto, UserDto } from '@tspark/common'; -import { Card } from '../features/card/card.entity'; -import { Game } from '../features/game/game.entity'; -import { User } from '../features/user/user.entity'; +import { Card } from '../../features/card/card.entity'; +import { Game } from '../../features/game/game.entity'; +import { User } from '../../features/user/user.entity'; export const mapper = createMapper({ strategyInitializer: classes(), diff --git a/packages/api/src/core/config.ts b/packages/api/src/core/config/config.ts similarity index 100% rename from packages/api/src/core/config.ts rename to packages/api/src/core/config/config.ts diff --git a/packages/api/src/core/configFn.ts b/packages/api/src/core/configFn.ts deleted file mode 100644 index 6937c18..0000000 --- a/packages/api/src/core/configFn.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { Application } from 'express'; -import { ConfigFunction } from 'inversify-express-utils'; - -export const configFn: ConfigFunction = () => {}; diff --git a/packages/api/src/core/base.controller.ts b/packages/api/src/core/controllers/base.controller.ts similarity index 100% rename from packages/api/src/core/base.controller.ts rename to packages/api/src/core/controllers/base.controller.ts diff --git a/packages/api/src/core/db/app-data-source.ts b/packages/api/src/core/db/app-data-source.ts new file mode 100644 index 0000000..619b688 --- /dev/null +++ b/packages/api/src/core/db/app-data-source.ts @@ -0,0 +1,13 @@ +import { DataSource } from 'typeorm'; +import { Game } from '../../features/game/game.entity'; +import { User } from '../../features/user/user.entity'; +import { Auth } from '../../features/auth/auth.entity'; +import { Card } from '../../features/card/card.entity'; + +export const appDataSource = new DataSource({ + type: 'sqlite', + database: 'sqlite.db', + entities: [User, Auth, Game, Card], + synchronize: true, + logging: false, +}); diff --git a/packages/api/src/core/base.entity.type.ts b/packages/api/src/core/db/base.entity.type.ts similarity index 100% rename from packages/api/src/core/base.entity.type.ts rename to packages/api/src/core/db/base.entity.type.ts diff --git a/packages/api/src/core/inversify.config.ts b/packages/api/src/core/inversify.config.ts deleted file mode 100644 index 028d9a2..0000000 --- a/packages/api/src/core/inversify.config.ts +++ /dev/null @@ -1,28 +0,0 @@ -import '../features/auth/auth.controller'; -import '../features/card/card.controller'; -import '../features/game/game.controller'; -import '../features/user/user.controller'; - -import { Container } from 'inversify'; -import { BaseMiddleware } from 'inversify-express-utils'; -import { AuthMiddleware } from '../features/auth/auth.middleware'; -import { AuthService } from '../features/auth/auth.service'; -import { IAuthService } from '../features/auth/auth.service.type'; -import { CardService } from '../features/card/card.service'; -import { ICardService } from '../features/card/card.service.type'; -import { GameServiceMock } from '../features/game/game.service.mock'; -import { IGameService } from '../features/game/game.service.type'; -import { UserService } from '../features/user/user.service'; -import { IUserService } from '../features/user/user.service.type'; -import { Middleware, Services } from './inversify.identifiers'; - -const container = new Container(); - -container.bind(Middleware.Auth).to(AuthMiddleware); - -container.bind(Services.Auth).to(AuthService); -container.bind(Services.Card).to(CardService); -container.bind(Services.Game).to(GameServiceMock); -container.bind(Services.User).to(UserService); - -export { container }; diff --git a/packages/api/src/core/inversify/config-function.ts b/packages/api/src/core/inversify/config-function.ts new file mode 100644 index 0000000..0772a43 --- /dev/null +++ b/packages/api/src/core/inversify/config-function.ts @@ -0,0 +1,3 @@ +import { ConfigFunction } from 'inversify-express-utils'; + +export const configFunction: ConfigFunction = () => {}; diff --git a/packages/api/src/core/inversify/container.ts b/packages/api/src/core/inversify/container.ts new file mode 100644 index 0000000..7b31bd3 --- /dev/null +++ b/packages/api/src/core/inversify/container.ts @@ -0,0 +1,28 @@ +import '../../features/auth/auth.controller'; +import '../../features/card/card.controller'; +import '../../features/game/game.controller'; +import '../../features/user/user.controller'; + +import { Container } from 'inversify'; +import { BaseMiddleware } from 'inversify-express-utils'; +import { AuthMiddleware } from '../../features/auth/auth.middleware'; +import { AuthService } from '../../features/auth/auth.service'; +import { IAuthService } from '../../features/auth/auth.service.type'; +import { CardService } from '../../features/card/card.service'; +import { ICardService } from '../../features/card/card.service.type'; +import { GameServiceMock } from '../../features/game/game.service.mock'; +import { IGameService } from '../../features/game/game.service.type'; +import { UserService } from '../../features/user/user.service'; +import { IUserService } from '../../features/user/user.service.type'; +import { Middleware, Services } from './identifiers'; + +const container = new Container(); + +container.bind(Middleware.Auth).to(AuthMiddleware); + +container.bind(Services.Auth).to(AuthService); +container.bind(Services.Card).to(CardService); +container.bind(Services.Game).to(GameServiceMock); +container.bind(Services.User).to(UserService); + +export { container }; diff --git a/packages/api/src/core/errorConfigFn.ts b/packages/api/src/core/inversify/error-config-function.ts similarity index 87% rename from packages/api/src/core/errorConfigFn.ts rename to packages/api/src/core/inversify/error-config-function.ts index 82c4cef..6f60aea 100644 --- a/packages/api/src/core/errorConfigFn.ts +++ b/packages/api/src/core/inversify/error-config-function.ts @@ -2,7 +2,7 @@ import { ErrorResponse } from '@tspark/common'; import { NextFunction, Request, Response } from 'express'; import { ConfigFunction } from 'inversify-express-utils'; -export const errorConfigFn: ConfigFunction = (app) => { +export const errorConfigFunction: ConfigFunction = (app) => { app.use( ( err: Error, diff --git a/packages/api/src/core/inversify.identifiers.ts b/packages/api/src/core/inversify/identifiers.ts similarity index 100% rename from packages/api/src/core/inversify.identifiers.ts rename to packages/api/src/core/inversify/identifiers.ts diff --git a/packages/api/src/core/crud.service.ts b/packages/api/src/core/services/crud.service.ts similarity index 93% rename from packages/api/src/core/crud.service.ts rename to packages/api/src/core/services/crud.service.ts index 286b826..6e195d3 100644 --- a/packages/api/src/core/crud.service.ts +++ b/packages/api/src/core/services/crud.service.ts @@ -1,8 +1,8 @@ import { injectable } from 'inversify'; import { FindOptionsWhere, ObjectLiteral, Repository } from 'typeorm'; -import { IBaseEntity } from './base.entity.type'; +import { mapper } from '../auto-mapper/mapper'; +import { IBaseEntity } from '../db/base.entity.type'; import { ICrudService } from './crud.service.type'; -import { mapper } from './mapper'; @injectable() export abstract class CrudService diff --git a/packages/api/src/core/crud.service.type.ts b/packages/api/src/core/services/crud.service.type.ts similarity index 100% rename from packages/api/src/core/crud.service.type.ts rename to packages/api/src/core/services/crud.service.type.ts diff --git a/packages/api/src/features/auth/auth.controller.ts b/packages/api/src/features/auth/auth.controller.ts index d04b59d..168b732 100644 --- a/packages/api/src/features/auth/auth.controller.ts +++ b/packages/api/src/features/auth/auth.controller.ts @@ -2,12 +2,12 @@ import { AuthCheckResponse, CookieKeys, UserDto } from '@tspark/common'; import { inject } from 'inversify'; import { controller, httpGet, httpPost } from 'inversify-express-utils'; import jwt from 'jsonwebtoken'; -import { BaseController } from '../../core/base.controller'; -import { config } from '../../core/config'; -import { Services } from '../../core/inversify.identifiers'; -import { IAuthService } from './auth.service.type'; -import { mapper } from '../../core/mapper'; +import { mapper } from '../../core/auto-mapper/mapper'; +import { config } from '../../core/config/config'; +import { BaseController } from '../../core/controllers/base.controller'; +import { Services } from '../../core/inversify/identifiers'; import { User } from '../user/user.entity'; +import { IAuthService } from './auth.service.type'; @controller('/auth') export class AuthController extends BaseController { diff --git a/packages/api/src/features/auth/auth.provider.ts b/packages/api/src/features/auth/auth.provider.ts index 2ac5cfb..6163e2a 100644 --- a/packages/api/src/features/auth/auth.provider.ts +++ b/packages/api/src/features/auth/auth.provider.ts @@ -1,9 +1,9 @@ import { ICookies, UserDto } from '@tspark/common'; import { Request } from 'express'; import { inject, injectable } from 'inversify'; -import { BaseController } from '../../core/base.controller'; -import { Services } from '../../core/inversify.identifiers'; -import { mapper } from '../../core/mapper'; +import { mapper } from '../../core/auto-mapper/mapper'; +import { BaseController } from '../../core/controllers/base.controller'; +import { Services } from '../../core/inversify/identifiers'; import { User } from '../user/user.entity'; import { IAuthProvider } from './auth.provider.type'; import { IAuthService } from './auth.service.type'; diff --git a/packages/api/src/features/auth/auth.service.ts b/packages/api/src/features/auth/auth.service.ts index 3a32c58..499b0a5 100644 --- a/packages/api/src/features/auth/auth.service.ts +++ b/packages/api/src/features/auth/auth.service.ts @@ -2,8 +2,8 @@ import { LoginRequest, RegisterRequest } from '@tspark/common'; import bcrypt from 'bcrypt'; import { inject, injectable } from 'inversify'; import jwt, { JwtPayload } from 'jsonwebtoken'; -import { config } from '../../core/config'; -import { Services } from '../../core/inversify.identifiers'; +import { config } from '../../core/config/config'; +import { Services } from '../../core/inversify/identifiers'; import { User } from '../user/user.entity'; import { userRepository } from '../user/user.repository'; import { IUserService } from '../user/user.service.type'; diff --git a/packages/api/src/features/card/card.controller.ts b/packages/api/src/features/card/card.controller.ts index c9d5b16..c6afe93 100644 --- a/packages/api/src/features/card/card.controller.ts +++ b/packages/api/src/features/card/card.controller.ts @@ -12,7 +12,7 @@ import { requestParam, response, } from 'inversify-express-utils'; -import { Middleware, Services } from '../../core/inversify.identifiers'; +import { Middleware, Services } from '../../core/inversify/identifiers'; import { ICardService } from './card.service.type'; @controller('/card', Middleware.Auth) diff --git a/packages/api/src/features/card/card.repository.ts b/packages/api/src/features/card/card.repository.ts index dd4e8b3..6a61df9 100644 --- a/packages/api/src/features/card/card.repository.ts +++ b/packages/api/src/features/card/card.repository.ts @@ -1,4 +1,4 @@ -import { appDataSource } from '../../core/app-data-source'; +import { appDataSource } from '../../core/db/app-data-source'; import { Card } from './card.entity'; export const cardRepository = appDataSource.getRepository(Card); diff --git a/packages/api/src/features/card/card.service.ts b/packages/api/src/features/card/card.service.ts index b599448..519aab8 100644 --- a/packages/api/src/features/card/card.service.ts +++ b/packages/api/src/features/card/card.service.ts @@ -1,7 +1,7 @@ import { CardDto } from '@tspark/common'; import { injectable } from 'inversify'; import { Repository } from 'typeorm'; -import { CrudService } from '../../core/crud.service'; +import { CrudService } from '../../core/services/crud.service'; import { Card } from './card.entity'; import { cardRepository } from './card.repository'; import { ICardService } from './card.service.type'; diff --git a/packages/api/src/features/card/card.service.type.ts b/packages/api/src/features/card/card.service.type.ts index f0420a7..9646591 100644 --- a/packages/api/src/features/card/card.service.type.ts +++ b/packages/api/src/features/card/card.service.type.ts @@ -1,4 +1,4 @@ import { CardDto } from '@tspark/common'; -import { ICrudService } from '../../core/crud.service.type'; +import { ICrudService } from '../../core/services/crud.service.type'; export interface ICardService extends ICrudService {} diff --git a/packages/api/src/features/game/game.controller.ts b/packages/api/src/features/game/game.controller.ts index 85b5982..7e51d64 100644 --- a/packages/api/src/features/game/game.controller.ts +++ b/packages/api/src/features/game/game.controller.ts @@ -4,8 +4,8 @@ import { controller, httpGet, } from 'inversify-express-utils'; +import { Services } from '../../core/inversify/identifiers'; import { IGameService } from './game.service.type'; -import { Services } from '../../core/inversify.identifiers'; @controller('/game') export class GameController extends BaseHttpController { diff --git a/packages/api/src/features/game/game.repository.ts b/packages/api/src/features/game/game.repository.ts index 1a60791..681e95a 100644 --- a/packages/api/src/features/game/game.repository.ts +++ b/packages/api/src/features/game/game.repository.ts @@ -1,4 +1,4 @@ -import { appDataSource } from '../../core/app-data-source'; +import { appDataSource } from '../../core/db/app-data-source'; import { Game } from './game.entity'; export const gameRepository = appDataSource.getRepository(Game); diff --git a/packages/api/src/features/user/user.controller.ts b/packages/api/src/features/user/user.controller.ts index d5c68e4..ef0c96b 100644 --- a/packages/api/src/features/user/user.controller.ts +++ b/packages/api/src/features/user/user.controller.ts @@ -4,8 +4,8 @@ import { controller, httpGet, } from 'inversify-express-utils'; +import { Services } from '../../core/inversify/identifiers'; import { IUserService } from './user.service.type'; -import { Services } from '../../core/inversify.identifiers'; @controller('/user') export class UserController extends BaseHttpController { diff --git a/packages/api/src/features/user/user.repository.ts b/packages/api/src/features/user/user.repository.ts index 07adcdc..791e01d 100644 --- a/packages/api/src/features/user/user.repository.ts +++ b/packages/api/src/features/user/user.repository.ts @@ -1,4 +1,4 @@ -import { appDataSource } from '../../core/app-data-source'; +import { appDataSource } from '../../core/db/app-data-source'; import { User } from './user.entity'; export const userRepository = appDataSource.getRepository(User); diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index d06eaea..a3d2f6f 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -6,12 +6,12 @@ import cookieParser from 'cookie-parser'; import cors from 'cors'; import express from 'express'; import { InversifyExpressServer } from 'inversify-express-utils'; -import { appDataSource } from './core/app-data-source'; -import { config } from './core/config'; -import { configFn } from './core/configFn'; -import { errorConfigFn } from './core/errorConfigFn'; -import { container } from './core/inversify.config'; -import { createMaps } from './core/mapper'; +import { createMaps } from './core/auto-mapper/mapper'; +import { config } from './core/config/config'; +import { appDataSource } from './core/db/app-data-source'; +import { configFunction } from './core/inversify/config-function'; +import { container } from './core/inversify/container'; +import { errorConfigFunction } from './core/inversify/error-config-function'; import { AuthProvider } from './features/auth/auth.provider'; createMaps(); @@ -47,8 +47,8 @@ const server = new InversifyExpressServer( AuthProvider, ); -server.setConfig(configFn); -server.setErrorConfig(errorConfigFn); +server.setConfig(configFunction); +server.setErrorConfig(errorConfigFunction); appDataSource .initialize()