1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-07-25 08:09:41 +02:00

feat(frontend): Create CRUD User Interface for Units and Foods

This commit is contained in:
hay-kot 2021-08-22 15:23:45 -08:00
parent 122d35ec09
commit a1aad078da
13 changed files with 517 additions and 42 deletions

View file

@ -10,39 +10,16 @@ export interface CrudAPIInterface {
// Methods
}
export const crudMixins = <T>(
requests: ApiRequestInstance,
baseRoute: string,
itemRoute: (itemId: string) => string
) => {
async function getAll(start = 0, limit = 9999) {
return await requests.get<T[]>(baseRoute, {
params: { start, limit },
});
}
export interface CrudAPIMethodsInterface {
// CRUD Methods
getAll(): any
createOne(): any
getOne(): any
updateOne(): any
patchOne(): any
deleteOne(): any
}
async function createOne(payload: T) {
return await requests.post<T>(baseRoute, payload);
}
async function getOne(itemId: string) {
return await requests.get<T>(itemRoute(itemId));
}
async function updateOne(itemId: string, payload: T) {
return await requests.put<T>(itemRoute(itemId), payload);
}
async function patchOne(itemId: string, payload: T) {
return await requests.patch(itemRoute(itemId), payload);
}
async function deleteOne(itemId: string) {
return await requests.delete<T>(itemRoute(itemId));
}
return { getAll, getOne, updateOne, patchOne, deleteOne, createOne };
};
export abstract class BaseAPI {
requests: ApiRequestInstance;
@ -66,11 +43,11 @@ export abstract class BaseCRUDAPI<T, U> extends BaseAPI implements CrudAPIInterf
return await this.requests.post<T>(this.baseRoute, payload);
}
async getOne(itemId: string) {
async getOne(itemId: string | number) {
return await this.requests.get<T>(this.itemRoute(itemId));
}
async updateOne(itemId: string, payload: T) {
async updateOne(itemId: string | number, payload: T) {
return await this.requests.put<T>(this.itemRoute(itemId), payload);
}

View file

@ -1,4 +1,3 @@
import { requests } from "../requests";
import { BaseCRUDAPI } from "./_base";
export type EventCategory = "general" | "recipe" | "backup" | "scheduled" | "migration" | "group" | "user";
@ -36,7 +35,7 @@ export class NotificationsAPI extends BaseCRUDAPI<EventNotification, CreateEvent
itemRoute = routes.aboutEventsNotificationsId;
/** Returns the Group Data for the Current User
*/
async testNotification(id: number) {
return await requests.post(routes.aboutEventsNotificationsTest, { id });
async testNotification(id: number | null = null, testUrl: string | null = null) {
return await this.requests.post(routes.aboutEventsNotificationsTest, { id, testUrl });
}
}

View file

@ -0,0 +1,22 @@
import { BaseCRUDAPI } from "./_base";
const prefix = "/api";
export interface CreateFood {
name: string;
description: string;
}
export interface Food extends CreateFood {
id: number;
}
const routes = {
food: `${prefix}/foods`,
foodsFood: (tag: string) => `${prefix}/foods/${tag}`,
};
export class FoodAPI extends BaseCRUDAPI<Food, CreateFood> {
baseRoute: string = routes.food;
itemRoute = routes.foodsFood;
}

View file

@ -0,0 +1,23 @@
import { BaseCRUDAPI } from "./_base";
const prefix = "/api";
export interface CreateUnit {
name: string;
abbreviation: string;
description: string;
}
export interface Unit extends CreateUnit {
id: number;
}
const routes = {
unit: `${prefix}/units`,
unitsUnit: (tag: string) => `${prefix}/units/${tag}`,
};
export class UnitAPI extends BaseCRUDAPI<Unit, CreateUnit> {
baseRoute: string = routes.unit;
itemRoute = routes.unitsUnit;
}

View file

@ -9,6 +9,8 @@ import { CategoriesAPI } from "./class-interfaces/categories";
import { TagsAPI } from "./class-interfaces/tags";
import { UtilsAPI } from "./class-interfaces/utils";
import { NotificationsAPI } from "./class-interfaces/event-notifications";
import { FoodAPI } from "./class-interfaces/recipe-foods";
import { UnitAPI } from "./class-interfaces/recipe-units";
import { ApiRequestInstance } from "~/types/api";
class Api {
@ -23,6 +25,8 @@ class Api {
public tags: TagsAPI;
public utils: UtilsAPI;
public notifications: NotificationsAPI;
public foods: FoodAPI;
public units: UnitAPI;
// Utils
public upload: UploadFile;
@ -36,6 +40,8 @@ class Api {
this.recipes = new RecipeAPI(requests);
this.categories = new CategoriesAPI(requests);
this.tags = new TagsAPI(requests);
this.units = new UnitAPI(requests);
this.foods = new FoodAPI(requests);
// Users
this.users = new UserApi(requests);