-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<void> { | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { OfferGenerator } from './offer-generator.interface.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface OfferGenerator { | ||
generate(): string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>(this.mockData.title); | ||
const description = getRandomElement<string>(this.mockData.description); | ||
const postDate = dayjs().subtract(getRandomNumber(FIRST_WEEK_DAY, LAST_WEEK_DAY), 'day').toISOString(); | ||
const city = getRandomElement<string>(this.mockData.city); | ||
const previewImage = getRandomElement<string>(this.mockData.previewImage); | ||
const images = getRandomItems<string>(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<string>(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<string>(this.mockData.goods).join(';'); | ||
const name = getRandomElement<string>(this.mockData.name); | ||
const email = getRandomElement<string>(this.mockData.email); | ||
const avatarUser = getRandomElement<string>(this.mockData.avatarUser); | ||
const password = getRandomElement<string>(this.mockData.password); | ||
const typeUser = getRandomElement<string>(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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters