Skip to content

making more complicated. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,785 changes: 2,957 additions & 1,828 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@
"scripts": {
"demo": "ts-node ./src/demo-data/run.ts",
"test": "jest --env=node --colors test --verbose=true",
"prettier": "prettier --write './{src,test}/**/*.ts'"
"prettier": "prettier --write './{src,test}/**/*.ts'",
"test-pattern": "jest --env=node --colors --verbose=true",
"test-00": "npm run test-pattern 00-*",
"test-01": "npm run test-pattern 01-*",
"test-02": "npm run test-pattern 02-*",
"test-03": "npm run test-pattern 03-*",
"test-04": "npm run test-pattern 04-*",
"test-05": "npm run test-pattern 05-*",
"test-06": "npm run test-pattern 06-*",
"test-07": "npm run test-pattern 07-*",
"test-08": "npm run test-pattern 08-*",
"test-09": "npm run test-pattern 09-*",
"test-10": "npm run test-pattern 10-*"
},
"author": "",
"license": "ISC",
Expand Down
34 changes: 31 additions & 3 deletions src/AdminFlightApi.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
import { AxiosResponse } from "axios";
import { adminClient } from "./axios";
import { AddFlightRequest, Flight } from "./api";
import { AddFlightRequest, Airport, Flight } from "./api";
import { AddAirportRequest } from "./api/AddAirportRequest";

export class AdminFlightApi {
static async addFlight(
req: AddFlightRequest
): Promise<AxiosResponse<Flight>> {
return adminClient.post("/flights", req);
}

static async updateFlight(
req: AddFlightRequest
): Promise<AxiosResponse<Flight>> {
return adminClient.put("/flights", req);
}

static async fetchFlight(id: number): Promise<AxiosResponse<Flight>> {
static async fetchFlight(id: string): Promise<AxiosResponse<Flight>> {
return adminClient.get(`/flights/${id}`);
}

static deleteFlight(id: number): Promise<AxiosResponse<void>> {
static deleteFlight(id: string): Promise<AxiosResponse<void>> {
return adminClient.delete(`/flights/${id}`);
}

static async addAirport(
req: AddAirportRequest
): Promise<AxiosResponse<Airport>> {
return adminClient.post("/airports", req);
}

static async updateAirport(
id: string,
req: AddAirportRequest
): Promise<AxiosResponse<Airport>> {
return adminClient.put(`/airports/${id}`, req);
}

static async fetchAirport(id: string): Promise<AxiosResponse<Airport>> {
return adminClient.get(`/airports/${id}`);
}

static deleteAirport(id: string): Promise<AxiosResponse<void>> {
return adminClient.delete(`/airports/${id}`);
}
}
24 changes: 14 additions & 10 deletions src/CustomerFlightApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { customerClient } from "./axios";
import { Airport, SearchFlightsRequest, PageResult, Flight } from "./api";

export class CustomerFlightApi {
static async searchAirports(search: string): Promise<AxiosResponse<Airport[]>> {
return customerClient.get('/airports', { params: { search } })
}
static async searchAirports(
search: string
): Promise<AxiosResponse<Airport[]>> {
return customerClient.get("/airports", { params: { search } });
}

static async searchFlights(req: SearchFlightsRequest): Promise<AxiosResponse<PageResult<Flight>>> {
return customerClient.post('/flights/search', req)
}
static async searchFlights(
req: SearchFlightsRequest
): Promise<AxiosResponse<PageResult<Flight>>> {
return customerClient.post("/flights/search", req);
}

static async findFlightById(id: number): Promise<AxiosResponse<Flight>> {
return customerClient.get(`/flights/${id}`)
}
}
static async findFlightById(id: string): Promise<AxiosResponse<Flight>> {
return customerClient.get(`/flights/${id}`);
}
}
8 changes: 6 additions & 2 deletions src/TestApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { AxiosResponse } from "axios";
import { testingClient } from "./axios";

export class TestApi {
static async clear(): Promise<AxiosResponse<void>> {
return testingClient.post("/clear");
static async clearFlights(): Promise<AxiosResponse<void>> {
return testingClient.post("/flights/clear");
}

static async clearAirports(): Promise<AxiosResponse<void>> {
return testingClient.post("/airports/clear");
}
}
11 changes: 11 additions & 0 deletions src/api/AddAirportRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export class AddAirportRequest {
country: string;
city: string;
airport: string;

constructor(country: string, city: string, airport: string) {
this.country = country;
this.city = city;
this.airport = airport;
}
}
10 changes: 5 additions & 5 deletions src/api/AddFlightRequest.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Airport } from "./Airport";
import { Moment } from "moment";
import { formatDateTime } from "../formatting";
import { AddAirportRequest } from "./AddAirportRequest";

export class AddFlightRequest {
from: Airport;
to: Airport;
from: AddAirportRequest;
to: AddAirportRequest;
carrier: string;
departureTime: string;
arrivalTime: string;

constructor(
from: Airport,
to: Airport,
from: AddAirportRequest,
to: AddAirportRequest,
carrier: string,
departureTime: Moment,
arrivalTime: Moment
Expand Down
4 changes: 3 additions & 1 deletion src/api/Airport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export class Airport {
id: string;
country: string;
city: string;
airport: string;

constructor(country: string, city: string, airport: string) {
constructor(id: string, country: string, city: string, airport: string) {
this.id = id;
this.country = country;
this.city = city;
this.airport = airport;
Expand Down
2 changes: 1 addition & 1 deletion src/api/Flight.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Airport } from "./Airport";

export interface Flight {
id: number;
id: string;
from: Airport;
to: Airport;
carrier: string;
Expand Down
6 changes: 4 additions & 2 deletions src/api/SearchFlightsRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ export class SearchFlightsRequest {
from: string;
to: string;
departureDate: string;
page: number;

constructor(from: string, to: string, departureDate: Moment) {
constructor(from: string, to: string, departureDate: Moment, page: number) {
this.from = from;
this.to = to;
this.departureDate = formatDate(departureDate)
this.departureDate = formatDate(departureDate);
this.page = page;
}
}
10 changes: 5 additions & 5 deletions src/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import axios, { AxiosBasicCredentials } from "axios";

const credentials: AxiosBasicCredentials = {
username: "codelex-admin",
password: "Password123"
password: "Password123",
};

export const adminClient = axios.create({
baseURL: "http://localhost:8080/admin-api/",
auth: credentials
baseURL: "http://localhost:8080/api/admin/",
auth: credentials,
});

export const testingClient = axios.create({
baseURL: "http://localhost:8080/testing-api/"
baseURL: "http://localhost:8080/api/testing/",
});

export const customerClient = axios.create({
baseURL: "http://localhost:8080/api/"
baseURL: "http://localhost:8080/api/customer/",
});
43 changes: 30 additions & 13 deletions src/demo-data/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,46 @@ import csv from "csvtojson";
import _ from "lodash";
import moment from "moment";
import path from "path";
import { Airport, AddFlightRequest } from "../api";
import { AddFlightRequest } from "../api";
import { AddAirportRequest } from "../api/AddAirportRequest";

const AIRLINES_CSV = path.resolve(__dirname + "/airlines.csv");
const AIRPORTS_CSV = path.resolve(__dirname + "/airports.csv");

let airlines: string[]
let airports: Airport[]
let airlines: string[];
let airports: AddAirportRequest[];

const readAirlines = async (): Promise<string[]> => {
const tranform = (lines: any[]) => {
return lines.map(it => it.description).filter(it => it);
return lines.map((it) => it.description).filter((it) => it);
};
return csv()
.fromFile(AIRLINES_CSV)
.then(tranform);
};

const readAirports = async (): Promise<Airport[]> => {
const readAirports = async (): Promise<AddAirportRequest[]> => {
const tranform = (lines: any[]) => {
return lines
.map(it => new Airport(it.iso_country, it.municipality, it.ident))
.filter(it => it.country && it.city && it.airport)
.filter(it => it.airport !== "0");
.map(
(it) => new AddAirportRequest(it.iso_country, it.municipality, it.ident)
)
.filter((it) => it.country && it.city && it.airport)
.filter((it) => it.airport !== "0");
};
return csv()
.fromFile(AIRPORTS_CSV)
.then(tranform);
};

export const init = async () => {
await readAirlines().then(it => airlines = it)
await readAirports().then(it => airports = it)
}
await readAirlines().then((it) => (airlines = it));
await readAirports().then((it) => (airports = it));
};

export const randomAddFlightRequest = (): AddFlightRequest => {
const departureDate = moment()
.year(2019 + _.random(0, 10))
.year(2024 + _.random(0, 10))
.month(1 + _.random(0, 11))
.day(1 + _.random(0, 27))
.hour(0 + _.random(0, 24))
Expand All @@ -64,8 +67,22 @@ export const randomAddFlightRequest = (): AddFlightRequest => {
);
};

export const randomAddAirportRequest = (): AddAirportRequest => {
return airports[_.random(0, airports.length)];
};

export const generateAddFlightRequests = async (
n: number
): Promise<AddFlightRequest[]> => {
return _.range(n).map(it => randomAddFlightRequest());
return _.range(n).map((it) => randomAddFlightRequest());
};

export const generateAddAirportRequests = async (
n: number
): Promise<AddAirportRequest[]> => {
return _.range(n).map((it) => randomAddAirportRequest());
};

export const getAllAirportRequests = (): AddAirportRequest[] => {
return airports;
};
8 changes: 4 additions & 4 deletions test/00-admin-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { AxiosBasicCredentials } from "axios";
import { AdminFlightApi, adminClient } from "../src";

describe("Admin Auth", () => {
it("should not be able to access flights with wrong password", async done => {
it("should not be able to access flights with wrong password", async (done) => {
const wrongAuth: AxiosBasicCredentials = {
username: "admin",
password: "00000000"
password: "00000000",
};

try {
Expand All @@ -16,9 +16,9 @@ describe("Admin Auth", () => {
}
});

it("should be able to access flights with correct password", async done => {
it("should be able to access flights with correct password", async (done) => {
try {
await AdminFlightApi.fetchFlight(123);
await AdminFlightApi.fetchFlight("123");
} catch (e) {
expect(e.response.status).toBe(404);
done();
Expand Down
11 changes: 9 additions & 2 deletions test/01-testing.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { TestApi } from "../src";

describe("Testing", () => {
it("should clear flights", async done => {
const response = await TestApi.clear();
it("should clear flights", async (done) => {
const response = await TestApi.clearFlights();
expect(response.status).toBe(200);

done();
});

it("should clear airports", async (done) => {
const response = await TestApi.clearAirports();
expect(response.status).toBe(200);

done();
Expand Down
Loading