From ec170f9b9383049fcc24159ecb5ada91a7e7d9c4 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 30 Aug 2022 21:22:10 -0800 Subject: [PATCH] location API --- frontend/src/api/base/base-api.ts | 8 ++++---- frontend/src/api/base/base-types.ts | 3 +++ frontend/src/api/classes/locations.ts | 24 ++++++++++++++++++++++++ frontend/src/api/user.ts | 12 ++++++++++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 frontend/src/api/base/base-types.ts create mode 100644 frontend/src/api/classes/locations.ts diff --git a/frontend/src/api/base/base-api.ts b/frontend/src/api/base/base-api.ts index 785cb82..e05a94d 100644 --- a/frontend/src/api/base/base-api.ts +++ b/frontend/src/api/base/base-api.ts @@ -9,9 +9,9 @@ import { Requests } from '../../lib/requests'; // > export class BaseAPI { - http: Requests; + http: Requests; - constructor(requests: Requests) { - this.http = requests; - } + constructor(requests: Requests) { + this.http = requests; + } } diff --git a/frontend/src/api/base/base-types.ts b/frontend/src/api/base/base-types.ts new file mode 100644 index 0000000..95a3b65 --- /dev/null +++ b/frontend/src/api/base/base-types.ts @@ -0,0 +1,3 @@ +export type Results = { + items: T[]; +}; diff --git a/frontend/src/api/classes/locations.ts b/frontend/src/api/classes/locations.ts new file mode 100644 index 0000000..17ad9de --- /dev/null +++ b/frontend/src/api/classes/locations.ts @@ -0,0 +1,24 @@ +import { BaseAPI, UrlBuilder } from '../base'; +import { type Results } from '../base/base-types'; + +export type LocationCreate = { + name: string; + description: string; +}; + +export type Location = LocationCreate & { + id: string; + groupId: string; + createdAt: string; + updatedAt: string; +}; + +export class LocationsApi extends BaseAPI { + async getAll() { + return this.http.get>(UrlBuilder('/locations')); + } + + async create(location: LocationCreate) { + return this.http.post(UrlBuilder('/locations'), location); + } +} diff --git a/frontend/src/api/user.ts b/frontend/src/api/user.ts index fb93a07..1bd0f2a 100644 --- a/frontend/src/api/user.ts +++ b/frontend/src/api/user.ts @@ -1,4 +1,6 @@ +import { Requests } from '@/lib/requests'; import { BaseAPI, UrlBuilder } from './base'; +import { LocationsApi } from './classes/locations'; export type Result = { item: T; @@ -12,6 +14,16 @@ export type User = { }; export class UserApi extends BaseAPI { + locations: LocationsApi; + + constructor(requests: Requests) { + super(requests); + + this.locations = new LocationsApi(requests); + + Object.freeze(this); + } + public self() { return this.http.get>(UrlBuilder('/users/self')); }