Skip to content

Commit

Permalink
feature: places core terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
ldiego73 committed Jun 10, 2020
1 parent 9b7c111 commit f388a65
Show file tree
Hide file tree
Showing 28 changed files with 245 additions and 152 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
yarn-error.log
yarn-error.log
.env
1 change: 1 addition & 0 deletions microservices/countries/core/src/domain/index.ts
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";
1 change: 0 additions & 1 deletion microservices/places/.env

This file was deleted.

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));
}
}
1 change: 1 addition & 0 deletions microservices/places/core/src/application/mappers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./address.mapper";
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));
}
}
}
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 };
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 };
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ import { Either, Result } from "@micro/kernel/lib/result";

import { AddressRepository } from "../../../domain";
import { AddressDto, SearchDto } from "../../dtos";
import { AddressMapper } from "../../mappers/address.mapper";

type Response<T> = Either<UseCaseUnexpectedError, T>;

export class SearchAddress
export class SearchAddressUseCase
implements UseCase<SearchDto, Response<AddressDto[]>> {
private repository: AddressRepository;

constructor(repository: AddressRepository) {
this.repository = repository;
}
constructor(
private repository: AddressRepository,
private mapper: AddressMapper
) {}

async execute(request: SearchDto): Promise<Response<AddressDto[]>> {
try {
const addresses = await this.repository.search(
request.country,
request.address
);
return Result.ok([]);
return Result.ok(this.mapper.toCollection(addresses));
} catch (err) {
return Result.fail(UseCaseUnexpectedError.create(err));
}
Expand Down
32 changes: 2 additions & 30 deletions microservices/places/core/src/domain/address.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { Entity, 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 { Country } from "./country";
import { AddressInvalidError } from "./errors";
import { LatLng } from "./lat-lng";

interface AddressProps {
export interface AddressProps {
country: Country;
address: string;
latLng: LatLng;
Expand All @@ -34,31 +30,7 @@ export class Address extends Entity<AddressProps> {
return this.props.latLng.lng;
}

private constructor(props: AddressProps, id?: UniqueEntityId) {
constructor(props: AddressProps, id?: UniqueEntityId) {
super(props, id);
}

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));
}
}
18 changes: 2 additions & 16 deletions microservices/places/core/src/domain/country.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ValueObject } from "@micro/kernel/lib/domain/value-object";
import { Either,Result } from "@micro/kernel/lib/result";
import * as validator from "@micro/utils";

import { CountryInvalidError } from "./errors";

interface CountryProps {
export interface CountryProps {
value: string;
}

Expand All @@ -15,17 +11,7 @@ export class Country extends ValueObject<CountryProps> {
return this.props.value;
}

private constructor(props: CountryProps) {
constructor(props: CountryProps) {
super(props);
}

public static create(country: string): Either<CountryInvalidError, Country> {
const validate = validator.pattern(this.COUNTRY_PATTERN)(country);

if (validate.success) {
return Result.ok(new Country({ value: country }));
}

return Result.fail(CountryInvalidError.create(country));
}
}
37 changes: 37 additions & 0 deletions microservices/places/core/src/domain/factory/address.factory.ts
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 microservices/places/core/src/domain/factory/country.factory.ts
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);
}
}
3 changes: 3 additions & 0 deletions microservices/places/core/src/domain/factory/index.ts
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 microservices/places/core/src/domain/factory/lat-lng.factory.ts
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);
}
}
1 change: 1 addition & 0 deletions microservices/places/core/src/domain/index.ts
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";
35 changes: 2 additions & 33 deletions microservices/places/core/src/domain/lat-lng.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ValueObject } from "@micro/kernel/lib/domain/value-object";
import { Either,Result } from "@micro/kernel/lib/result";
import * as validator from "@micro/utils";

import { LatitudeInvalidError, LongitudeInvalidError } from "./errors";

interface LatLngProps {
export interface LatLngProps {
lat: string;
lng: string;
}
Expand All @@ -21,34 +17,7 @@ export class LatLng extends ValueObject<LatLngProps> {
return this.props.lng;
}

private constructor(props: LatLngProps) {
constructor(props: LatLngProps) {
super(props);
}

public static create(
lat: string,
lng: string
): Either<LatitudeInvalidError | LongitudeInvalidError, LatLng> {
const validateLat = validator.pattern(this.LAT_PATTERN)(lat);
const validateLng = validator.pattern(this.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 }));
}
}
8 changes: 0 additions & 8 deletions microservices/places/core/src/domain/repository.ts

This file was deleted.

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[]>;
}
1 change: 1 addition & 0 deletions microservices/places/core/src/domain/repository/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./address.repository";
9 changes: 9 additions & 0 deletions microservices/places/core/src/infraestructure/index.ts
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 };
Loading

0 comments on commit f388a65

Please sign in to comment.