Add TransactionService

This commit is contained in:
William Brawner 2018-08-27 21:40:17 -05:00
parent 2074b5a5aa
commit c1903c8f40
3 changed files with 48 additions and 18 deletions

View file

@ -0,0 +1,15 @@
import { TestBed, inject } from '@angular/core/testing';
import { TransactionService } from './transaction.service';
describe('TransactionService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [TransactionService]
});
});
it('should be created', inject([TransactionService], (service: TransactionService) => {
expect(service).toBeTruthy();
}));
});

View file

@ -0,0 +1,27 @@
import { Injectable } from '@angular/core';
import { of, Observable } from 'rxjs';
import { Transaction } from './transaction';
import { TransactionType } from './transaction.type';
@Injectable({
providedIn: 'root'
})
export class TransactionService {
constructor() { }
getTransactions(): Observable<Transaction[]> {
return of([
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
])
}
}

View file

@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Transaction } from '../transaction';
import { TransactionType } from '../transaction.type';
import { TransactionService } from '../transaction.service';
@Component({
selector: 'app-transactions',
@ -12,29 +13,16 @@ export class TransactionsComponent implements OnInit {
public transactionType = TransactionType;
public transactions: Transaction[]
constructor() { }
constructor(private transactionService: TransactionService) { }
ngOnInit() {
this.getTransactions()
}
onScroll() {
}
getTransactions(): void {
this.transactions = [
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Spent some money", title: "An Expense", type: TransactionType.EXPENSE, categoryId: 0},
{id: 0, amount: Math.random() * 100, date: new Date(), description: "Earned some money", title: "Some Income", type: TransactionType.INCOME, categoryId: 0},
]
this.transactionService.getTransactions().subscribe(transactions => {
this.transactions = transactions;
})
}
}