Skip to content

Commit

Permalink
Merge pull request #9 from al-bertes/module7-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Oct 28, 2024
2 parents 35e1b6b + 801bb71 commit ad09350
Show file tree
Hide file tree
Showing 41 changed files with 750 additions and 70 deletions.
82 changes: 73 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"main": "main.js",
"scripts": {
"start": "npm run build && node ./dist/main.rest.js",
"start:dev": "nodemon -L",
"start:dev": "nodemon",
"build": "npm run clean && npm run compile",
"lint": "eslint src/ --ext .ts",
"compile": "tsc -p tsconfig.json",
Expand All @@ -21,7 +21,7 @@
"devDependencies": {
"@types/convict": "^6.1.6",
"@types/convict-format-with-validator": "^6.0.5",
"@types/express": "^5.0.0",
"@types/express": "^4.17.21",
"@types/node": "^20.16.5",
"@typescript-eslint/eslint-plugin": "6.7.0",
"@typescript-eslint/parser": "6.7.0",
Expand All @@ -46,13 +46,15 @@
"@typegoose/typegoose": "^12.8.0",
"chalk": "^5.3.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"dayjs": "^1.11.13",
"dotenv": "^16.4.5",
"express": "^4.21.1",
"express-async-handler": "^1.2.0",
"got": "^14.4.2",
"http-status-codes": "^2.3.0",
"inversify": "^6.0.2",
"joi": "^17.13.3",
"mongodb": "^6.9.0",
"mongoose": "^8.7.0",
"pino": "^9.4.0",
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/import.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class ImportCommand implements Command {
userId: user.id,
name: offer.name,
description: offer.description,
offerType: offer.offerType,
previewImage: offer.previewImage,
images: offer.images,
createdData: offer.createdData,
Expand Down
43 changes: 22 additions & 21 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ export class RestApplication {
@inject(Component.DatabaseClient) private readonly databaseClient: DatabaseClient,
@inject(Component.ExceptionFilter) private readonly appExceptionFilter: ExceptionFilter,
@inject(Component.UserController) private readonly userController: Controller,
@inject(Component.OfferController) private readonly offerController: Controller,
@inject(Component.CommentController) private readonly commentController: Controller
) {
this.server = express();
}

private async initDb() {
this.logger.info('Application initialization');
this.logger.info('Init database…');

const mongoUri = getMongoURI(
this.config.get('DB_USER'),
this.config.get('DB_PASSWORD'),
Expand All @@ -30,48 +35,44 @@ export class RestApplication {
this.config.get('DB_NAME')
);

this.logger.info('Init database completed');
return this.databaseClient.connect(mongoUri);
}

private async _initServer() {
private _initServer() {
const port = this.config.get('PORT');
this.logger.info('Try to init server…');
this.server.listen(port);
this.logger.info(
`🚀 Server started on http://localhost:${this.config.get('PORT')}`
);
}

private async _initControllers() {
private _initControllers() {
this.logger.info('Init controllers');
this.server.use('/users', this.userController.router);
this.server.use('/offers', this.offerController.router);
this.server.use('/comments', this.commentController.router);
this.logger.info('Controller initialization completed');
}

private async _initMiddleware() {
private _initMiddleware() {
this.logger.info('Init app-level middleware');
this.server.use(express.json());
this.logger.info('App-level middleware initialization completed');
}

private async _initExceptionFilters() {
private _initExceptionFilters() {
this.logger.info('Init exception filters');
this.server.use(this.appExceptionFilter.catch.bind(this.appExceptionFilter));
this.logger.info('Exception filters initialization compleated');
}

public async init() {
this.logger.info('Application initialization');
this.logger.info('Init database…');
await this.initDb();
this.logger.info('Init database completed');
this.logger.info('Init controllers');

this.logger.info('Init app-level middleware');
await this._initMiddleware();
this.logger.info('App-level middleware initialization completed');

await this._initControllers();
this.logger.info('Controller initialization completed');

this.logger.info('Init exception filters');
await this._initExceptionFilters();
this.logger.info('Exception filters initialization compleated');

this.logger.info('Try to init server…');
await this._initServer();
this.logger.info(
`🚀 Server started on http://localhost:${this.config.get('PORT')}`
);
}
}
6 changes: 2 additions & 4 deletions src/shared/helpers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ export function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : '';
}

export function createErrorObject(message: string) {
return {
error: message,
};
export function createErrorObject(error: string) {
return { error };
}

export function fillDTO<T, V>(someDto: ClassConstructor<T>, plainObject: V) {
Expand Down
21 changes: 11 additions & 10 deletions src/shared/libs/file-reader/tsv-file-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import EventEmitter from 'node:events';
import { createReadStream } from 'node:fs';

import { FileReader } from './file-reader.interface.js';
import { Offer} from '../../types/index.js';
import { Cities, Offer} from '../../types/index.js';
import { TSV_SEPARATOR } from '../../constants.js';

export class TSVFileReader extends EventEmitter implements FileReader {
Expand All @@ -18,6 +18,7 @@ export class TSVFileReader extends EventEmitter implements FileReader {
const [
name,
description,
offerType,
createdDate,
city,
previewImage,
Expand All @@ -32,15 +33,15 @@ export class TSVFileReader extends EventEmitter implements FileReader {
coordinates
] = line.split('\t');

const parseAuthor = author.split(TSV_SEPARATOR);
const lat = Number(coordinates.split(TSV_SEPARATOR)[0]);
const lon = Number(coordinates.split(TSV_SEPARATOR)[1]);
const [userName, avatarPath, email, typeUser] = author.split(TSV_SEPARATOR);
const [lat, lon] = coordinates.split(TSV_SEPARATOR);

return {
name,
description,
offerType,
createdData: new Date(createdDate),
city,
city: city as Cities,
previewImage,
images: images.split(TSV_SEPARATOR),
premium: premium.toLocaleLowerCase() === 'true',
Expand All @@ -49,13 +50,13 @@ export class TSVFileReader extends EventEmitter implements FileReader {
guests: Number(guests),
amenities: amenities.split(TSV_SEPARATOR),
author: {
name: parseAuthor[0],
avatarPath: parseAuthor[1],
email: parseAuthor[2],
typeUser: parseAuthor[3],
name: userName,
avatarPath,
email,
typeUser,
},
price: Number(price),
coordinates: {latitude: lat, longitude: lon}
coordinates: {latitude: Number(lat), longitude: Number(lon)}
};
}

Expand Down
12 changes: 7 additions & 5 deletions src/shared/libs/offer-generator/tsv-offer-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ const LAST_WEEK_DAY = 7;
export class TSVOfferGenerator implements OfferGenerator {
constructor(private readonly mockData: MockServerData) {}
public generate(): string {
const name = getRandomItem<string>(this.mockData.name);
const describe = getRandomItem<string>(this.mockData.describe);
const city = getRandomItem<string>(this.mockData.city);
const name = getRandomItem(this.mockData.name);
const describe = getRandomItem(this.mockData.describe);
const offerType = getRandomItem(this.mockData.offerType);
const city = getRandomItem(this.mockData.city);
const previewImage = getRandomItem(this.mockData.previewImage);
const images = getRandomItems<string>(this.mockData.images).join(TSV_SEPARATOR);
const images = getRandomItems(this.mockData.images).join(TSV_SEPARATOR);
const premium = !!generateRandomValue(0, 1);
const price = generateRandomValue(50, 200);
const rating = generateRandomValue(0, 5);
const bedrooms = generateRandomValue(0, 5);
const guests = generateRandomValue(0, 5);
const amenities = getRandomItems<string>(this.mockData.amenities).join(TSV_SEPARATOR);
const amenities = getRandomItems(this.mockData.amenities).join(TSV_SEPARATOR);
const authorRandom = getRandomItem(this.mockData.author);
const author = [authorRandom.name, authorRandom.avatarPath, authorRandom.email, authorRandom.typeUser].join(TSV_SEPARATOR);
const coordinates = [generateRandomValue(1000, 3000), generateRandomValue(1000, 3000)].join(TSV_SEPARATOR);
Expand All @@ -37,6 +38,7 @@ export class TSVOfferGenerator implements OfferGenerator {
return [
name,
describe,
offerType,
createData,
city,
previewImage,
Expand Down
Loading

0 comments on commit ad09350

Please sign in to comment.