Skip to content

Commit

Permalink
Merge pull request openshift#19 from bparees/add_crud
Browse files Browse the repository at this point in the history
update sample app to do crud ops
  • Loading branch information
bparees committed Nov 3, 2014
2 parents 19b0cff + a2a73db commit 0ac5f86
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 19 deletions.
43 changes: 37 additions & 6 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,41 @@
end

get '/' do
Timestamp.create(date: Time.now, text: "This is a message from a database query. The last insertion in the database was at")
"Hello World!\n"+
# ENV values are generated during template processing
# and then passed to the container when openshift launches it.
"All the environment variables are: #{ENV.map { |k,v| "#{k}=#{v}" }.join("\n")}]\n" +
"#{Timestamp.last().text} #{Timestamp.last().date}."
File.read('main.html')
end

get '/keys' do
a={}
KeyPair.all.each do |v|
a[v.key]=v.value
end
a.to_s
end

get '/keys/:id' do
if KeyPair.exists?(params[:id])
KeyPair.find(params[:id]).value
else
not_found "Key not found"
end
end

post '/keys/:id' do
if KeyPair.exists?(params[:id])
KeyPair.update(params['id'], value: params['value'])
"Key updated"
else
KeyPair.create(key:params[:id],value:params['value']).save
"Key created"
end
end

delete '/keys/:id' do
if KeyPair.exists?(params[:id])
v=KeyPair.find(params[:id])
v.destroy
"Key deleted"
else
"Key not found"
end
end
12 changes: 0 additions & 12 deletions db/migrate/1414364400_create_timestamp.rb

This file was deleted.

12 changes: 12 additions & 0 deletions db/migrate/20141102191902_create_key_pair.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateKeyPair < ActiveRecord::Migration
def up
create_table :key_pairs, :primary_key => :key do |t|
t.string :value
end
change_column :key_pairs, :key, :string
end

def down
drop_table :keypairs
end
end
58 changes: 58 additions & 0 deletions main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<html>
<head>
<title>Hello from OpenShift v3!</title>
</head>
<body>
Welcome to an OpenShift v3 Demo App!


<form name="myForm" onSubmit="return handleSubmit()">
Key:
<input type="text" id="key" name="Key"/>
<input type="submit" id="get" name="action" value="Get"/>
<input type="submit" id="put" name="action"value="Put"/>
<input type="submit" id="delete" name="action" value="Delete"/>
</form>
Value:
<input type="text" id="value" name="value" value="">
</body>

<script type="text/javascript">
function handleSubmit()
{


//document.getElementById("value").value="updated";
return false;
}

document.getElementById('get').onclick = function() {
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://"+window.location.hostname+":"+window.location.port+"/keys/"+document.getElementById("key").value, false );
xmlHttp.send( null );
document.getElementById("value").value=xmlHttp.responseText;
}

document.getElementById('put').onclick = function() {
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "POST", "http://"+window.location.hostname+":"+window.location.port+"/keys/"+document.getElementById("key").value, false );
var params = "value="+document.getElementById("value").value;

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", params.length);
xmlHttp.setRequestHeader("Connection", "close");

xmlHttp.send( params );
document.getElementById("value").value=xmlHttp.responseText;
}

document.getElementById('delete').onclick = function() {
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "DELETE", "http://"+window.location.hostname+":"+window.location.port+"/keys/"+document.getElementById("key").value, false );
xmlHttp.send( null );
document.getElementById("value").value="Key deleted";
}

</script>

</html>
3 changes: 2 additions & 1 deletion models.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'active_record'

class Timestamp < ActiveRecord::Base
class KeyPair < ActiveRecord::Base
self.primary_key='key'
end

0 comments on commit 0ac5f86

Please sign in to comment.