2021-08-04 05:38:45 +00:00
|
|
|
import { BaseAPIClass, crudMixins } from "./_base";
|
2021-08-02 03:24:47 +00:00
|
|
|
import { Recipe } from "~/types/api-types/admin";
|
2021-08-04 05:38:45 +00:00
|
|
|
import { ApiRequestInstance } from "~/types/api";
|
2021-08-02 03:24:47 +00:00
|
|
|
|
|
|
|
const prefix = "/api";
|
|
|
|
|
|
|
|
const routes = {
|
|
|
|
recipesCreate: `${prefix}/recipes/create`,
|
|
|
|
recipesBase: `${prefix}/recipes`,
|
|
|
|
recipesSummary: `${prefix}/recipes/summary`,
|
|
|
|
recipesTestScrapeUrl: `${prefix}/recipes/test-scrape-url`,
|
|
|
|
recipesCreateUrl: `${prefix}/recipes/create-url`,
|
|
|
|
recipesCreateFromZip: `${prefix}/recipes/create-from-zip`,
|
2021-08-03 06:15:11 +00:00
|
|
|
recipesCategory: `${prefix}/recipes/category`,
|
|
|
|
|
2021-08-02 03:24:47 +00:00
|
|
|
recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`,
|
|
|
|
recipesRecipeSlugZip: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/zip`,
|
|
|
|
recipesRecipeSlugImage: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/image`,
|
|
|
|
recipesRecipeSlugAssets: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/assets`,
|
|
|
|
};
|
|
|
|
|
2021-08-04 05:38:45 +00:00
|
|
|
export class RecipeAPI extends BaseAPIClass<Recipe> {
|
2021-08-03 06:15:11 +00:00
|
|
|
baseRoute: string = routes.recipesSummary;
|
2021-08-04 05:38:45 +00:00
|
|
|
itemRoute = routes.recipesRecipeSlug;
|
|
|
|
|
|
|
|
constructor(requests: ApiRequestInstance) {
|
|
|
|
super(requests);
|
|
|
|
const { getAll, getOne, updateOne, patchOne, deleteOne } = crudMixins<Recipe>(
|
|
|
|
requests,
|
|
|
|
routes.recipesSummary,
|
|
|
|
routes.recipesRecipeSlug
|
|
|
|
);
|
2021-08-03 06:15:11 +00:00
|
|
|
|
2021-08-04 05:38:45 +00:00
|
|
|
this.getAll = getAll;
|
|
|
|
this.getOne = getOne;
|
|
|
|
this.updateOne = updateOne;
|
|
|
|
this.patchOne = patchOne;
|
|
|
|
this.deleteOne = deleteOne;
|
|
|
|
}
|
2021-08-02 03:24:47 +00:00
|
|
|
|
2021-08-03 06:15:11 +00:00
|
|
|
async getAllByCategory(categories: string[]) {
|
|
|
|
return await this.requests.get<Recipe[]>(routes.recipesCategory, {
|
2021-08-04 05:38:45 +00:00
|
|
|
categories,
|
2021-08-03 06:15:11 +00:00
|
|
|
});
|
2021-08-02 03:24:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:38:45 +00:00
|
|
|
updateImage(slug: string, fileObject: File) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("image", fileObject);
|
|
|
|
formData.append("extension", fileObject.name.split(".").pop());
|
|
|
|
|
|
|
|
return this.requests.put<any>(routes.recipesRecipeSlugImage(slug), formData);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateImagebyURL(slug: string, url: string) {
|
|
|
|
return this.requests.post(routes.recipesRecipeSlugImage(slug), { url });
|
|
|
|
}
|
|
|
|
|
2021-08-02 03:24:47 +00:00
|
|
|
async createOne(name: string) {
|
2021-08-04 05:38:45 +00:00
|
|
|
return await this.requests.post<Recipe>(routes.recipesBase, { name });
|
2021-08-02 03:24:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 06:15:11 +00:00
|
|
|
async createOneByUrl(url: string) {
|
|
|
|
return await this.requests.post(routes.recipesCreateUrl, { url });
|
|
|
|
}
|
2021-08-02 03:24:47 +00:00
|
|
|
|
2021-08-03 06:15:11 +00:00
|
|
|
// * Methods to Generate reference urls for assets/images *
|
2021-08-02 03:24:47 +00:00
|
|
|
recipeImage(recipeSlug: string, version = null, key = null) {
|
|
|
|
return `/api/media/recipes/${recipeSlug}/images/original.webp?&rnd=${key}&version=${version}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
recipeSmallImage(recipeSlug: string, version = null, key = null) {
|
|
|
|
return `/api/media/recipes/${recipeSlug}/images/min-original.webp?&rnd=${key}&version=${version}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
recipeTinyImage(recipeSlug: string, version = null, key = null) {
|
|
|
|
return `/api/media/recipes/${recipeSlug}/images/tiny-original.webp?&rnd=${key}&version=${version}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
recipeAssetPath(recipeSlug: string, assetName: string) {
|
|
|
|
return `/api/media/recipes/${recipeSlug}/assets/${assetName}`;
|
|
|
|
}
|
|
|
|
}
|