Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ export class ExchangeRate {
public country!: string;

@Field(() => String)
public currncy!: string;
public currency!: string;

@Field(() => Number)
public amount!: number;

@Field(() => String)
public code!: string;

@Field(() => Number)
public rate!: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { ExchangeRateService } from './exchange-rate.service';
export class ExchangeRateResolver {
constructor(private readonly exchangeRateService: ExchangeRateService) {}

// TODO: Implement a GraphQL Query that returns the exchange rates
@Query(() => String)
async exchangeRates(): Promise<String> {
return 'Hello';
@Query(() => [ExchangeRate])
async exchangeRates(): Promise<ExchangeRate[]> {
const rates: ExchangeRate[] = await this.exchangeRateService.getExchangeRates();
return rates;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { Injectable } from '@nestjs/common';
import { ExchangeRate } from '@cuid/entities';
import fetch from 'node-fetch';

const CNB_URL = 'https://www.cnb.cz/cs/financni_trhy/devizovy_trh/kurzy_devizoveho_trhu/denni_kurz.txt';

@Injectable()
export class ExchangeRateService {
public getExchangeRates = async () => {
const rates: ExchangeRate[] = [];

// TODO: Implement the fetching and parsing of the exchange rates.
// Use this method in the resolver.
// Fetch the exchange rates from CNB website
const response: Response = await fetch(CNB_URL);
const data: string = await response.text();

// Split the data into lines and remove header
const rateLines: string[] = data.trim().split('\n').splice(2);

// Loop through each line and push a new object to the array (wanted to use map, but since rates is a const, I use loop with Array.push instead)
for (const rateLine of rateLines) {
const [country, currency, amount, code, rate] = rateLine.split('|');
const currencyObject: ExchangeRate = {
country,
currency,
amount: parseInt(amount),
code,
rate: parseFloat(rate.replace(',', '.')),
};
rates.push(currencyObject);
}

return rates;
};
Expand Down
25 changes: 23 additions & 2 deletions fullstack/task/packages/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { useQuery, gql } from '@apollo/client';
import ExchangeRateTable from './ExchangeRateTable';

const EXCHANGE_RATES = gql`
query GetExchangeRates {
exchangeRates {
country
currency
amount
code
rate
}
}
`;

function App() {
// TODO: Add the FE for exchange rates here.
const { loading, error, data } = useQuery(EXCHANGE_RATES);

if (loading) return <p>Loading...</p>;
if (error) return <p>Error: {error.message}</p>;

const exchangeRatesData = data.exchangeRates;

return (
<div>
<p>Add the exchange rates here</p>
<h1>Exchange Rates</h1>
<ExchangeRateTable data={exchangeRatesData} />
</div>
);
}
Expand Down
58 changes: 58 additions & 0 deletions fullstack/task/packages/web/src/ExchangeRateTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
interface ExchangeRate {
country: string;
currency: string;
amount: number;
code: string;
rate: number;
}

interface ExchangeRateTableProps {
data: ExchangeRate[];
}

const ExchangeRateTable: React.FC<ExchangeRateTableProps> = ({ data }) => {
return (
<table style={{ borderCollapse: 'collapse', width: '800px' }}>
<thead>
<tr>
<th style={tableHeaderStyle}>Country</th>
<th style={tableHeaderStyle}>Currency</th>
<th style={tableHeaderStyle}>Amount</th>
<th style={tableHeaderStyle}>Code</th>
<th style={tableHeaderStyle}>Rate</th>
</tr>
</thead>
<tbody>
{data.map((row, index) => (
<tr key={index} style={index % 2 === 0 ? evenRowStyle : oddRowStyle}>
<td style={tableCellStyle}>{row.country}</td>
<td style={tableCellStyle}>{row.currency}</td>
<td style={tableCellStyle}>{row.amount}</td>
<td style={tableCellStyle}>{row.code}</td>
<td style={tableCellStyle}>{row.rate}</td>
</tr>
))}
</tbody>
</table>
);
};

const tableHeaderStyle: React.CSSProperties = {
borderBottom: '2px solid #ccc',
padding: '8px',
textAlign: "left",
};

const evenRowStyle: React.CSSProperties = {
backgroundColor: '#f2f2f2',
};

const oddRowStyle: React.CSSProperties = {
backgroundColor: '#ffffff',
};

const tableCellStyle: React.CSSProperties = {
padding: '8px',
};

export default ExchangeRateTable;