diff --git a/www/api/v1/api-init.php b/www/api/v1/api-init.php index e188fa4..4a2e99a 100644 --- a/www/api/v1/api-init.php +++ b/www/api/v1/api-init.php @@ -22,6 +22,28 @@ function returns($content = 'Success', $code = 0){ die(); } +function returnsErr($alternate = ''){ + global $db; + if($error = $db->error){ + returns("Database error: ".$error, 1); + } + if($alternate != ''){ + returns($alternate, 2); + } + + returns("Error", 2); +} + +function checkArgs($args){ + foreach($args as $key => $arg){ + if($arg == ""){ + return $key; + } + } + return true; +} + + function sanitize(): array{ global $_GET, $_POST; $data = array(); @@ -29,6 +51,7 @@ function sanitize(): array{ foreach([$_GET, $_POST] as $request){ if(!empty($request)){ foreach($request as $key => $value){ + if(in_array($value, ['string', 'integer', 'number', 'boolean'])){ $value = ''; } // API DEFAULTS CLEAN if(($data[$key] = filter($value)) === false){ print_r($value); echo "Failed to sanitize: `".$key."`: ".$value." \t-\t type: ".gettype($value)."\n"; diff --git a/www/api/v1/docs.php b/www/api/v1/docs.php index 678400d..55b9ec3 100644 --- a/www/api/v1/docs.php +++ b/www/api/v1/docs.php @@ -7,6 +7,9 @@ error_reporting(E_ALL); $docs['general'][] = array("method"=>'GET', "href"=>"/", "name"=>"Nothing", "body"=>"{}"); $docs['recipe'][] = array("method"=>'GET', "href"=>"/recipe", "name"=>"Get all available recipes", "body"=>"{}"); $docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Create recipe", "body"=>"{\n \"name\": \"string\",\n \"portions\": \"integer\",\n \"public\": \"boolean\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Add item to recipe", "body"=>"{\n \"recipe_id\": \"integer\",\n \"name\": \"string\",\n \"price\": \"number\",\n \"amount\": \"integer\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Edit recipe item", "body"=>"{\n \"recipe_id\": \"integer\",\n \"recipe_item_id\": \"integer\",\n \"newName\": \"string\",\n \"newPrice\": \"number\",\n \"newAmount\": \"integer\",\n \"newItem_id\": \"integer\" \n}"); +$docs['recipe'][] = array("method"=>'POST', "href"=>"/recipe", "name"=>"Delete recipe item", "body"=>"{\n \"recipe_id\": \"integer\",\n \"del_item_id\": \"integer\",\n \"delName\": \"string\" \n}"); @@ -61,7 +64,7 @@ function capitalizeFirst($input){ ?>

- "+ + "

"+ + "
"+ + "
"+ + "
    "; + + for(const itemKey in recipe.items){ + const recipeItem = recipe.items[itemKey]; + html += "
  • "+ + "
    "+ + ""+recipeItem.amount+" "+ + ""+recipeItem.name+" "+ + ""+recipeItem.price+" "; + if(recipe.owner) { + html += " "; + } + html += "
    "+ + "
  • "; + } + + html += "
"+ + "
"+ + " "+ + " "+ + " "+ + "
"+ + " "; + if(recipe.owner){ + html += " "; + } + html += "
"+ + "
"; + + let htmlElem = $(html); + + // MODIFY PORTIONS + htmlElem.find('.portionAmountBtns button, .portionAmountBtns input').on('click', portionEv=>{ + let clickedElem = $(portionEv.currentTarget); + let inputElem = clickedElem.parent().find('input'); + let amountNum = Number(inputElem.val()); + switch (clickedElem.attr('data-type')){ + case 'descend': + if(amountNum > 1){ + amountNum--; + } + break; + case 'ascend': + if(amountNum < 99){ + amountNum++; + } + break; + } + this.portionAmount = amountNum; + + inputElem.val(amountNum); + inputElem.trigger('change'); + }); + htmlElem.find('.portionAmountBtns input').on('change', portionChangeEv => { + let amountModifier = Number($(portionChangeEv.currentTarget).val()); + $("#recipeItems"+key+" li .number").each((k, portionVal)=>{ + let elem = $(portionVal); + elem.html( Number(elem.attr('data-amount'))*amountModifier ); + }); + }); + + // SAVE AS A STORE + htmlElem.find('.addStoreContents').one('click', addStoreEv => { + let recipeList = $(addStoreEv.currentTarget).parent().find('ul'); + let recipeItems = []; + recipeList.find("li").each((k, recipeItem)=> { + let elem = $(recipeItem); + recipeItems.push([elem.find('.itemName').html(), elem.find('.price').html(), elem.find('.recipeItemAmount').html()]); + }); + + this.saveToStore(recipeList.attr('data-recipe-name'), recipeItems); + addStoreModal.hide(); + }); + + // TODO: EDIT RECIPE + htmlElem.find('.editList').css('cursor','pointer').one('click', editListEv => { + let accBody = $(editListEv.currentTarget).parent(); + + accBody.find('li .editing').show(); + }); + + htmlElem.find(".ariaButton").off('keydown').on('keydown', function(e){ + if(e.code === "Space" || e.code === "Enter"){ + e.preventDefault(); + $(this).trigger('click'); + } + }); + + htmlElem.find('.editRow').on('click', ev=>{this.editRow(ev);}); + htmlElem.find('.delRow').on('click', ev=>{this.editRow(ev, true);}); + + htmlElem.appendTo(appendElem); + } + + } + + editRow(event, doDelete){ + doDelete = doDelete || false; + let eventElem = $(event.currentTarget); + let recipe_id = eventElem.parent().parent().parent().parent().attr('data-recipe-id'); + let r_item_id = eventElem.parent().attr('data-itemid'); + let item_name = eventElem.parent().parent().find('.itemName').html(); + + if(doDelete){ + ajaxPost('/api/v1/recipe', { recipe_id: recipe_id, del_item_num: r_item_id, delName: item_name }).done(resp => { + // eventElem.remove(); + eventElem.parent().parent().parent().hide(); + }); + } + else { + alert("Edit coming soon..."+"\nrecipeid: "+recipe_id+"\nitem_num: "+item_num); + } + } + + saveToStore(storeName, recipeItems){ + if(typeof Store !== "undefined" && typeof stores !== "undefined"){ + if(!storeName){ + storeName = 'Recipe'; + } + else { + storeName = 'Recipe: '+storeName; + } + + stores.push( new Store(storeName)); + let storeKey = stores.length - 1; + return stores[storeKey].getStoreID().done(json => { + for (const recKey in recipeItems) { + const recItem = recipeItems[recKey]; + stores[storeKey].addItem(recItem[0], recItem[1], recItem[2]); + } + }); + } + console.error("Store class not initialized, or no stores-list found."); + return false; + } +} + +function ajaxPost(url, params){ + return $.post(url, params) + .done(resp => { + if(resp.status !== 0){ + alert(resp.message); + return; + } + }); +} \ No newline at end of file