common-cents-web/src/app/services/category.service.ts
2026-02-26 12:10:55 -06:00

36 lines
1 KiB
TypeScript

import { Injectable, signal } from '@angular/core';
import { HttpService } from './http.service';
@Injectable({
providedIn: 'root'
})
export class CategoryService {
private internalCategories = signal<Category[]>([]);
public readonly categories = this.internalCategories.asReadonly();
public readonly categoryPath = 'http://localhost:3000/common-cents/categories';
public constructor(private readonly http: HttpService) {
void this.fetchCategories();
}
public async fetchCategories(): Promise<void> {
this.internalCategories.set(await this.http.get<Category[]>(this.categoryPath));
}
public async createCategory(category: CreateCategory): Promise<Category> {
return await this.http.post<Category>(this.categoryPath, category);
}
public async updateCategory(category: Category): Promise<Category> {
return await this.http.put<Category>(this.categoryPath, category);
}
}
export interface Category {
id: string;
name: string;
}
export interface CreateCategory {
name: string;
}