Skip to content

Commit

Permalink
Merge pull request #21 from maick05/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
maick05 authored Jun 26, 2022
2 parents b1d1b9f + b348170 commit df4cc5e
Show file tree
Hide file tree
Showing 62 changed files with 2,114 additions and 523 deletions.
34 changes: 34 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Changelog

All notable changes to this project will be documented in this file.

## [0.0.1] - 2022-05-30

### Added

- First Version

## [0.0.2] - 2022-06-20

### Added

- MongoDB
- Neighborhoods Collection

## [0.0.3] - 2022-06-23

### Added

- Validate data input by collections
- Country Schema
- State Schema
- City Schema
- Mongoose Migrations

## [0.0.4] - 2022-06-24

### Added

- Route Neighborhoods By State
- Route Seed Neighborhoods By State
- logSeed Collection
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ maicksantos05@hotmail.com
After run the server, make a get in browser, postman or similar

```bash
## Url example
## Neighborhoods By City example
http://localhost:3000/neighborhoods/city/brazil/sc/orleans
## Neighborhoods By State example
http://localhost:3000/neighborhoods/city/brazil/sc
```

[Swagger](https://app.swaggerhub.com/apis/dev-seeder/Places/1.0.0)

At the moment, it's working only for Brazil cities.
At the moment, it's working only for Brazilians cities and states.
27 changes: 27 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"nest-puppeteer": "^1.1.1",
"puppeteer": "^14.1.0",
"reflect-metadata": "^0.1.13",
"request-ip": "^2.2.0",
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"sinon": "^14.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
db.cities.deleteMany({
id: {
$in: [
10400, 10969, 11128, 11170, 11305, 11432, 11450, 11890, 12343, 12732,
13245, 13617, 13619, 14202, 14246, 14298, 14371, 14520, 15228, 15359
]
}
});
39 changes: 30 additions & 9 deletions src/microservice/adapter/controller/neighborhoods.controller.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import { Controller, Get, HttpStatus, Param } from '@nestjs/common';
import { GetNeighborhoodsByStateService } from '../../domain/service/neighborhoods/get/get-neighborhoods-by-state.service';
import { NestResponse } from '../../../core/http/nest-response';
import { AbstractController } from '../../domain/controller/abstract-controller';
import { GetNeighborhoodsByCityService } from '../../domain/service/neighborhoods/get-neighborhoods-by-city.service';
import { GetNeighborhoodsByCityService } from '../../domain/service/neighborhoods/get/get-neighborhoods-by-city.service';
import { SearchNeighborhoodsInput } from '../../domain/model/search/search-neighborhoods-input.model';
import { SeedNeighborhoodsByStateService } from '../../domain/service/neighborhoods/seed/seed-neighborhoods-by-state.service';

@Controller('neighborhoods')
export class NeighborhoodsController extends AbstractController {
constructor(
private readonly getNeighborhoodsByCityService: GetNeighborhoodsByCityService
private readonly getNeighborhoodsByCityService: GetNeighborhoodsByCityService,
private readonly getNeighborhoodsByStateService: GetNeighborhoodsByStateService,
private readonly seedNeighborhoodsByStateService: SeedNeighborhoodsByStateService
) {
super();
}

@Get('/city/:country/:state/:city')
async getNeighborhoodsByCity(
@Param('country') country,
@Param('state') state,
@Param('city') city
@Param() params: SearchNeighborhoodsInput
): Promise<NestResponse> {
return this.buildResponse(
HttpStatus.OK,
await this.getNeighborhoodsByCityService.getNeighborhoodsByCity(
country,
state,
city
await this.getNeighborhoodsByCityService.getNeighborhoodsByCity(params)
);
}

@Get('/state/:country/:state')
async getNeighborhoodsByState(
@Param() params: SearchNeighborhoodsInput
): Promise<NestResponse> {
return this.buildResponse(
HttpStatus.OK,
await this.getNeighborhoodsByStateService.getNeighborhoodsByState(params)
);
}

@Get('/seed/state/:country/:state')
async seedNeighborhoodsByState(
@Param() params: SearchNeighborhoodsInput
): Promise<NestResponse> {
return this.buildResponse(
HttpStatus.OK,
await this.seedNeighborhoodsByStateService.seedNeighborhoodsByState(
params
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ValidOutputSearchNeighborhood } from 'src/microservice/domain/interface/valid-output-search/valid-outpu-search-neighborhood.interface';
import { NeighborhoodsByCity } from '../../../../domain/model/neighborhoods-by-city.model';
import { NeighborhoodByCity } from '../../../../domain/model/neighborhoods/neighborhood-by-city.model';
import { Neighborhood } from '../../../../domain/schemas/neighborhood.schema';

export class NeighborhoodsMongoBuilder {
constructor(private readonly puppeteerResponse: NeighborhoodsByCity[]) {}
constructor(private readonly puppeteerResponse: NeighborhoodByCity[]) {}

build(searchParams: ValidOutputSearchNeighborhood): Neighborhood[] {
const arr = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { NeighborhoodsByCity } from '../../../../domain/model/neighborhoods-by-city.model';
import { NeighborhoodByCity } from '../../../../domain/model/neighborhoods/neighborhood-by-city.model';
import { Neighborhood } from '../../../../domain/schemas/neighborhood.schema';

export class NeighborhoodsPuppeteerBuilder {
constructor(private readonly mongoResponse: Neighborhood[]) {}

build(): NeighborhoodsByCity[] {
build(): NeighborhoodByCity[] {
const arr = [];
this.mongoResponse.forEach((document) => {
const obj = new NeighborhoodsByCity();
const obj = new NeighborhoodByCity();
obj.city = `${document.city.capitalize()} - ${document.state.toUpperCase()}`;
obj.name = document.name;
arr.push(obj);
Expand Down
30 changes: 22 additions & 8 deletions src/microservice/adapter/neighborhoods.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigModule } from '@nestjs/config';
import { PuppeteerModule } from 'nest-puppeteer';
import { NeighborhoodsController } from './controller/neighborhoods.controller';
import { GuiaMaisRepository } from './repository/neighborhoods/puppeteer/guia-mais.repository';
import { GetNeighborhoodsByCityService } from '../domain/service/neighborhoods/get-neighborhoods-by-city.service';
import { GetNeighborhoodsByCityService } from '../domain/service/neighborhoods/get/get-neighborhoods-by-city.service';
import configuration from '../../config/configuration';
import { NeighborhoodsMongoose } from './repository/neighborhoods/neighborhoods-mongoose.repository';
import {
Expand All @@ -13,14 +13,21 @@ import {
import { MongooseModule } from '@nestjs/mongoose';
import { SaveNeighborhoodsByCityService } from '../domain/service/neighborhoods/save-neighborhoods-by-city.service';
import { Country, CountrySchema } from '../domain/schemas/country.schema';
import { GetCountryByNameOrAliasService } from '../domain/service/countries/get-country-by-name-or-alias.service';
import { ValidateCountryByNameOrAliasService } from '../domain/service/countries/validate-country-by-name-or-alias.service';
import { CountriesMongoose } from './repository/countries/countries-mongoose.repository';
import { State, StateSchema } from '../domain/schemas/state.schema';
import { GetStateByNameOrAliasService } from '../domain/service/states/get-state-by-name-or-alias.service';
import { ValidateStateByNameOrAliasService } from '../domain/service/states/validate-state-by-name-or-alias.service';
import { StatesMongoose } from './repository/states/states-mongoose.repository';
import { City, CitySchema } from '../domain/schemas/city.schema';
import { GetCityByNameOrAliasService } from '../domain/service/cities/get-city-by-name-or-alias.service';
import { ValidateCityByNameOrAliasService } from '../domain/service/cities/validate-city-by-name-or-alias.service';
import { CitiesMongoose } from './repository/cities/cities-mongoose.repository';
import { GetNeighborhoodsByStateService } from '../domain/service/neighborhoods/get/get-neighborhoods-by-state.service';
import { ValidateInputParamsService } from '../domain/service/validate-input-params.service';
import { SeedNeighborhoodsByStateService } from '../domain/service/neighborhoods/seed/seed-neighborhoods-by-state.service';
import { GetCitiesByStateService } from '../domain/service/cities/get-cities-by-state.service';
import { LogSeed, LogSeedSchema } from '../domain/schemas/logseed.schema';
import { LogSeedMongoose } from './repository/logseed/logseed-mongoose.repository';
import { LogSeedJobService } from '../domain/service/logseed/log-seed-job.service';

@Module({
imports: [
Expand All @@ -33,7 +40,8 @@ import { CitiesMongoose } from './repository/cities/cities-mongoose.repository';
{ name: Neighborhood.name, schema: NeighborhoodSchema },
{ name: Country.name, schema: CountrySchema },
{ name: State.name, schema: StateSchema },
{ name: City.name, schema: CitySchema }
{ name: City.name, schema: CitySchema },
{ name: LogSeed.name, schema: LogSeedSchema }
])
],
controllers: [NeighborhoodsController],
Expand All @@ -46,11 +54,17 @@ import { CitiesMongoose } from './repository/cities/cities-mongoose.repository';
CountriesMongoose,
StatesMongoose,
CitiesMongoose,
LogSeedMongoose,
GetNeighborhoodsByCityService,
GetNeighborhoodsByStateService,
SaveNeighborhoodsByCityService,
GetCountryByNameOrAliasService,
GetStateByNameOrAliasService,
GetCityByNameOrAliasService
ValidateCountryByNameOrAliasService,
ValidateStateByNameOrAliasService,
ValidateCityByNameOrAliasService,
ValidateInputParamsService,
SeedNeighborhoodsByStateService,
LogSeedJobService,
GetCitiesByStateService
]
})
export class NeighborhoodsModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
LogSeed,
LogSeedDocument
} from '../../../domain/schemas/logseed.schema';
import { MongooseRepository } from '../../../domain/repository/mongoose/mongoose.repository';

@Injectable()
export class LogSeedMongoose extends MongooseRepository<
LogSeed,
LogSeedDocument
> {
constructor(
@InjectModel(LogSeed.name)
model: Model<LogSeedDocument>
) {
super(model);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { SearchNeighborhoodsDB } from 'src/microservice/domain/model/search/search-neighborhoods-db.model';
import { PlacesMongooseRepository } from '../../../domain/repository/mongoose/places-mongoose.repository';
import {
Neighborhood,
Expand All @@ -19,14 +18,4 @@ export class NeighborhoodsMongoose extends PlacesMongooseRepository<
) {
super(model);
}

async findBySearchParams(
searchParams: SearchNeighborhoodsDB
): Promise<Neighborhood[]> {
return this.model
.find(this.buildRegexFilterQuery(searchParams))
.select({ _id: 0 })
.lean()
.exec();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CheerioAPI } from 'cheerio';
import { InjectPage } from 'nest-puppeteer';
import { NeighborhoodsByCity } from '../../../../domain/model/neighborhoods-by-city.model';
import { NeighborhoodByCity } from '../../../../domain/model/neighborhoods/neighborhood-by-city.model';
import { SearchNeighborhoodsInput } from '../../../../domain/model/search/search-neighborhoods-input.model';
import { IPuppeteerNeighborhoodRepository } from '../../../../domain/interface/puppeteer/repository/puppeteer-neighborhood-repository.interface';
import { PuppeteerNeighborhoodRepository } from '../../../../domain/repository/puppeteer/neighborhood/puppeteer-neighborhood.repository';
import { Page } from '../../../../domain/interface/puppeteer/page.interface';
import { EnumTranslations } from '../../../../domain/enumerators/enum-translations.enumerator';

@Injectable()
export class GuiaMaisRepository
extends PuppeteerNeighborhoodRepository
implements IPuppeteerNeighborhoodRepository
{
language = EnumTranslations.BR;
constructor(
protected configService: ConfigService,
@InjectPage() protected readonly page: Page
Expand All @@ -23,15 +25,12 @@ export class GuiaMaisRepository
);
}

buildElementsFromDocument(
searchParams,
$: CheerioAPI
): NeighborhoodsByCity[] {
buildElementsFromDocument(searchParams, $: CheerioAPI): NeighborhoodByCity[] {
const arrNeighborhoods = [];
$('.cities.centerContent')
.find('a')
.each(function () {
const neighborhood = new NeighborhoodsByCity();
const neighborhood = new NeighborhoodByCity();

neighborhood.name = $(this).text();
neighborhood.city = `${searchParams.city.capitalize()} - ${searchParams.state.toUpperCase()}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export enum EnumTranslations {
KR = 'kr',
BR = 'br',
PT = 'pt',
NL = 'nl',
HR = 'hr',
FA = 'fa',
DE = 'de',
ES = 'es',
FR = 'fr',
JS = 'ja',
IT = 'it',
CN = 'cn',
TR = 'tr'
}
4 changes: 4 additions & 0 deletions src/microservice/domain/enumerators/enum-type-logseed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum EnumTypeLogSeed {
NeighborhoodsByState = 'NeighborhoodsByState',
NeighborhoodsByCity = 'NeighborhoodsByCity'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface AggregatedNeighborhoodsCity {
_id: { cityId: number };
count: number;
city: string;
}
Loading

0 comments on commit df4cc5e

Please sign in to comment.