Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rcole1919 committed Oct 23, 2024
1 parent 9116fe1 commit ac225c5
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 43 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
HOST=
PORT=
SALT=
DB_HOST=
Expand Down
12 changes: 3 additions & 9 deletions src/rest/base-controller.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,15 @@ import { ILogger } from '../shared/libs/logger/types/index.js';
@injectable()
export abstract class BaseController implements IController {
private readonly DEFAULT_CONTENT_TYPE = 'application/json';
private readonly _router: Router;
public readonly router: Router = Router();

constructor(
protected readonly logger: ILogger,
) {
this._router = Router();
}

get router(): Router {
return this._router;
}
) {}

public addRoute(route: IRoute): void {
const wrapperAsyncHandler = asyncHandler(route.handler.bind(this));
this._router[route.method](route.path, wrapperAsyncHandler);
this.router[route.method](route.path, wrapperAsyncHandler);
this.logger.info(`Route registered: ${route.method.toUpperCase()} ${route.path}`);
}

Expand Down
8 changes: 3 additions & 5 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { IController, IExceptionFilter } from './types/index.js';

@injectable()
export class RestApplication {
private readonly server: Express;
private readonly server: Express = express();

constructor(
@inject(COMPONENT.LOGGER) private readonly logger: ILogger,
Expand All @@ -19,9 +19,7 @@ export class RestApplication {
@inject(COMPONENT.OFFER_CONTROLLER) private readonly offerController: IController,
@inject(COMPONENT.USER_CONTROLLER) private readonly userController: IController,
@inject(COMPONENT.EXCEPTION_FILTER) private readonly appExceptionFilter: IExceptionFilter,
) {
this.server = express();
}
) {}

private async initDb() {
const mongoUri = getMongoURI(
Expand Down Expand Up @@ -74,6 +72,6 @@ export class RestApplication {

this.logger.info('Try to init server...');
await this.initServer();
this.logger.info(`Server started on http://localhost:${this.config.get('PORT')}`);
this.logger.info(`Server started on ${this.config.get('HOST')}:${this.config.get('PORT')}`);
}
}
2 changes: 1 addition & 1 deletion src/rest/types/route.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import { EHttpMethod } from './index.js';
export interface IRoute {
path: string;
method: EHttpMethod;
handler: (req: Request, res: Response, next: NextFunction) => void;
handler: (req: Request, res: Response, next: NextFunction) => Promise<void> | void;
}
26 changes: 13 additions & 13 deletions src/shared/constants/component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
export const COMPONENT = {
REST_APPLICATION: Symbol.for('RestApplication'),
LOGGER: Symbol.for('Logger'),
CONFIG: Symbol.for('Config'),
DATABASE_CLIENT: Symbol.for('DatabaseClient'),
USER_SERVICE: Symbol.for('UserService'),
USER_MODEL: Symbol.for('UserModel'),
OFFER_MODEL: Symbol.for('OfferModel'),
OFFER_SERVICE: Symbol.for('OfferService'),
COMMENT_MODEL: Symbol.for('CommentModel'),
COMMENT_SERVICE: Symbol.for('CommentService'),
OFFER_CONTROLLER: Symbol.for('OfferController'),
EXCEPTION_FILTER: Symbol.for('ExceptionFilter'),
USER_CONTROLLER: Symbol.for('UserController'),
REST_APPLICATION: Symbol(),
LOGGER: Symbol(),
CONFIG: Symbol(),
DATABASE_CLIENT: Symbol(),
USER_SERVICE: Symbol(),
USER_MODEL: Symbol(),
OFFER_MODEL: Symbol(),
OFFER_SERVICE: Symbol(),
COMMENT_MODEL: Symbol(),
COMMENT_SERVICE: Symbol(),
OFFER_CONTROLLER:Symbol(),
EXCEPTION_FILTER:Symbol(),
USER_CONTROLLER: Symbol(),
};
6 changes: 6 additions & 0 deletions src/shared/libs/config/rest.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import { TRestSchema } from './types/index.js';
convict.addFormats(validator);

export const configRestSchema = convict<TRestSchema>({
HOST: {
doc: 'Service hostname',
format: String,
env: 'HOST',
default: '',
},
PORT: {
doc: 'Port for incoming connections',
format: 'port',
Expand Down
1 change: 1 addition & 0 deletions src/shared/libs/config/types/rest-schema.type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type TRestSchema = {
HOST: string;
PORT: number;
SALT: string;
DB_HOST: string;
Expand Down
3 changes: 1 addition & 2 deletions src/shared/libs/database-client/mongo.database-client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as Mongoose from 'mongoose';
import pkg from 'mongoose';
import { inject, injectable } from 'inversify';
import { setTimeout } from 'node:timers/promises';

import { IDatabaseClient } from './types/index.js';
import { COMPONENT, DB_CONNECT_RETRY } from '../../constants/index.js';
import { ILogger } from '../logger/types/index.js';

const { ConnectionStates } = pkg;
const { ConnectionStates } = Mongoose;

@injectable()
export class MongoDatabaseClient implements IDatabaseClient {
Expand Down
5 changes: 3 additions & 2 deletions src/shared/modules/offer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { OfferEntity, OfferModel } from './offer.entity.js';
export { CreateOfferDTO } from './dto/create-offer.dto.js';
export { UpdateOfferDTO } from './dto/update-offer.dto.js';
export { ShortOfferRDO } from './rdo/short-offer.rdo.js';
export { FullOfferRDO } from './rdo/full-offer.rdo.js';
export { OfferEntity, OfferModel } from './offer.entity.js';
export { DefaultOfferService } from './default-offer.service.js';
export { createOfferContainer } from './offer.container.js';
export {
Expand All @@ -10,4 +12,3 @@ export {
selectOfferFields,
} from './offer.aggregation.js';
export { OfferController } from './offer.controller.js';
export { OfferRDO } from './rdo/offer.rdo.js';
10 changes: 5 additions & 5 deletions src/shared/modules/offer/offer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EHttpMethod } from '../../../rest/types/index.js';
import { COMPONENT } from '../../constants/index.js';
import { ILogger } from '../../libs/logger/types/index.js';
import { IOfferService } from './types/index.js';
import { CreateOfferDTO, OfferRDO, UpdateOfferDTO } from './index.js';
import { CreateOfferDTO, ShortOfferRDO, UpdateOfferDTO, FullOfferRDO } from './index.js';
import { fillDTO } from '../../helpers/index.js';

@injectable()
Expand All @@ -29,7 +29,7 @@ export class OfferController extends BaseController {

public async index(_req: Request, res: Response): Promise<void> {
const offers = await this.offerService.find();
const responseData = fillDTO(OfferRDO, offers);
const responseData = fillDTO(ShortOfferRDO, offers);
this.ok(res, responseData);
}

Expand All @@ -38,7 +38,7 @@ export class OfferController extends BaseController {
res: Response,
): Promise<void> {
const result = await this.offerService.create(body);
this.created(res, fillDTO(OfferRDO, result));
this.created(res, fillDTO(FullOfferRDO, result));
}

public async findOne(req: Request, res: Response): Promise<void> {
Expand All @@ -52,7 +52,7 @@ export class OfferController extends BaseController {
);
}

const responseData = fillDTO(OfferRDO, existsOffer);
const responseData = fillDTO(FullOfferRDO, existsOffer);
this.ok(res, responseData);
}

Expand All @@ -76,6 +76,6 @@ export class OfferController extends BaseController {
);
}

this.ok(res, result);
this.ok(res, fillDTO(FullOfferRDO, result));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Expose } from 'class-transformer';
import { Expose, Type } from 'class-transformer';
import { EFacilities, EHousing, TCoords } from '../../../types/index.js';
import { UserRDO } from '../../user/index.js';

export class FullOfferRDO {
@Expose({ name: '_id' })
public id!: string;

export class OfferRDO {
@Expose()
public _id!: string;
public createdAt!: string;

@Expose()
public title!: string;
Expand All @@ -23,6 +27,10 @@ export class OfferRDO {
@Expose()
public isPremium!: boolean;

// TODO in next module
// @Expose()
// public isFavorite!: boolean;

@Expose()
public housingType!: EHousing;

Expand All @@ -42,5 +50,6 @@ export class OfferRDO {
public coords!: TCoords;

@Expose()
public author!: unknown;
@Type(() => UserRDO)
public author!: UserRDO;
}
55 changes: 55 additions & 0 deletions src/shared/modules/offer/rdo/short-offer.rdo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Expose, Type } from 'class-transformer';
import { EFacilities, EHousing, TCoords } from '../../../types/index.js';
import { UserRDO } from '../../user/index.js';

export class ShortOfferRDO {
@Expose({ name: '_id' })
public id!: string;

@Expose()
public createdAt!: string;

@Expose()
public title!: string;

@Expose()
public description!: string;

@Expose()
public city!: string;

@Expose()
public previewImagePath!: string;

@Expose()
public photos!: string[];

@Expose()
public isPremium!: boolean;

@Expose()
public housingType!: EHousing;

@Expose()
public roomsNumber!: number;

@Expose()
public visitorsNumber!: number;

@Expose()
public price!: number;

@Expose()
public facilities!: EFacilities;

@Expose()
public coords!: TCoords;

// TODO in next module
// @Expose()
// public commentsCount!: number;

@Expose()
@Type(() => UserRDO)
public author!: UserRDO;
}
4 changes: 2 additions & 2 deletions src/shared/modules/user/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { UserEntity, UserModel } from './user.entity.js';
export { CreateUserDTO } from './dto/create-user.dto.js';
export { UpdateUserDTO } from './dto/update-user.dto.js';
export { LoginUserDTO } from './dto/login-user.dto.js';
export { UserRDO } from './rdo/user.rdo.js';
export { UserEntity, UserModel } from './user.entity.js';
export { DefaultUserService } from './default-user.service.js';
export { createUserContainer } from './user.container.js';
export { populateFavorites } from './user.aggregation.js';
export { UserController } from './user.controller.js';
export { UserRDO } from './rdo/user.rdo.js';
4 changes: 4 additions & 0 deletions src/shared/modules/user/rdo/user.rdo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Expose } from 'class-transformer';
import { EUserType } from '../../../types/index.js';

export class UserRDO {
@Expose()
Expand All @@ -9,4 +10,7 @@ export class UserRDO {

@Expose()
public name!: string;

@Expose()
public type!: EUserType;
}

0 comments on commit ac225c5

Please sign in to comment.