Source

revSpark / demo / wsdemo / products.logic

Full commit
<?rev

/*******************************************************
* 
*  Code for table "products"
*
*  Fields: title, website, price, description, id
*  Primary Key: id
*
*  URL: http://wecode.org/revspark/demo/service/products
*
*******************************************************/


/*
*
* ~~ CREATE A NEW RECORD ~~
*
* This matches POST requests to /products
* It will pick the record data from the POST body.
* it should be an array that is arrayencoded into a string
* and then base64encoded.
*
*/
if match_post("/products") then
  put recordFromPost() into tRecordA
end if

/*
*
* ~~ UPDATE A RECORD ~~
*
* This matches POST requests to:
* 		/update/products/<field>/<value>		-		This will update all records where <field> = <value>
*			/update/products/<primaryKeyValue>	-		This will update a record with the given primary key
*
* It will pick the record data from the POST body.
* it should be an array that is arrayencoded into a string
* and then base64encoded.
*
*/
if match_post("/update/products/:field/:value") then
    put recordFromPost() into tRecordA
end if

if match_post("/update/products/:primaryKeyValue") then
	
end if

/*
*
* ~~ RETRIEVE RECORDS ~~
*
* This will match GET resquests to:
*		/products/<primaryKeyValue>		-		Returns the record that the primary key matches primaryKeyValue
*		/products/<field>/<value>			-		Returns one or more records that matches field = value
*
* Both return an Rev Array thats been array encoded as a string and then base64 encoded. To use the response from those URLs do like
* this example below:
* 
* get URL "http://wecode.org/revspark/demo/service/products/3"
* if word 1 of it is not "Error" then
*  	put it into tRecordA
*		put the arrayDecode of the base64decode of tRecordA into tRecordA
* end if
*
*/
if match_get("/products/:primaryKey") then
  
end if

if match_get("/products/:field/:value") then

end if


/*
* This will match DELETE requests such as: /products/title/<value>
* Be aware that this has potential to delete more than one record at a given time.
*
* The best way to delete a record from table products is to send a DELETE request using the primary key
* as the field and the primary key as the value, like: /products/id/<value>. As a shortcut
* You can simply send a DELETE request to /products/<primary key value> and that specific record will be deleted.
* 
*/
if match_delete("/products/:field/:value") then
  put para("field") into tField
  put para("value") into tValue

  put format("DELETE FROM products WHERE %s = :1", tField) into tSQL

  revExecuteSQL _DB["id"], tSQL, "tValue"
  if the result is not a number then
     throw the result
  else
    if the result < 2 then
      put the result && "record deleted."
    else
      put the result && "records deleted."
    end if
  end if
  quit
end if

if match_delete("/products/:primaryKeyValue") then
  put "id" into tField
  put para("primaryKeyValue") into tValue

  put format("DELETE FROM products WHERE %s = :1", tField) into tSQL

  revExecuteSQL _DB["id"], tSQL, "tValue"
  if the result is not a number then
     throw the result
  else
      put the result && "record deleted."
  end if
  quit
end if