1
0
Fork 1
mirror of https://github.com/hay-kot/homebox.git synced 2024-12-24 15:56:31 +00:00
homebox/frontend/lib/api/classes/items.ts

76 lines
1.8 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;
2022-09-09 18:20:38 +00:00
lifetimeWarranty: boolean;
warrantyExpires: Date;
warrantyDetails: string;
2022-09-03 09:17:57 +00:00
}
export class ItemsApi extends BaseAPI {
async getAll() {
2022-09-09 06:05:23 +00:00
return this.http.get<Results<Item>>({ url: route('/items') });
2022-09-03 09:17:57 +00:00
}
async create(item: ItemCreate) {
2022-09-09 06:05:23 +00:00
return this.http.post<ItemCreate, Item>({ url: route('/items'), body: item });
2022-09-03 09:17:57 +00:00
}
async get(id: string) {
2022-09-09 06:05:23 +00:00
const payload = await this.http.get<Item>({ url: route(`/items/${id}`) });
2022-09-07 05:58:59 +00:00
if (!payload.data) {
return payload;
}
// Parse Date Types
payload.data.purchaseTime = new Date(payload.data.purchaseTime);
payload.data.soldTime = new Date(payload.data.soldTime);
2022-09-09 18:20:38 +00:00
payload.data.warrantyExpires = new Date(payload.data.warrantyExpires);
2022-09-07 05:58:59 +00:00
return payload;
2022-09-03 09:17:57 +00:00
}
async delete(id: string) {
2022-09-09 06:05:23 +00:00
return this.http.delete<void>({ url: route(`/items/${id}`) });
2022-09-03 09:17:57 +00:00
}
async update(id: string, item: ItemCreate) {
2022-09-09 06:05:23 +00:00
return this.http.put<ItemCreate, Item>({ url: route(`/items/${id}`), body: item });
}
async import(file: File) {
const formData = new FormData();
formData.append('csv', file);
return this.http.post<FormData, void>({ url: route('/items/import'), data: formData });
2022-09-03 09:17:57 +00:00
}
}