From 31731111f97c12eee4aa06f53dc1eb0703ebd8e2 Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 13 Oct 2024 21:44:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D1=83=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BC=D0=BE=D0=BA=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/commands/generate.command.ts | 49 +++++++++++++++++ src/cli/index.ts | 1 + src/main.cli.ts | 4 +- src/shared/libs/offer-generator/index.ts | 1 + .../offer-generator.interface.ts | 3 + .../offer-generator/tsv-offer-generate.ts | 55 +++++++++++++++++++ src/shared/types/index.ts | 3 +- src/shared/types/mock-server-data.ts | 15 +++++ tsconfig.json | 1 + 9 files changed, 129 insertions(+), 3 deletions(-) create mode 100644 src/cli/commands/generate.command.ts create mode 100644 src/shared/libs/offer-generator/index.ts create mode 100644 src/shared/libs/offer-generator/offer-generator.interface.ts create mode 100644 src/shared/libs/offer-generator/tsv-offer-generate.ts create mode 100644 src/shared/types/mock-server-data.ts diff --git a/src/cli/commands/generate.command.ts b/src/cli/commands/generate.command.ts new file mode 100644 index 0000000..0751480 --- /dev/null +++ b/src/cli/commands/generate.command.ts @@ -0,0 +1,49 @@ +import got from 'got'; +import { Command } from './command.interface.js'; +import { MockServerData } from '../../shared/types/index.js'; +import { TSVOfferGenerator } from '../../shared/libs/offer-generator/tsv-offer-generate.js'; +import { TSVFileWriter } from '../../shared/libs/file-writer/tsv-file-wtiter.js'; + +export class GenerateCommand implements Command { + private initialData: MockServerData; + + private async load(url: string) { + try { + this.initialData = await got.get(url).json(); + } catch { + throw new Error (`Can't load data from ${url}`); + } + } + + private async write(filepath: string, offerCount: number) { + const tsvOfferGenerator = new TSVOfferGenerator(this.initialData); + const tsvFileWriter = new TSVFileWriter(filepath); + for (let i = 0; i < offerCount; i++) { + await tsvFileWriter.write(tsvOfferGenerator.generate()); + } + } + + public getName(): string { + return '--generate'; + } + + public async execute(...parameters: string[]): Promise { + const [count, filepath, url] = parameters; + const offerCount = Number.parseInt(count, 10); + + try { + await this.load(url); + await this.write(filepath, offerCount); + console.info(`File ${filepath} was created!`); + + } catch (error: unknown) { + console.error('Can\'t generate data'); + + if (error instanceof Error) { + console.error('error'); + } + } finally { + console.log(count, filepath, url); + } + } +} diff --git a/src/cli/index.ts b/src/cli/index.ts index 67ca9c5..fddeb4e 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -3,3 +3,4 @@ export { CommandParser } from './command-parser.js'; export { HelpCommand } from './commands/help.command.js'; export { VersionCommand } from './commands/version.command.js'; export { ImportCommand } from './commands/import.command.js'; +export { GenerateCommand } from './commands/generate.command.js'; diff --git a/src/main.cli.ts b/src/main.cli.ts index 3498237..62f7e88 100644 --- a/src/main.cli.ts +++ b/src/main.cli.ts @@ -1,8 +1,8 @@ -import { CLIApplication, HelpCommand, VersionCommand, ImportCommand } from './cli/index.js'; +import { CLIApplication, HelpCommand, VersionCommand, ImportCommand, GenerateCommand} from './cli/index.js'; const bootstrap = () => { const cliApplication = new CLIApplication(); - cliApplication.registerCommands([new HelpCommand(), new VersionCommand(), new ImportCommand()]); + cliApplication.registerCommands([new HelpCommand(), new VersionCommand(), new ImportCommand(), new GenerateCommand()]); cliApplication.processCommand(process.argv); }; diff --git a/src/shared/libs/offer-generator/index.ts b/src/shared/libs/offer-generator/index.ts new file mode 100644 index 0000000..dc94ca3 --- /dev/null +++ b/src/shared/libs/offer-generator/index.ts @@ -0,0 +1 @@ +export { OfferGenerator } from './offer-generator.interface.js'; diff --git a/src/shared/libs/offer-generator/offer-generator.interface.ts b/src/shared/libs/offer-generator/offer-generator.interface.ts new file mode 100644 index 0000000..eca13ba --- /dev/null +++ b/src/shared/libs/offer-generator/offer-generator.interface.ts @@ -0,0 +1,3 @@ +export interface OfferGenerator { + generate(): string +} diff --git a/src/shared/libs/offer-generator/tsv-offer-generate.ts b/src/shared/libs/offer-generator/tsv-offer-generate.ts new file mode 100644 index 0000000..c2cde2e --- /dev/null +++ b/src/shared/libs/offer-generator/tsv-offer-generate.ts @@ -0,0 +1,55 @@ +import dayjs from 'dayjs'; +import { getRandomElement, getRandomItems, getRandomNumber } from '../../helpers/index.js'; +import { MockServerData } from '../../types/index.js'; +import { OfferGenerator } from './index.js'; + +const FIRST_WEEK_DAY = 1; +const LAST_WEEK_DAY = 7; + +const MINIMUM_RATING = 1; +const MAXIMUM_RATING = 5; + +const MAX_BEDROOMS = 5; +const MIN_BEDROOM = 1; + +const MIN_ADULT = 1; +const MAX_ADULTS = 5; + +const MIN_PRICE = 560; +const MAX_PRICE = 3500; + +export class TSVOfferGenerator implements OfferGenerator { + constructor(private readonly mockData: MockServerData) {} + + public generate(): string { + const title = getRandomElement(this.mockData.title); + const description = getRandomElement(this.mockData.description); + const postDate = dayjs().subtract(getRandomNumber(FIRST_WEEK_DAY, LAST_WEEK_DAY), 'day').toISOString(); + const city = getRandomElement(this.mockData.city); + const previewImage = getRandomElement(this.mockData.previewImage); + const images = getRandomItems(this.mockData.images).join(';'); + const isFavorite = Math.random() < 0.5; + const isPremium = Math.random() < 0.5; + const rating = getRandomNumber(MINIMUM_RATING, MAXIMUM_RATING); + const type = getRandomElement(this.mockData.type); + const bedrooms = getRandomNumber(MIN_BEDROOM, MAX_BEDROOMS); + const maxAdults = getRandomNumber(MIN_ADULT, MAX_ADULTS); + const price = getRandomNumber(MIN_PRICE, MAX_PRICE); + const goods = getRandomItems(this.mockData.goods).join(';'); + const name = getRandomElement(this.mockData.name); + const email = getRandomElement(this.mockData.email); + const avatarUser = getRandomElement(this.mockData.avatarUser); + const password = getRandomElement(this.mockData.password); + const typeUser = getRandomElement(this.mockData.typeUser); + + return [ + title, description, postDate, + city, previewImage, images, + isFavorite, isPremium, rating, + type, bedrooms, maxAdults, + price, goods, name, + email, avatarUser, password, + typeUser + ].join('\t'); + } +} diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index 04cde9f..ead490f 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -1,2 +1,3 @@ export { Offer } from './offer-type.js'; -export { TypeUser, User} from './user-type.js'; +export { TypeUser, User } from './user-type.js'; +export { MockServerData } from './mock-server-data.js'; diff --git a/src/shared/types/mock-server-data.ts b/src/shared/types/mock-server-data.ts new file mode 100644 index 0000000..36fb3f5 --- /dev/null +++ b/src/shared/types/mock-server-data.ts @@ -0,0 +1,15 @@ + +export type MockServerData = { + title: string[], + description: string[], + city: string[], + previewImage: string[], + images: string[], + type: string[], + goods: string[], + name: string[]; + email: string[]; + avatarUser: string[]; + password: string[]; + typeUser: string[]; +} diff --git a/tsconfig.json b/tsconfig.json index a24611b..e061253 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,6 +8,7 @@ "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, + "strictPropertyInitialization": false, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true,