added services and interfaces for expense types
This commit is contained in:
parent
bd18fecdff
commit
7bb21f8796
8 changed files with 142 additions and 5 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ export class ExpenseList implements OnInit {
|
||||||
public constructor(private readonly expenses: Expenses) { }
|
public constructor(private readonly expenses: Expenses) { }
|
||||||
|
|
||||||
public ngOnInit() {
|
public ngOnInit() {
|
||||||
void this.expenses.getExpenses();
|
this.expenses.getExpenses().then(expenses => {
|
||||||
|
console.log({ expenses });
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
src/app/services/categories.ts
Normal file
19
src/app/services/categories.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class Categories {
|
||||||
|
public static readonly BASE_URL = 'http://localhost:3000/common-cents/categories';
|
||||||
|
|
||||||
|
public async getCategories(): Promise<Category[]> {
|
||||||
|
console.log('getCategories called');
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Category {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
@ -1,12 +1,32 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { SubCategory } from './sub-categories';
|
||||||
|
import { Category } from './categories';
|
||||||
|
import { Merchant } from './merchants';
|
||||||
|
import { Tag } from './tags';
|
||||||
|
import { Http } from './http';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class Expenses {
|
export class Expenses {
|
||||||
public static readonly BASE_URL = 'http://localhost:3000/common-cents/expenses';
|
public static readonly EXPENSES_URI = 'http://localhost:3000/common-cents/expenses';
|
||||||
|
|
||||||
public async getExpenses(): Promise<void> {
|
public constructor(private readonly http: Http) { }
|
||||||
console.log('getExpenses called');
|
|
||||||
|
public async getExpenses(): Promise<Expense[]> {
|
||||||
|
return this.http.get<Expense[]>(Expenses.EXPENSES_URI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Expense {
|
||||||
|
id: string;
|
||||||
|
year: string;
|
||||||
|
month: string;
|
||||||
|
day: string;
|
||||||
|
cents: number;
|
||||||
|
description?: string;
|
||||||
|
category: Category;
|
||||||
|
subCategory?: SubCategory;
|
||||||
|
merchant?: Merchant;
|
||||||
|
tags: Tag[];
|
||||||
|
}
|
||||||
|
|
|
||||||
40
src/app/services/http.ts
Normal file
40
src/app/services/http.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { firstValueFrom } from 'rxjs';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class Http {
|
||||||
|
public constructor(private httpClient: HttpClient) { }
|
||||||
|
|
||||||
|
public async get<T>(url: string): Promise<T> {
|
||||||
|
return this.request<T>(url, 'get');
|
||||||
|
}
|
||||||
|
|
||||||
|
public async post<T>(url: string, body?: any): Promise<T> {
|
||||||
|
return this.request<T>(url, 'post', body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async put<T>(url: string, body?: any): Promise<T> {
|
||||||
|
return this.request<T>(url, 'put', body);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async delete<T>(url: string): Promise<T> {
|
||||||
|
return this.request<T>(url, 'delete');
|
||||||
|
}
|
||||||
|
|
||||||
|
private async request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete', body?: any): Promise<T> {
|
||||||
|
const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' };
|
||||||
|
switch (method) {
|
||||||
|
case 'post':
|
||||||
|
return firstValueFrom(this.httpClient.post<T>(url, body, { headers }));
|
||||||
|
case 'put':
|
||||||
|
return firstValueFrom(this.httpClient.put<T>(url, body, { headers }));
|
||||||
|
case 'delete':
|
||||||
|
return firstValueFrom(this.httpClient.delete<T>(url, { headers }));
|
||||||
|
default:
|
||||||
|
return firstValueFrom(this.httpClient.get<T>(url, { headers }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/app/services/merchants.ts
Normal file
19
src/app/services/merchants.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class Merchants {
|
||||||
|
public static readonly BASE_URL = 'http://localhost:3000/common-cents/merchants';
|
||||||
|
|
||||||
|
public async getMerchants(): Promise<Merchant[]> {
|
||||||
|
console.log('getMerchants called');
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Merchant {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
19
src/app/services/sub-categories.ts
Normal file
19
src/app/services/sub-categories.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class SubCategories {
|
||||||
|
public static readonly BASE_URL = 'http://localhost:3000/common-cents/sub-categories';
|
||||||
|
|
||||||
|
public async getSubCategories(): Promise<SubCategory[]> {
|
||||||
|
console.log('getSubCategories called');
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface SubCategory {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
19
src/app/services/tags.ts
Normal file
19
src/app/services/tags.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class Tags {
|
||||||
|
public static readonly BASE_URL = 'http://localhost:3000/common-cents/tags';
|
||||||
|
|
||||||
|
public async getTags(): Promise<Tag[]> {
|
||||||
|
console.log('getTags called');
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Tag {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue