-
Notifications
You must be signed in to change notification settings - Fork 2
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
28 changed files
with
245 additions
and
152 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
yarn-error.log | ||
yarn-error.log | ||
.env |
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,4 +1,5 @@ | ||
export * from "./iso"; | ||
export * from "./country"; | ||
export * from "./factory"; | ||
export * from "./repository"; | ||
export * from "./errors"; |
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
microservices/places/core/src/application/mappers/address.mapper.ts
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,20 @@ | ||
import { Address } from "../../domain"; | ||
import { AddressDto } from "../dtos"; | ||
|
||
export class AddressMapper { | ||
toDto(address: Address): AddressDto { | ||
return { | ||
id: address.id.toValue(), | ||
country: address.country, | ||
description: address.address, | ||
latLng: { | ||
lat: address.lat, | ||
lng: address.lng, | ||
}, | ||
} as AddressDto; | ||
} | ||
|
||
toCollection(address: Address[]): AddressDto[] { | ||
return address.map((a) => this.toDto(a)); | ||
} | ||
} |
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 * from "./address.mapper"; |
25 changes: 25 additions & 0 deletions
25
...rvices/places/core/src/application/uses-cases/geocode-address/geocode-address.use-case.ts
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,25 @@ | ||
import { UseCase, UseCaseUnexpectedError } from "@micro/kernel/lib/application"; | ||
import { Either, Result } from "@micro/kernel/lib/result"; | ||
|
||
import { AddressRepository } from "../../../domain"; | ||
import { AddressDto, LocationDto } from "../../dtos"; | ||
import { AddressMapper } from "../../mappers/address.mapper"; | ||
|
||
type Response<T> = Either<UseCaseUnexpectedError, T>; | ||
|
||
export class GeocodeAddressUseCase | ||
implements UseCase<LocationDto, Response<AddressDto[]>> { | ||
constructor( | ||
private repository: AddressRepository, | ||
private mapper: AddressMapper | ||
) {} | ||
|
||
async execute(request: LocationDto): Promise<Response<AddressDto[]>> { | ||
try { | ||
const addresses = await this.repository.geocode(request.lat, request.lng); | ||
return Result.ok(this.mapper.toCollection(addresses)); | ||
} catch (err) { | ||
return Result.fail(UseCaseUnexpectedError.create(err)); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
microservices/places/core/src/application/uses-cases/geocode-address/index.ts
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,12 @@ | ||
import { addressRepository } from "../../../infraestructure"; | ||
import { AddressMapper } from "../../mappers"; | ||
import { GeocodeAddressUseCase } from "./geocode-address.use-case"; | ||
|
||
const mapper = new AddressMapper(); | ||
const geocodeAddressUseCase = new GeocodeAddressUseCase( | ||
addressRepository, | ||
mapper | ||
); | ||
|
||
export * from "./geocode-address.use-case"; | ||
export { geocodeAddressUseCase }; |
12 changes: 12 additions & 0 deletions
12
microservices/places/core/src/application/uses-cases/search-address/index.ts
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,12 @@ | ||
import { addressRepository } from "../../../infraestructure"; | ||
import { AddressMapper } from "../../mappers"; | ||
import { SearchAddressUseCase } from "./search-address.use-case"; | ||
|
||
const mapper = new AddressMapper(); | ||
const searchAddressUseCase = new SearchAddressUseCase( | ||
addressRepository, | ||
mapper | ||
); | ||
|
||
export * from "./search-address.use-case"; | ||
export { searchAddressUseCase }; |
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
37 changes: 37 additions & 0 deletions
37
microservices/places/core/src/domain/factory/address.factory.ts
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,37 @@ | ||
import { UniqueEntityId } from "@micro/kernel/lib/domain"; | ||
import { Either, Result } from "@micro/kernel/lib/result"; | ||
import { Schema } from "@micro/utils"; | ||
import * as validator from "@micro/utils"; | ||
|
||
import { Address, AddressProps } from "../address"; | ||
import { AddressInvalidError } from "../errors"; | ||
|
||
export class AddressFactory { | ||
public static create( | ||
props: AddressProps, | ||
id?: UniqueEntityId | ||
): Either<AddressInvalidError, Address> { | ||
const schema: Schema<AddressProps> = { | ||
country: validator.object({ | ||
value: validator.string(), | ||
}), | ||
address: validator.string(), | ||
latLng: validator.object({ | ||
lat: validator.string(), | ||
lng: validator.string(), | ||
}), | ||
}; | ||
|
||
const validate = validator.validate(schema, props); | ||
|
||
if (validate.success) { | ||
return Result.ok(new Address(props, id)); | ||
} | ||
|
||
return Result.fail(AddressInvalidError.create(validate.message as string)); | ||
} | ||
|
||
public static createFrom(props: AddressProps, id?: UniqueEntityId): Address { | ||
return new Address(props, id); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
microservices/places/core/src/domain/factory/country.factory.ts
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,23 @@ | ||
import { Either, Result } from "@micro/kernel/lib/result"; | ||
import * as validator from "@micro/utils"; | ||
|
||
import { Country, CountryProps } from "../country"; | ||
import { CountryInvalidError } from "../errors"; | ||
|
||
export class CountryFactory { | ||
public static create(country: string): Either<CountryInvalidError, Country> { | ||
const validate = validator.pattern(Country.COUNTRY_PATTERN)(country); | ||
|
||
if (validate.success) { | ||
return Result.ok(new Country({ value: country })); | ||
} | ||
|
||
return Result.fail(CountryInvalidError.create(country)); | ||
} | ||
|
||
public static createFrom(country: string): Country { | ||
const props: CountryProps = { value: country }; | ||
|
||
return new Country(props); | ||
} | ||
} |
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 * from "./address.factory"; | ||
export * from "./country.factory"; | ||
export * from "./lat-lng.factory"; |
43 changes: 43 additions & 0 deletions
43
microservices/places/core/src/domain/factory/lat-lng.factory.ts
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,43 @@ | ||
import { Either, Result } from "@micro/kernel/lib/result"; | ||
import * as validator from "@micro/utils"; | ||
|
||
import { LatitudeInvalidError, LongitudeInvalidError } from "../errors"; | ||
import { LatLng, LatLngProps } from "../lat-lng"; | ||
|
||
export class LatLngFactory { | ||
public static create( | ||
lat: string, | ||
lng: string | ||
): Either<LatitudeInvalidError | LongitudeInvalidError, LatLng> { | ||
const validateLat = validator.pattern(LatLng.LAT_PATTERN)(lat); | ||
const validateLng = validator.pattern(LatLng.LNG_PATTERN)(lng); | ||
|
||
const errors: Either< | ||
LatitudeInvalidError | LongitudeInvalidError, | ||
LatLng | ||
>[] = []; | ||
|
||
if (!validateLat.success) { | ||
errors.push(Result.fail(LatitudeInvalidError.create(lat))); | ||
} | ||
|
||
if (!validateLng.success) { | ||
errors.push(Result.fail(LongitudeInvalidError.create(lng))); | ||
} | ||
|
||
if (errors.length > 0) { | ||
return Result.combine(errors); | ||
} | ||
|
||
return Result.ok(new LatLng({ lat, lng })); | ||
} | ||
|
||
public static createFrom(lat: string, lng: string) { | ||
const props: LatLngProps = { | ||
lat, | ||
lng, | ||
}; | ||
|
||
return new LatLng(props); | ||
} | ||
} |
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,5 +1,6 @@ | ||
export * from "./errors"; | ||
export * from "./address"; | ||
export * from "./country"; | ||
export * from "./factory"; | ||
export * from "./lat-lng"; | ||
export * from "./repository"; |
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 was deleted.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
microservices/places/core/src/domain/repository/address.repository.ts
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,6 @@ | ||
import { Address } from "../address"; | ||
|
||
export interface AddressRepository { | ||
search(country: string, address: string): Promise<Address[]>; | ||
geocode(lat: string, lng: string): Promise<Address[]>; | ||
} |
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 * from "./address.repository"; |
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,9 @@ | ||
import { AddressMapper } from "./mappers"; | ||
import { AddressRepositoryImpl } from "./repository"; | ||
import { SearchService } from "./services"; | ||
|
||
const mapper = new AddressMapper(); | ||
const service = new SearchService(); | ||
const addressRepository = new AddressRepositoryImpl(service, mapper); | ||
|
||
export { addressRepository }; |
Oops, something went wrong.