108 lines
3.3 KiB
TypeScript
108 lines
3.3 KiB
TypeScript
import axios from "axios";
|
|
import type { CreateRecipeRequest, CreateRecipeResponse, DeleteRecipeResponse, EditRecipeRequest, GetRecipeOfTheWeekResponse, GetRecipeResponse, IsRecipeOwnerResponse, SearchRecipesResponse, EditRecipeResponse } from "../types/api/recipe";
|
|
import type { Recipe } from "../types/recipe";
|
|
import type { ApiError } from "../types/api/error";
|
|
import type { SearchFilters } from "../types/search";
|
|
import { GetBackendUrl } from "./environment";
|
|
|
|
const BACKEND_URL = GetBackendUrl();
|
|
|
|
|
|
export async function GetRecipeOfTheWeek(): Promise<Recipe | ApiError> {
|
|
const response = await axios.get<GetRecipeOfTheWeekResponse>(`${BACKEND_URL}/v2/api/recipe/of-the-week`);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function GetRecipe(id: number): Promise<Recipe | ApiError> {
|
|
const response = await axios.get<GetRecipeResponse>(`${BACKEND_URL}/v2/api/recipe/${id}`);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function SearchRecipes(filters: SearchFilters): Promise<Recipe[] | ApiError> {
|
|
const response = await axios.post<SearchRecipesResponse>(`${BACKEND_URL}/v2/api/recipe/search`, filters);
|
|
|
|
if (response.status !== 200 || response.data.recipes === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipes;
|
|
}
|
|
|
|
export async function CreateRecipe(data: CreateRecipeRequest): Promise<Recipe | ApiError> {
|
|
const response = await axios.post<CreateRecipeResponse>(`${BACKEND_URL}/v2/api/recipe`, data);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function EditRecipe(data: EditRecipeRequest): Promise<Recipe | ApiError> {
|
|
const response = await axios.put<EditRecipeResponse>(`${BACKEND_URL}/v2/api/recipe/${data.Id}`, data);
|
|
|
|
if (response.status !== 200 || response.data.recipe === undefined) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.recipe;
|
|
}
|
|
|
|
export async function DeleteRecipe(id: number): Promise<ApiError | null> {
|
|
const response = await axios.delete<DeleteRecipeResponse>(`${BACKEND_URL}/v2/api/recipe/${id}`);
|
|
|
|
if (response.status !== 200) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
export async function IsRecipeOwner(recipeId: number): Promise<boolean | ApiError> {
|
|
const response = await axios.get<IsRecipeOwnerResponse>(`${BACKEND_URL}/v2/api/recipe/${recipeId}/is-owner`);
|
|
|
|
if (response.status !== 200) {
|
|
const err: ApiError = {
|
|
status: response.data.status,
|
|
message: response.data.message
|
|
};
|
|
return err;
|
|
}
|
|
|
|
return response.data.owner;
|
|
}
|