homebox/frontend/lib/api/classes/items.ts

65 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-09-05 00:55:52 +00:00
import { BaseAPI, route } from '../base';
2022-09-03 09:17:57 +00:00
import { Label } from './labels';
import { Location } from './locations';
import { Results } from './types';
export interface ItemCreate {
name: string;
description: string;
locationId: string;
labelIds: string[];
}
export interface Item {
createdAt: string;
description: string;
id: string;
labels: Label[];
location: Location;
manufacturer: string;
modelNumber: string;
name: string;
notes: string;
purchaseFrom: string;
purchasePrice: number;
2022-09-07 05:58:59 +00:00
purchaseTime: Date;
2022-09-03 09:17:57 +00:00
serialNumber: string;
soldNotes: string;
soldPrice: number;
2022-09-07 05:58:59 +00:00
soldTime: Date;
2022-09-03 09:17:57 +00:00
soldTo: string;
updatedAt: string;
}
export class ItemsApi extends BaseAPI {
async getAll() {
2022-09-05 00:55:52 +00:00
return this.http.get<Results<Item>>(route('/items'));
2022-09-03 09:17:57 +00:00
}
async create(item: ItemCreate) {
2022-09-05 00:55:52 +00:00
return this.http.post<ItemCreate, Item>(route('/items'), item);
2022-09-03 09:17:57 +00:00
}
async get(id: string) {
2022-09-07 05:58:59 +00:00
const payload = await this.http.get<Item>(route(`/items/${id}`));
if (!payload.data) {
return payload;
}
// Parse Date Types
payload.data.purchaseTime = new Date(payload.data.purchaseTime);
payload.data.soldTime = new Date(payload.data.soldTime);
return payload;
2022-09-03 09:17:57 +00:00
}
async delete(id: string) {
2022-09-05 00:55:52 +00:00
return this.http.delete<void>(route(`/items/${id}`));
2022-09-03 09:17:57 +00:00
}
async update(id: string, item: ItemCreate) {
2022-09-05 00:55:52 +00:00
return this.http.put<ItemCreate, Item>(route(`/items/${id}`), item);
2022-09-03 09:17:57 +00:00
}
}