feature/get-places-api #2
2
.env.development
Normal file
2
.env.development
Normal file
@ -0,0 +1,2 @@
|
||||
VITE_API_ORIGIN = http://176.53.196.42:6001/api/v1
|
||||
VITE_API_PLACES = /place
|
@ -1,4 +1,9 @@
|
||||
type Place = {
|
||||
type Places = {
|
||||
placeType: string;
|
||||
name: string;
|
||||
qr: null;
|
||||
id: string;
|
||||
parentId: string | null;
|
||||
};
|
||||
|
||||
export default Places;
|
||||
|
34
src/business-logic/core/places/common/model/placesModel.ts
Normal file
34
src/business-logic/core/places/common/model/placesModel.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import Places from "../entity/placeEntity";
|
||||
|
||||
class PlacesModel {
|
||||
private placeType: string;
|
||||
|
||||
private name: string;
|
||||
|
||||
private qr: null;
|
||||
|
||||
private id: string;
|
||||
|
||||
private parentId: string | null;
|
||||
|
||||
constructor(store: Places) {
|
||||
const { id, name, parentId, placeType, qr } = store;
|
||||
this.parentId = parentId;
|
||||
this.id = id;
|
||||
this.qr = qr;
|
||||
this.name = name;
|
||||
this.placeType = placeType;
|
||||
}
|
||||
|
||||
getData(): Places {
|
||||
return {
|
||||
id: this.id,
|
||||
name: this.name,
|
||||
parentId: this.parentId,
|
||||
placeType: this.placeType,
|
||||
qr: this.qr,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default PlacesModel;
|
@ -0,0 +1,16 @@
|
||||
import PlacesModel from "../../../common/model/placesModel";
|
||||
import getPlacesRO from "../response-object/getPlacesRO";
|
||||
import { GetPlacesResponse } from "../response-object/protocols";
|
||||
|
||||
const getPlacesRepo = async (httpHandler: () => Promise<GetPlacesResponse>) => {
|
||||
// call httpHandler
|
||||
const placesResponse = await httpHandler();
|
||||
// user response object to turn it to the data that we want
|
||||
const placesData = getPlacesRO(placesResponse);
|
||||
// make model
|
||||
const placesModel = new PlacesModel(placesData);
|
||||
// return the model
|
||||
return placesModel;
|
||||
};
|
||||
|
||||
export default getPlacesRepo;
|
@ -1,5 +1,5 @@
|
||||
import { GetPlacesRO } from "../response-object/protocols";
|
||||
import PlacesModel from "../../../common/model/placesModel";
|
||||
|
||||
type IGetPlacesRepo = () => Promise<GetPlacesRO>;
|
||||
type IGetPlacesRepo = () => Promise<PlacesModel>;
|
||||
|
||||
export default IGetPlacesRepo;
|
||||
|
@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-underscore-dangle */
|
||||
import { GetPlacesRO, GetPlacesResponse } from "./protocols";
|
||||
|
||||
const getPlacesRO = (placeResponse: GetPlacesResponse): GetPlacesRO => ({
|
||||
@ -8,6 +9,7 @@ const getPlacesRO = (placeResponse: GetPlacesResponse): GetPlacesRO => ({
|
||||
availableServices: placeResponse.available_services,
|
||||
createdAt: placeResponse.createdAt,
|
||||
updatedAt: placeResponse.updatedAt,
|
||||
})
|
||||
qr: null,
|
||||
});
|
||||
|
||||
export default getPlacesRO;
|
@ -1,3 +1,5 @@
|
||||
import Places from "../../../common/entity/placeEntity";
|
||||
|
||||
export type GetPlacesResponse = {
|
||||
_id: string;
|
||||
place_type: string;
|
||||
@ -8,11 +10,7 @@ export type GetPlacesResponse = {
|
||||
available_services: string[];
|
||||
};
|
||||
|
||||
export type GetPlacesRO = {
|
||||
id: string;
|
||||
placeType: string;
|
||||
name: string;
|
||||
parentId: string | null;
|
||||
export type GetPlacesRO = Places & {
|
||||
availableServices: string[];
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
|
3
src/business-logic/core/places/get-places/index.ts
Normal file
3
src/business-logic/core/places/get-places/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import getPlaces from "./infra/getPlacesInfra";
|
||||
|
||||
export default getPlaces;
|
@ -0,0 +1,18 @@
|
||||
import PlacesModel from "../../common/model/placesModel";
|
||||
import getPlacesRepo from "../data/repository/GetPlacesRepo";
|
||||
import IGetPlacesRepo from "../data/repository/IGetPlacesRepo";
|
||||
import GettingPlacesUsecase from "../usecase/getPlaceUsecase";
|
||||
import { IgetPlacesInfra } from "./protocols";
|
||||
|
||||
const getPlaces = ({
|
||||
httpHandler,
|
||||
}: IgetPlacesInfra): (() => Promise<PlacesModel>) => {
|
||||
// get httpHandler
|
||||
const repository: IGetPlacesRepo = () => getPlacesRepo(httpHandler);
|
||||
// connet usecase and repository
|
||||
const usecase = new GettingPlacesUsecase(repository);
|
||||
// return method to use
|
||||
return () => usecase.execute();
|
||||
};
|
||||
|
||||
export default getPlaces;
|
@ -0,0 +1,5 @@
|
||||
import { GetPlacesResponse } from "../data/response-object/protocols";
|
||||
|
||||
export interface IgetPlacesInfra {
|
||||
httpHandler: () => Promise<GetPlacesResponse>;
|
||||
}
|
3
src/business-logic/core/places/get-places/port.ts
Normal file
3
src/business-logic/core/places/get-places/port.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { type IgetPlacesInfra } from "./infra/protocols";
|
||||
|
||||
export default IgetPlacesInfra;
|
@ -1,3 +1,4 @@
|
||||
import PlacesModel from "../../../common/model/placesModel";
|
||||
import IGetPlacesRepo from "../../data/repository/IGetPlacesRepo";
|
||||
import { GetPlacesRO } from "../../data/response-object/protocols";
|
||||
import GettingPlacesUsecase from "../getPlaceUsecase";
|
||||
@ -10,10 +11,13 @@ const mockedRO: GetPlacesRO = {
|
||||
parentId: null,
|
||||
placeType: "continent",
|
||||
updatedAt: "updatedTime",
|
||||
qr: null,
|
||||
};
|
||||
|
||||
const model = new PlacesModel(mockedRO);
|
||||
const mockedRepo: IGetPlacesRepo = jest
|
||||
.fn()
|
||||
.mockImplementation(async () => mockedRO);
|
||||
.mockImplementation(async () => model);
|
||||
|
||||
describe("getting places usecase tests", () => {
|
||||
it("call repository on execute usecase", async () => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import PlacesModel from "../../common/model/placesModel";
|
||||
import IGetPlacesRepo from "../data/repository/IGetPlacesRepo";
|
||||
import { GetPlacesRO } from "../data/response-object/protocols";
|
||||
|
||||
/**
|
||||
* this usecase is responsible for calling the repo and returning the places data from the repository
|
||||
@ -11,8 +11,10 @@ class GettingPlacesUsecase {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
async execute(): Promise<GetPlacesRO> {
|
||||
async execute(): Promise<PlacesModel> {
|
||||
// call usecase and return the repo
|
||||
const placesData = await this.repository();
|
||||
return placesData;
|
||||
}
|
||||
}
|
||||
|
||||
|
30
src/driven/adapters/get-places-adapter/getPlacesAdapter.ts
Normal file
30
src/driven/adapters/get-places-adapter/getPlacesAdapter.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { GetPlacesResponse } from "~/business-logic/core/places/get-places/data/response-object/protocols";
|
||||
import IGetPlacesPort from "~/business-logic/core/places/get-places/port";
|
||||
import { HTTPPovider } from "~/driven/boundaries/http-boundary/httpBoundary";
|
||||
import { HttpOptionsType } from "~/driven/boundaries/http-boundary/protocols";
|
||||
import { apiUrls } from "~/driven/utils/configs/appConfig";
|
||||
|
||||
const getPlacesAdapter = async (): Promise<IGetPlacesPort> => {
|
||||
// url of api
|
||||
const url = apiUrls.core.places;
|
||||
// make the options of request
|
||||
const options: HttpOptionsType = {
|
||||
url,
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
};
|
||||
// make the httpHandler
|
||||
const httpProvider = new HTTPPovider();
|
||||
|
||||
const httpHandler = async () =>
|
||||
httpProvider.request<GetPlacesResponse>(options);
|
||||
|
||||
// return the method
|
||||
return {
|
||||
httpHandler,
|
||||
};
|
||||
};
|
||||
|
||||
export default getPlacesAdapter;
|
@ -1,7 +1,8 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { staticMessages } from "~/driven/utils/constants/staticMessages";
|
||||
import { ApiGlobalResponseObject } from "~/driven/utils/protocols/serviceProtocols";
|
||||
|
||||
export class HTTPBoundary {
|
||||
export class HTTPPovider {
|
||||
async request<R>(customOptions: AxiosRequestConfig) {
|
||||
const options: AxiosRequestConfig = {
|
||||
...customOptions,
|
||||
@ -10,9 +11,9 @@ export class HTTPBoundary {
|
||||
Authorization: `Bearer eyJhbGciOi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REnjfKhw3gvjK9YSSBeMf08abDexbVSJW4rbPxtmcO9WGdZN8C1k7O-_9VHz-3NBCq7vRYBNID--xVTga_MMv3mCORzHtUdx2D7B2d7YxBAjQs5x9JCl_eXuN_IHODc2HMww-s5akquzdEKGFSfASu30NpNZucbO9XyTMkAWuV-H9Xd73eq5o-tSd0jMOfY9G1it9xLAM2Gk4xmIuUA16aLnjY7_rmm3rOypw`,
|
||||
},
|
||||
};
|
||||
const response = await axios<R>(options);
|
||||
const response = await axios<ApiGlobalResponseObject<R>>(options);
|
||||
if (!response) throw new Error(staticMessages.service.errors[500]);
|
||||
|
||||
return response.data;
|
||||
return response.data.data;
|
||||
}
|
||||
}
|
||||
|
3
src/driven/boundaries/http-boundary/protocols.ts
Normal file
3
src/driven/boundaries/http-boundary/protocols.ts
Normal file
@ -0,0 +1,3 @@
|
||||
import { AxiosRequestConfig } from "axios";
|
||||
|
||||
export type HttpOptionsType = AxiosRequestConfig;
|
@ -1,4 +1,5 @@
|
||||
import { icons } from "../constants/assertUrls";
|
||||
import { ENVs } from "../constants/envs";
|
||||
import { staticMessages } from "../constants/staticMessages";
|
||||
|
||||
export const appConfig = {};
|
||||
@ -21,5 +22,9 @@ export const routesData = {
|
||||
},
|
||||
};
|
||||
|
||||
const baseApiUrl = import.meta.env.BASE_API_URL;
|
||||
export const apiUrls = {};
|
||||
const baseApiUrl = ENVs.apiOrigin;
|
||||
export const apiUrls = {
|
||||
core: {
|
||||
places: `${baseApiUrl}${ENVs.apiPlaces}`,
|
||||
},
|
||||
};
|
||||
|
4
src/driven/utils/constants/envs.ts
Normal file
4
src/driven/utils/constants/envs.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export const ENVs = {
|
||||
apiOrigin: process.env.VITE_API_ORIGIN,
|
||||
apiPlaces: process.env.VITE_API_PLACES,
|
||||
};
|
@ -1,6 +1,6 @@
|
||||
export type RequestMethods = "get" | "post" | "put" | "delete";
|
||||
|
||||
export type apiGlobalResponseObject<DataType> = {
|
||||
export type ApiGlobalResponseObject<DataType> = {
|
||||
type: "Success" | "client Error" | string;
|
||||
status: 200 | 400 | 401 | 500 | number;
|
||||
message: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user