feat: basic api
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
import { CommonModule, CurrencyPipe, DatePipe } from '@angular/common';
|
import { CommonModule, CurrencyPipe, DatePipe } from '@angular/common';
|
||||||
import { Component } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import { APIService } from '../../services/api';
|
||||||
|
import { Transaction } from '@shared/interfaces/transaction';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-screen-profile',
|
selector: 'app-screen-profile',
|
||||||
@@ -7,14 +9,22 @@ import { Component } from '@angular/core';
|
|||||||
templateUrl: './screen-profile.html',
|
templateUrl: './screen-profile.html',
|
||||||
styleUrl: './screen-profile.less',
|
styleUrl: './screen-profile.less',
|
||||||
})
|
})
|
||||||
export class ScreenProfile {
|
export class ScreenProfile implements OnInit{
|
||||||
username = 'John Doe';
|
username = 'John Doe';
|
||||||
email = 'john.doe@example.com';
|
email = 'john.doe@example.com';
|
||||||
|
transactions!: Transaction[];
|
||||||
|
|
||||||
|
constructor(private api: APIService){}
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
this.api.getTransactions().subscribe({
|
||||||
|
next: (transactions) => {
|
||||||
|
this.transactions = transactions;
|
||||||
|
},
|
||||||
|
error: (err) => {
|
||||||
|
console.error('Error fetching transactions:', err);
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
transactions = [
|
|
||||||
{ partner: 'Alice Smith', amount: 50.00, date: new Date('2026-03-01'), type: 'Received' },
|
|
||||||
{ partner: 'Bob Johnson', amount: -25.50, date: new Date('2026-02-28'), type: 'Sent' },
|
|
||||||
{ partner: 'Charlie Brown', amount: 75.25, date: new Date('2026-02-27'), type: 'Received' },
|
|
||||||
{ partner: 'Diana Prince', amount: -100.00, date: new Date('2026-02-25'), type: 'Sent' },
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
client/src/app/services/api.spec.ts
Normal file
16
client/src/app/services/api.spec.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { APIService } from './api';
|
||||||
|
|
||||||
|
describe('Api', () => {
|
||||||
|
let service: APIService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(APIService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
17
client/src/app/services/api.ts
Normal file
17
client/src/app/services/api.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
import { Transaction } from '@shared/interfaces/transaction'
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class APIService {
|
||||||
|
private apiUrl = 'http://localhost:3000/api'
|
||||||
|
|
||||||
|
constructor(private http: HttpClient){}
|
||||||
|
|
||||||
|
getTransactions(): Observable<Transaction[]>{
|
||||||
|
return this.http.get<Transaction[]>(this.apiUrl + '/transactions');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import express, { Express, Request, Response } from "express";
|
import express, { Express, Request, Response } from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
|
import transactionsRouter from './routes/transactions';
|
||||||
|
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
@@ -13,6 +14,8 @@ app.get("/api/health", (req: Request, res: Response) => {
|
|||||||
res.json({ status: "OK" });
|
res.json({ status: "OK" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.use('/api/transactions', transactionsRouter);
|
||||||
|
|
||||||
const PORT: number = parseInt(process.env.PORT as string) || 3000;
|
const PORT: number = parseInt(process.env.PORT as string) || 3000;
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
console.log(`Server running on port ${PORT}`);
|
console.log(`Server running on port ${PORT}`);
|
||||||
|
|||||||
17
server/src/routes/transactions.ts
Normal file
17
server/src/routes/transactions.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import { Transaction } from "@shared/interfaces/transaction"
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Mock data
|
||||||
|
const mockTransactions: Transaction[] = [
|
||||||
|
{ id: '1', partner: 'Alice Smith', amount: 50.00, date: new Date('2026-03-01'), type: 'Received' },
|
||||||
|
{ id: '2', partner: 'Bob Johnson', amount: -25.50, date: new Date('2026-02-28'), type: 'Sent' },
|
||||||
|
];
|
||||||
|
|
||||||
|
// GET /api/transactions
|
||||||
|
router.get('/', (req, res) => {
|
||||||
|
res.json(mockTransactions);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
||||||
7
shared/interfaces/transaction.ts
Normal file
7
shared/interfaces/transaction.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export interface Transaction {
|
||||||
|
id: string;
|
||||||
|
partner: string;
|
||||||
|
amount: number;
|
||||||
|
date: Date;
|
||||||
|
type: 'Sent' | 'Received';
|
||||||
|
}
|
||||||
@@ -8,7 +8,10 @@
|
|||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"target": "ES2022",
|
"target": "ES2022",
|
||||||
"module": "es2022",
|
"module": "es2022",
|
||||||
"lib": ["es2022", "dom"]
|
"lib": ["es2022", "dom"],
|
||||||
|
"paths": {
|
||||||
|
"@shared/*": ["shared/*"]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user