2022-01-09 07:24:34 +00:00
|
|
|
import { BaseCRUDAPI } from "../_base";
|
|
|
|
import { ApiRequestInstance } from "~/types/api";
|
2022-01-17 00:24:24 +00:00
|
|
|
import {
|
|
|
|
ShoppingListCreate,
|
|
|
|
ShoppingListItemCreate,
|
|
|
|
ShoppingListItemOut,
|
|
|
|
ShoppingListOut,
|
|
|
|
} from "~/types/api-types/group";
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
const prefix = "/api";
|
|
|
|
|
|
|
|
const routes = {
|
|
|
|
shoppingLists: `${prefix}/groups/shopping/lists`,
|
|
|
|
shoppingListsId: (id: string) => `${prefix}/groups/shopping/lists/${id}`,
|
2022-02-13 21:23:42 +00:00
|
|
|
shoppingListIdAddRecipe: (id: string, recipeId: string) => `${prefix}/groups/shopping/lists/${id}/recipe/${recipeId}`,
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
shoppingListItems: `${prefix}/groups/shopping/items`,
|
|
|
|
shoppingListItemsId: (id: string) => `${prefix}/groups/shopping/items/${id}`,
|
2022-01-09 07:24:34 +00:00
|
|
|
};
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
export class ShoppingListsApi extends BaseCRUDAPI<ShoppingListOut, ShoppingListCreate> {
|
|
|
|
baseRoute = routes.shoppingLists;
|
|
|
|
itemRoute = routes.shoppingListsId;
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-02-13 21:23:42 +00:00
|
|
|
async addRecipe(itemId: string, recipeId: string) {
|
2022-01-17 00:24:24 +00:00
|
|
|
return await this.requests.post(routes.shoppingListIdAddRecipe(itemId, recipeId), {});
|
|
|
|
}
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-02-13 21:23:42 +00:00
|
|
|
async removeRecipe(itemId: string, recipeId: string) {
|
2022-01-17 00:24:24 +00:00
|
|
|
return await this.requests.delete(routes.shoppingListIdAddRecipe(itemId, recipeId));
|
|
|
|
}
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
export class ShoppingListItemsApi extends BaseCRUDAPI<ShoppingListItemOut, ShoppingListItemCreate> {
|
|
|
|
baseRoute = routes.shoppingListItems;
|
|
|
|
itemRoute = routes.shoppingListItemsId;
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
async updateMany(items: ShoppingListItemOut[]) {
|
|
|
|
return await this.requests.put(routes.shoppingListItems, items);
|
|
|
|
}
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
async deleteMany(items: ShoppingListItemOut[]) {
|
|
|
|
let query = "?";
|
|
|
|
|
|
|
|
items.forEach((item) => {
|
|
|
|
query += `ids=${item.id}&`;
|
|
|
|
});
|
|
|
|
|
|
|
|
return await this.requests.delete(routes.shoppingListItems + query);
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ShoppingApi {
|
|
|
|
public lists: ShoppingListsApi;
|
2022-01-17 00:24:24 +00:00
|
|
|
public items: ShoppingListItemsApi;
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
constructor(requests: ApiRequestInstance) {
|
|
|
|
this.lists = new ShoppingListsApi(requests);
|
2022-01-17 00:24:24 +00:00
|
|
|
this.items = new ShoppingListItemsApi(requests);
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
}
|