Skip to content

Commit

Permalink
reorganize api core modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mairisb committed Jul 14, 2024
1 parent 22936f8 commit bccfa81
Show file tree
Hide file tree
Showing 26 changed files with 76 additions and 77 deletions.
13 changes: 0 additions & 13 deletions packages/api/src/core/app-data-source.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
File renamed without changes.
4 changes: 0 additions & 4 deletions packages/api/src/core/configFn.ts

This file was deleted.

File renamed without changes.
13 changes: 13 additions & 0 deletions packages/api/src/core/db/app-data-source.ts
Original file line number Diff line number Diff line change
@@ -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,
});
File renamed without changes.
28 changes: 0 additions & 28 deletions packages/api/src/core/inversify.config.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/api/src/core/inversify/config-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ConfigFunction } from 'inversify-express-utils';

export const configFunction: ConfigFunction = () => {};
28 changes: 28 additions & 0 deletions packages/api/src/core/inversify/container.ts
Original file line number Diff line number Diff line change
@@ -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<BaseMiddleware>(Middleware.Auth).to(AuthMiddleware);

container.bind<IAuthService>(Services.Auth).to(AuthService);
container.bind<ICardService>(Services.Card).to(CardService);
container.bind<IGameService>(Services.Game).to(GameServiceMock);
container.bind<IUserService>(Services.User).to(UserService);

export { container };
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<E extends IBaseEntity & ObjectLiteral, EDto>
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions packages/api/src/features/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/features/auth/auth.provider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/features/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/features/card/card.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/features/card/card.repository.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion packages/api/src/features/card/card.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/features/card/card.service.type.ts
Original file line number Diff line number Diff line change
@@ -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<CardDto> {}
2 changes: 1 addition & 1 deletion packages/api/src/features/game/game.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/features/game/game.repository.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion packages/api/src/features/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/features/user/user.repository.ts
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 8 additions & 8 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -47,8 +47,8 @@ const server = new InversifyExpressServer(
AuthProvider,
);

server.setConfig(configFn);
server.setErrorConfig(errorConfigFn);
server.setConfig(configFunction);
server.setErrorConfig(errorConfigFunction);

appDataSource
.initialize()
Expand Down

0 comments on commit bccfa81

Please sign in to comment.