mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-24 07:39:41 +02:00
feat(frontend): 🚧 CRUD Functionality
This commit is contained in:
parent
00a8fdda41
commit
afcad2f701
49 changed files with 845 additions and 275 deletions
|
@ -1,10 +1,49 @@
|
|||
import { ApiRequestInstance } from "~/types/api";
|
||||
|
||||
export class BaseAPIClass {
|
||||
requests: ApiRequestInstance
|
||||
|
||||
constructor(requests: ApiRequestInstance) {
|
||||
this.requests = requests;
|
||||
}
|
||||
export interface CrudAPIInterface {
|
||||
requests: ApiRequestInstance;
|
||||
|
||||
// Route Properties / Methods
|
||||
baseRoute: string;
|
||||
itemRoute(itemId: string): string;
|
||||
|
||||
// Methods
|
||||
}
|
||||
|
||||
export abstract class BaseAPIClass<T> implements CrudAPIInterface {
|
||||
requests: ApiRequestInstance;
|
||||
|
||||
abstract baseRoute: string;
|
||||
abstract itemRoute(itemId: string): string;
|
||||
|
||||
constructor(requests: ApiRequestInstance) {
|
||||
this.requests = requests;
|
||||
}
|
||||
|
||||
async getAll(start = 0, limit = 9999) {
|
||||
return await this.requests.get<T[]>(this.baseRoute, {
|
||||
params: { start, limit },
|
||||
});
|
||||
}
|
||||
|
||||
async getOne(itemId: string) {
|
||||
return await this.requests.get<T>(this.itemRoute(itemId));
|
||||
}
|
||||
|
||||
async createOne(payload: T) {
|
||||
return await this.requests.post(this.baseRoute, payload);
|
||||
}
|
||||
|
||||
async updateOne(itemId: string, payload: T){
|
||||
return await this.requests.put<T>(this.itemRoute(itemId), payload);
|
||||
}
|
||||
|
||||
async patchOne(itemId: string, payload: T) {
|
||||
return await this.requests.patch(this.itemRoute(itemId), payload);
|
||||
}
|
||||
|
||||
async deleteOne(itemId: string) {
|
||||
return await this.requests.delete<T>(this.itemRoute(itemId));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,30 +11,35 @@ const routes = {
|
|||
recipesCreateUrl: `${prefix}/recipes/create-url`,
|
||||
recipesCreateFromZip: `${prefix}/recipes/create-from-zip`,
|
||||
|
||||
recipesCategory: `${prefix}/recipes/category`,
|
||||
|
||||
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`,
|
||||
};
|
||||
|
||||
class RecipeAPI extends BaseAPIClass {
|
||||
async getAll(start = 0, limit = 9999) {
|
||||
return await this.requests.get<Recipe[]>(routes.recipesSummary, {
|
||||
params: { start, limit },
|
||||
class RecipeAPI extends BaseAPIClass<Recipe> {
|
||||
baseRoute: string = routes.recipesSummary;
|
||||
itemRoute = (itemid: string) => routes.recipesRecipeSlug(itemid);
|
||||
|
||||
|
||||
async getAllByCategory(categories: string[]) {
|
||||
return await this.requests.get<Recipe[]>(routes.recipesCategory, {
|
||||
categories
|
||||
});
|
||||
}
|
||||
|
||||
async getOne(slug: string) {
|
||||
return await this.requests.get<Recipe>(routes.recipesRecipeSlug(slug));
|
||||
}
|
||||
|
||||
// @ts-ignore - Override method doesn't take same arguments are parent class
|
||||
async createOne(name: string) {
|
||||
return await this.requests.post(routes.recipesBase, { name });
|
||||
}
|
||||
|
||||
async createOneByUrl(url: string) {
|
||||
return await this.requests.post(routes.recipesCreateUrl, { url });
|
||||
}
|
||||
|
||||
|
||||
|
||||
// * Methods to Generate reference urls for assets/images *
|
||||
recipeImage(recipeSlug: string, version = null, key = null) {
|
||||
return `/api/media/recipes/${recipeSlug}/images/original.webp?&rnd=${key}&version=${version}`;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue