Skip to content

Commit

Permalink
Merge pull request htmlacademy-nodejs-api#11 from alatielle/module8-t…
Browse files Browse the repository at this point in the history
…ask1
  • Loading branch information
keksobot authored Oct 29, 2023
2 parents c9d2c72 + 9dbc0d8 commit 8cfd72c
Show file tree
Hide file tree
Showing 118 changed files with 1,642 additions and 517 deletions.
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

16 changes: 16 additions & 0 deletions mocks/mock-server-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@
"https://s3.amazonaws.com/uifaces/faces/twitter/adhiardana/128.jpg",
"https://s3.amazonaws.com/uifaces/faces/twitter/ponchomendivil/128.jpg",
"https://s3.amazonaws.com/uifaces/faces/twitter/hugocornejo/128.jpg"
],
"cities": [
{ "name": "Amsterdam", "latitude": 52.370216, "longitude": 4.895168 },
{ "name": "Brussels", "latitude": 50.846557, "longitude": 4.351697 },
{
"name": "Cologne",
"latitude": 50.938361,
"longitude": 6.959974
},
{ "name": "Dusseldorf", "latitude": 51.225402, "longitude": 6.776314 },
{ "name": "Hamburg", "latitude": 53.550341, "longitude": 10.000654 },
{
"name": "Paris",
"latitude": 48.85661,
"longitude": 2.351499
}
]
}
}
49 changes: 27 additions & 22 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"json-server": "0.17.3",
"nodemon": "3.0.1",
"pino-pretty": "10.2.0",
"prettier": "3.0.3",
"random-location": "1.1.3",
"rimraf": "5.0.1",
"ts-node": "10.9.1",
"tslib": "2.6.2",
Expand All @@ -57,6 +57,7 @@
"express-async-handler": "1.2.0",
"got": "13.0.0",
"inversify": "6.0.1",
"jose": "5.0.0",
"mime-types": "2.1.35",
"mongoose": "7.5.3",
"multer": "1.4.5-lts.1",
Expand Down
1 change: 1 addition & 0 deletions public/default-avatar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions specification/project.spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ paths:
'401':
description: User session token is invalid.

/users/logout:
post:
tags:
- users
summary: User session finalization
description: Ends authorized user's session.
responses:
'204':
description: Session is successfully ended.
'401':
description: User session token is invalid.

/users/{usedId}/avatar:
post:
tags:
Expand Down
4 changes: 3 additions & 1 deletion src/cli/cli-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class CLIApplication {

public getDefaultCommand(): Command | never {
if (!this.commands[this.defaultCommand]) {
throw new Error(`The default command (${this.defaultCommand}) is not registered.`);
throw new Error(
`The default command (${this.defaultCommand}) is not registered.`,
);
}
return this.commands[this.defaultCommand];
}
Expand Down
8 changes: 6 additions & 2 deletions src/cli/commands/help.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class HelpCommand implements Command {
console.info(`Программа для подготовки данных для REST API сервера.

${chalk.bold('ПРИМЕР')}
${chalk.bold('cli.js')} --${chalk.underline('command')} [...${chalk.underline('arguments')}]
${chalk.bold('cli.js')} --${chalk.underline(
'command',
)} [...${chalk.underline('arguments')}]

${chalk.bold('КОМАНДЫ')}
${chalk.bold('--version')}
Expand All @@ -19,7 +21,9 @@ ${chalk.bold('КОМАНДЫ')}
Печатает этот текст
${chalk.bold('--import')} ${chalk.underline('path')}
Импортирует данные из TSV
${chalk.bold('--generate')} ${chalk.underline('n')} ${chalk.underline('path')} ${chalk.underline('url')}
${chalk.bold('--generate')} ${chalk.underline('n')} ${chalk.underline(
'path',
)} ${chalk.underline('url')}
Генерирует произвольное количество тестовых данных
`);
}
Expand Down
44 changes: 37 additions & 7 deletions src/cli/commands/import.command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { Command } from './command.interface.js';
import { TSVFileReader } from '../../shared/libs/file-reader/index.js';
import { createOffer, getMongoURI } from '../../shared/helpers/index.js';
import { UserService } from '../../shared/modules/user/user-service.interface.js';
import { DefaultOfferService, OfferModel, OfferService } from '../../shared/modules/offer/index.js';
import { DatabaseClient, MongoDatabaseClient } from '../../shared/libs/database-client/index.js';
import { UserService } from '../../shared/modules/user/index.js';
import {
DefaultOfferService,
OfferModel,
OfferService,
} from '../../shared/modules/offer/index.js';
import {
CityService,
CityModel,
DefaultCityService,
} from '../../shared/modules/city/index.js';
import {
DatabaseClient,
MongoDatabaseClient,
} from '../../shared/libs/database-client/index.js';
import { ConsoleLogger, Logger } from '../../shared/libs/logger/index.js';
import { DefaultUserService, UserModel } from '../../shared/modules/user/index.js';
import {
DefaultUserService,
UserModel,
} from '../../shared/modules/user/index.js';
import { Offer } from '../../shared/types/index.js';
import { DEFAULT_DB_PORT, DEFAULT_USER_PASSWORD } from './command.constant.js';

export class ImportCommand implements Command {
private userService: UserService;
private offerService: OfferService;
private cityService: CityService;
private databaseClient: DatabaseClient;
private readonly logger: Logger;
private salt: string;
Expand All @@ -23,6 +39,7 @@ export class ImportCommand implements Command {
this.logger = new ConsoleLogger();
this.offerService = new DefaultOfferService(this.logger, OfferModel);
this.userService = new DefaultUserService(this.logger, UserModel);
this.cityService = new DefaultCityService(this.logger, CityModel);
this.databaseClient = new MongoDatabaseClient(this.logger);
}

Expand Down Expand Up @@ -50,12 +67,23 @@ export class ImportCommand implements Command {
this.salt,
);

const city = await this.cityService.findByCityNameOrCreate(
offer.city.name,
{
name: offer.city.name,
location: {
latitude: offer.city.location.latitude,
longitude: offer.city.location.longitude,
},
},
);

await this.offerService.create({
authorId: user.id,
title: offer.title,
description: offer.description,
postDate: offer.postDate,
city: offer.city,
cityId: city.id,
imagePreview: offer.imagePreview,
images: offer.images,
isPremium: offer.isPremium,
Expand Down Expand Up @@ -101,7 +129,6 @@ export class ImportCommand implements Command {
return;
}


const uri = getMongoURI(login, password, host, DEFAULT_DB_PORT, dbname);
this.salt = salt;

Expand All @@ -115,7 +142,10 @@ export class ImportCommand implements Command {
try {
await fileReader.read();
} catch (error) {
this.logger.error(`Can't import data from file: ${filename}`, error as Error);
this.logger.error(
`Can't import data from file: ${filename}`,
error as Error,
);
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/cli/commands/version.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ type PackageJSONConfig = {
};

function isPackageJSONConfig(value: unknown): value is PackageJSONConfig {
return typeof value === 'object' && value !== null && !Array.isArray(value) && Object.hasOwn(value, 'version');
return (
typeof value === 'object' &&
value !== null &&
!Array.isArray(value) &&
Object.hasOwn(value, 'version')
);
}

export class VersionCommand implements Command {
Expand Down Expand Up @@ -39,7 +44,10 @@ export class VersionCommand implements Command {
const version = this.readVersion();
this.logger.info(chalk.green(version));
} catch (error) {
this.logger.error(`Failed to read version from ${chalk.bold(this.filePath)}`, error as Error);
this.logger.error(
`Failed to read version from ${chalk.bold(this.filePath)}`,
error as Error,
);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main.cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ function getFilesByPattern(dirPath: string, pattern: string) {
const dirContent = readdirSync(resolve(dirPath));
const regExp = new RegExp(pattern, 'i');

return dirContent.filter((fileName) => regExp.test(fileName)).map((fileName) => resolve(dirPath, fileName));
return dirContent
.filter((fileName) => regExp.test(fileName))
.map((fileName) => resolve(dirPath, fileName));
}

async function bootstrap() {
Expand Down
13 changes: 11 additions & 2 deletions src/main.rest.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import 'reflect-metadata';
import { Container } from 'inversify';
import { Component } from './shared/types/index.js';
import { RestApplication, createRestApplicationContainer } from './rest/index.js';
import {
RestApplication,
createRestApplicationContainer,
} from './rest/index.js';
import { createUserContainer } from './shared/modules/user/index.js';
import { createOfferContainer } from './shared/modules/offer/index.js';
import { createCommentContainer } from './shared/modules/comment/index.js';
import { createAuthContainer } from './shared/modules/auth/index.js';
import { createCityContainer } from './shared/modules/city/index.js';

async function bootstrap() {
const appContainer = Container.merge(
createRestApplicationContainer(),
createUserContainer(),
createOfferContainer(),
createCommentContainer(),
createCityContainer(),
createAuthContainer(),
);

const application = appContainer.get<RestApplication>(Component.RestApplication);
const application = appContainer.get<RestApplication>(
Component.RestApplication,
);
await application.init();
}

Expand Down
Loading

0 comments on commit 8cfd72c

Please sign in to comment.