def handleDeleteRecipe(self, recipeID): db = GroceryDB() if not db.recipeExists(recipeID): self.handle404("Recipe does not exist.") else: db.deleteRecipe(recipeID) self.handle200("Recipe successfully deleted")
def handleGroceryListRetrieve(self, listID): db = GroceryDB() if not db.groceryListExists(listID): self.handle404("Grocery List does not exist.") else: groceryList = db.getGroceryList(listID) self.handle200JSONResponse(groceryList)
def handleDeleteGroceryList(self, listID): db = GroceryDB() if not db.groceryListExists(listID): self.handle404("Grocery list does not exist.") else: db.deleteGroceryList(listID) self.handle200("Grocery list successfully deleted")
def handleIngredientRetrieve(self, ingredientID): db = GroceryDB() if not db.ingredientExists(ingredientID): self.handle404("Ingredient does not exist.") else: ingredient = db.getIngredient(ingredientID) self.handle200JSONResponse(ingredient)
def handleRecipeRetrieve(self, recipeID): db = GroceryDB() if not db.recipeExists(recipeID): self.handle404("Recipe does not exist.") else: recipe = db.getRecipe(recipeID) self.handle200JSONResponse(recipe)
def handleCreateGroceryList(self): parsedBody = self.getParsedBody() db = GroceryDB() label = "Grocery List Label" if parsedBody.get("label") != None: label = parsedBody["label"][0] groceryList = db.createGroceryList(label) self.handle201JSONResponse(groceryList)
def main(): db = GroceryDB() db.createTables() port = 8080 if len(sys.argv) > 1: port = int(sys.argv[1]) listen = ("0.0.0.0", port) server = HTTPServer(listen, RequestHandler) print("Listening...") server.serve_forever()
def handleCreateRecipe(self): parsedBody = self.getParsedBody() db = GroceryDB() label = "Recipe Label" instructions = "" if parsedBody.get("label") != None: label = parsedBody["label"][0] if parsedBody.get("instructions") != None: instructions = parsedBody["instructions"][0] recipeJson = db.createRecipe(label, instructions) self.handle201JSONResponse(recipeJson)
def handleCreateIngredient(self): parsedBody = self.getParsedBody() db = GroceryDB() label = "Ingredient Label" category = "Misc." if parsedBody.get("label") != None: label = parsedBody["label"][0] if parsedBody.get("category") != None: category = parsedBody["category"][0] ingredient = db.createIngredient(label, category) self.handle201JSONResponse(ingredient)
def handleUpdateIngredient(self, ingredientID): parsedBody = self.getParsedBody() db = GroceryDB() ingredient = db.getIngredient(ingredientID) label = ingredient["label"] category = ingredient["category"] if parsedBody.get("label") != None: label = parsedBody["label"][0] if parsedBody.get("category") != None: category = parsedBody["category"][0] db.updateIngredient(label, category, ingredientID) self.handle201("Ingredient updated.")
def handleAddRecipeIngredient(self): db = GroceryDB() parsedBody = self.getParsedBody() ingredientID = -1 recipeID = -1 if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("recipe_id") != None: recipeID = parsedBody["recipe_id"][0] if not db.ingredientExists(ingredientID) or not db.recipeExists( recipeID): self.handle404("Ingredient or recipe does not exist.") elif db.recipeIngredientExists(recipeID, ingredientID): self.handle422("Recipe ingredient already exists.") else: quantity = "1" quantityType = "" if parsedBody.get("quantity") != None: tempQuantity = parsedBody["quantity"][0] if isValidQuantityString(tempQuantity): quantity = parseQuantityString(tempQuantity) if parsedBody.get("quantity_type") != None: quantityType = parsedBody["quantity_type"][0] db.addIngredientToRecipe(recipeID, ingredientID, quantity, quantityType) self.handle201("Recipe ingredient added.")
def handleDeleteRecipeIngredient(self, queryString): db = GroceryDB() parsedQs = parse_qs(queryString) parsedBody = self.getParsedBody() ingredientID = -1 recipeID = -1 if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("recipe_id") != None: recipeID = parsedBody["recipe_id"][0] if parsedQs.get("ingredient_id") != None: ingredientID = parsedQs["ingredient_id"][0] if parsedQs.get("recipe_id") != None: recipeID = parsedQs["recipe_id"][0] if not db.recipeIngredientExists(recipeID, ingredientID): print(recipeID, ingredientID) self.handle404("Recipe ingredient does not exist.") else: db.deleteRecipeIngredient(recipeID, ingredientID) self.handle200("Recipe ingredient successfully deleted")
def handleDeleteGroceryListItem(self, queryString): db = GroceryDB() parsedQs = parse_qs(queryString) parsedBody = self.getParsedBody() ingredientID = -1 listID = -1 quantityType = "" if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("list_id") != None: listID = parsedBody["list_id"][0] if parsedBody.get("quantity_type") != None: quantityType = parsedBody["quantity_type"][0] if parsedQs.get("ingredient_id") != None: ingredientID = parsedQs["ingredient_id"][0] if parsedQs.get("list_id") != None: listID = parsedQs["list_id"][0] if parsedQs.get("quantity_type") != None: quantityType = parsedQs["quantity_type"][0] if not db.groceryListItemExists(listID, ingredientID, quantityType): self.handle404("Grocery list item does not exist.") else: db.deleteGroceryListItem(listID, ingredientID, quantityType) self.handle200("Grocery list item successfully deleted.")
def handleAddGroceryListItem(self): db = GroceryDB() parsedBody = self.getParsedBody() ingredientID = -1 listID = -1 if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("list_id") != None: listID = parsedBody["list_id"][0] if not db.ingredientExists(ingredientID) or not db.groceryListExists( listID): self.handle404("Ingredient or grocery list does not exist.") else: quantity = "1" quantityType = "" if parsedBody.get("quantity") != None: tempQuantity = parsedBody["quantity"][0] if isValidQuantityString(tempQuantity): quantity = parseQuantityString(tempQuantity) if parsedBody.get("quantity_type") != None: quantityType = parsedBody["quantity_type"][0] itemJsonString = db.addItemToGroceryList(listID, ingredientID, quantity, quantityType) self.handle201JSONResponse(itemJsonString)
def handleDeleteIngredient(self, ingredientID): db = GroceryDB() if not db.ingredientExists(ingredientID): self.handle404("Ingredient does not exist.") elif db.ingredientReferencedByOtherTables(ingredientID): self.handle204( "Ingredient is referenced by other dependent tables") else: db.deleteIngredient(ingredientID) self.handle200("Ingredient successfully deleted")
def handleAddRecipeToGroceryList(self): db = GroceryDB() parsedBody = self.getParsedBody() recipeID = -1 listID = -1 if parsedBody.get("recipe_id") != None: recipeID = parsedBody["recipe_id"][0] if parsedBody.get("list_id") != None: listID = parsedBody["list_id"][0] if not db.recipeExists(recipeID) or not db.groceryListExists(listID): self.handle404("Recipe or grocery list does not exist.") else: ingredients = db.getRecipeIngredients(recipeID) db.addRecipeItemsToGroceryList(listID, ingredients) self.handle201("Recipe added to grocery list")
def handleUpdateGroceryList(self, listID): parsedBody = self.getParsedBody() db = GroceryDB() if not db.groceryListExists(listID): self.handle404("Grocery list does not exist.") else: groceryList = db.getGroceryList(listID) label = groceryList["label"] if parsedBody.get("label") != None: label = parsedBody["label"][0].strip() db.updateGroceryList(listID, label) self.handle201("Grocery list updated.")
def handleUpdateRecipe(self, recipeID): parsedBody = self.getParsedBody() db = GroceryDB() if not db.recipeExists(recipeID): self.handle404("Recipe does not exist.") else: recipe = db.getRecipe(recipeID) label = recipe["label"] instructions = recipe["instructions"] if parsedBody.get("label") != None: label = parsedBody["label"][0].strip() if parsedBody.get("instructions") != None: instructions = parsedBody["instructions"][0].strip() db.updateRecipe(recipeID, label, instructions) self.handle201("Recipe updated.")
def handleUpdateGroceryListItem(self): db = GroceryDB() parsedBody = self.getParsedBody() ingredientID = -1 listID = -1 originalQuantityType = "" if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("list_id") != None: listID = parsedBody["list_id"][0] if parsedBody.get("original_quantity_type") != None: originalQuantityType = parsedBody["original_quantity_type"][0] if not db.groceryListItemExists(listID, ingredientID, originalQuantityType): self.handle404("Grocery list item does not exist.") else: groceryListItem = db.getGroceryListItem(listID, ingredientID, originalQuantityType) quantity = groceryListItem["quantity"] newQuantityType = groceryListItem["quantity_type"] grabbed = groceryListItem["grabbed"] quantityUpdate = parsedBody.get("quantity") != None quantityTypeUpdate = parsedBody.get("new_quantity_type") != None if quantityUpdate: tempQuantity = parsedBody["quantity"][0] if isValidQuantityString(tempQuantity): quantity = parseQuantityString(tempQuantity) if quantityTypeUpdate: newQuantityType = parsedBody["new_quantity_type"][0].strip() if quantityUpdate or quantityTypeUpdate: item = db.updateGroceryListItem(listID, ingredientID, quantity, originalQuantityType, newQuantityType) self.handle201JSONResponse(item) if parsedBody.get("grabbed") != None: grabbed = parsedBody["grabbed"][0] db.setGroceryListItemGrabbed(grabbed, listID, ingredientID, originalQuantityType) self.handle201("Grocery list item grabbed set.")
def handleUpdateRecipeIngredient(self): db = GroceryDB() parsedBody = self.getParsedBody() ingredientID = -1 recipeID = -1 if parsedBody.get("ingredient_id") != None: ingredientID = parsedBody["ingredient_id"][0] if parsedBody.get("recipe_id") != None: recipeID = parsedBody["recipe_id"][0] if not db.recipeIngredientExists(recipeID, ingredientID): self.handle404("Recipe ingredient does not exist.") else: recipeIngredient = db.getRecipeIngredient(recipeID, ingredientID) quantity = recipeIngredient["quantity"] quantityType = recipeIngredient["quantity_type"] if parsedBody.get("quantity") != None: tempQuantity = parsedBody["quantity"][0] if isValidQuantityString(tempQuantity): quantity = parseQuantityString(tempQuantity) if parsedBody.get("quantity_type") != None: quantityType = parsedBody["quantity_type"][0].strip() db.updateRecipeIngredient(recipeID, ingredientID, quantity, quantityType) self.handle201("Recipe ingredient updated.")
def handleListGroceryLists(self): db = GroceryDB() groceryLists = {"grocery_lists": db.getGroceryLists()} self.handle200JSONResponse(groceryLists)
def handleListIngredients(self): db = GroceryDB() ingredients = {"ingredients": db.getIngredients()} self.handle200JSONResponse(ingredients)
def handleListRecipes(self): db = GroceryDB() recipes = {"recipes": db.getRecipes()} self.handle200JSONResponse(recipes)