Adding store now works

master
Eirik Th S 2021-04-14 01:08:25 +02:00
parent 836712868e
commit 1e396f1c51
2 changed files with 120 additions and 8 deletions

View File

@ -29,7 +29,35 @@ if(!empty($data) && isset($user_id)){
returns("Missing store-name value", 1);
}
if( initStore($data['storeName']) ){
if( ($temp = initStore($data['storeName'])) !== false ){
returns($temp);
}
else {
returns($db->error,1);
}
}
if($data['plan'] == 'renameStore'){
if(($temp = checkArgs(array("storeID"=>@$data['storeID'], "newName"=>@$data['newName']))) !== true){
returns("Missing a value: $temp", 1);
}
if( renameStore($data['storeID'], $data['newName']) !== false ){
returns();
}
else {
returns($db->error,1);
}
}
if($data['plan'] == 'deleteStore'){
if(($temp = checkArgs(array("storeID"=>@$data['storeID'], "storeName"=>@$data['storeName'], "itemsLength"=>@$data['itemsLength']))) !== true){
returns("Missing a value: $temp", 1);
}
if( deleteStore($data['storeID'], $data['storeName'], $data['itemsLength']) !== false ){
returns();
}
else {
@ -162,23 +190,55 @@ function initStore($storeName){
$matchingStores = $storeCheckRes->fetch_row()[0];
if($matchingStores == 1){
$sql = "UPDATE plan_store SET null WHERE `user_id` = '$user_id' AND `name` = '$storeName';";
// $sql = "UPDATE plan_store SET null WHERE `user_id` = '$user_id' AND `name` = '$storeName';";
$sql = "SELECT plan_store_id FROM plan_store WHERE `user_id` = '$user_id' AND `name` = '$storeName';";
if( ($res = $db->query($sql)) !== false){
return $res->fetch_assoc()["plan_store_id"];
}
}
else if($matchingStores == 0){
$sql = "INSERT INTO plan_store (user_id, `name`) VALUES ($user_id, '$storeName');";
}
else {
return false;
}
if( $db->query($sql) !== false){
return true;
if( $db->query($sql) !== false){
return $db->insert_id;
}
}
}
return false;
}
function renameStore($storeID, $newName){
global $db, $user_id;
$storeCheckSql = "SELECT count(0) FROM plan_store WHERE `user_id` = '$user_id' AND `name` = '$newName';";
if($storeCheckRes = $db->query($storeCheckSql) && $storeCheckRes->fetch_row()[0] > 0){
return false;
}
$renameStoreSql = "UPDATE plan_store SET `name` = '$newName' WHERE `user_id` = '$user_id' AND `plan_store_id` = '$storeID';";
if($db->query($renameStoreSql) !== false){
return true;
}
}
function deleteStore($storeID, $storeName, $itemsLength){
global $db, $user_id;
$verifyOwnerSql = "SELECT plan_store_id FROM plan_store WHERE `user_id` = '$user_id' AND `plan_store_id` = '$storeID' AND `name` = '$storeName'";
$getItemsSql = "SELECT count(0) FROM plan_store_items WHERE plan_store_id = ($verifyOwnerSql)";
if($getItemsSql = $db->query($getItemsSql) && $getItemsSql->fetch_row()[0] = $itemsLength){
// DELETE QUERIES HERE
return true;
}
return false;
}
function addItem($storeID, $name, $price){
global $db, $user_id;

View File

@ -71,6 +71,12 @@ class Store {
addItem(text, price){
if(text.length > 0){
if(this.storeID === null){
this.getStoreID().done(json => { this.addItem(text, price); });
return;
}
let that = this;
return $.ajax({
method: "POST",
@ -189,6 +195,52 @@ class Store {
removeStore(){
this.selector.remove();
}
getStoreID(){
let that = this;
return $.ajax({
method: "POST",
url: "do.php",
data: { plan: 'saveStore', storeName: this.title },
dataType: 'JSON'
})
.done(json => {
if(handleJsonErrors(json)){
return false;
}
console.log("getStore:", json);
that.storeID = json['data'];
return that.storeID;
})
.fail(handleAjaxErrors);
}
rename(newName){
if(this.storeID !== null){
return $.ajax({
method: "POST",
url: "do.php",
data: { plan: 'renameStore', storeID: this.storeID, newName: newName },
dataType: 'JSON'
})
.done(json => {
if(handleJsonErrors(json)){
return false;
}
return true;
})
.fail(handleAjaxErrors);
}
//TODO:
console.log("No store-id");
}
}