Skip to content
Snippets Groups Projects
Commit f26d25d1 authored by Lukas Karsch's avatar Lukas Karsch
Browse files

#40 all fetch calls for garden controller

parent 12d88631
No related branches found
No related tags found
1 merge request!28Closes #40 : Custom useGrowbrosFetcher hook to make calls to the backend.
......@@ -70,6 +70,73 @@ export class BackendConnectorImpl implements IBackendConnector {
return result;
}
async addToGarden(plantId: number): Promise<FetchResult<void>> {
let result: FetchResult<void> = {};
try {
await fetch(this.baseUrl + "/garden/add" + plantId);
} catch (e) {
console.error("An error occurred while adding plant with ID", plantId, "to garden");
console.error(e);
result = {err: e};
}
return result;
}
async clearGarden(): Promise<FetchResult<void>> {
let result: FetchResult<void> = {};
try {
await fetch(this.baseUrl + "/garden/remove/all");
} catch (e) {
console.error("An error occurred while clearing garden");
console.error(e);
result = {err: e};
}
return result;
}
async getGardenEntries(sort?: string): Promise<FetchResult<Plant[]>> {
let result: FetchResult<Plant[]> = {};
try {
const params = new URLSearchParams();
if (sort) params.set("sort", sort);
const response = await fetch(this.baseUrl + "/garden" + params);
const json = await response.json();
result = this.parsePlants(json);
} catch (e) {
console.error("An error occurred while getting garden entries")
console.error(e);
result = {...result, err: e};
}
return result;
}
async getGardenSize(): Promise<FetchResult<number>> {
let result: FetchResult<number> = {};
try {
const response = await fetch(this.baseUrl + "/garden/count");
const json = await response.json();
const size = Number.parseInt(json);
result = {value: size};
} catch (e) {
console.error("An error occurred while fetching garden size")
console.error(e);
result = {...result, err: e};
}
return result;
}
async removeEntryFromGarden(entryId: number): Promise<FetchResult<void>> {
let result: FetchResult<void> = {};
try {
await fetch(this.baseUrl + "/garden/remove/" + entryId);
} catch (e) {
console.error("An error occurred while removing garden entry with ID", entryId);
console.error(e);
result = {err: e};
}
return result;
}
parseSinglePlant(json: any): FetchResult<Plant> {
let result: FetchResult<Plant> = {};
try {
......
......@@ -9,4 +9,14 @@ export interface IBackendConnector {
getRandomPlants(amount: number): Promise<FetchResult<Plant[]>>;
getSinglePlant(id: number): Promise<FetchResult<Plant | null>>;
addToGarden(plantId: number): Promise<FetchResult<void>>;
getGardenEntries(sort?: string): Promise<FetchResult<Plant[]>>;
getGardenSize(): Promise<FetchResult<number>>;
removeEntryFromGarden(entryId: number): Promise<FetchResult<void>>;
clearGarden(): Promise<FetchResult<void>>;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment