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