Skip to content

Instantly share code, notes, and snippets.

@miguelbermudez
Created June 4, 2012 20:16
Show Gist options
  • Save miguelbermudez/2870607 to your computer and use it in GitHub Desktop.
Save miguelbermudez/2870607 to your computer and use it in GitHub Desktop.
Simple Sinatra App for Reading a SQLite3 DB and outputting JSON
require 'rubygems'
require 'sinatra'
require 'sinatra/json'
require 'data_mapper'
require 'dm-serializer'
require 'haml'
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/george.db")
class Painting
include DataMapper::Resource
property :id, Serial
property :artist, String, :length => 255
property :title, String, :length => 255
property :year, Integer
property :description, Text
property :imagefilename, String, :length => 255
property :room_gallery, String
property :link_count, Integer, :default => 0
property :note_count, Integer, :default => 0
property :exb_count, Integer, :default => 0
has n, :links
has n, :notes
has n, :exhibitions
has n, :themes, :through => Resource
def update_counts
puts "Updating link, note & exhibition counts for Paiting id:#{self.id}..."
self.update(:link_count => self.links.count)
self.update(:note_count => self.notes.count)
self.update(:exb_count => self.exhibitions.count)
end
def link_to(painting)
link = self.links.create(:target => painting.title, :target_id => painting.id)
#create link
#link = Link.create()
#link.target = painting.title
#link.target_id = painting.id
#add link to painting
#self.links << link
end
def add_note(note_str)
#create note
noteObj = Note.create()
noteObj.content = note_str
#add note to painting
self.notes << noteObj
self.note_count+=1
end
def add_exhibition(exb_str)
#create exhibition
exbObj = Exhibition.create()
exbObj.content = exb_str
#add exhibition to painting
self.exhibitions << exbObj
self.exb_count+=1
end
end
class Link
include DataMapper::Resource
property :id, Serial
property :target, String, :length => 255 , :required => true #linked painting_title
property :target_id, Integer #linked painting_id
belongs_to :painting
end
class Note
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :painting
end
class Exhibition
include DataMapper::Resource
property :id, Serial
property :content, Text
belongs_to :painting
end
class Theme
include DataMapper::Resource
property :id, Serial
property :keyword, String, :required => true, :unique => true
has n, :paintings, :through => Resource
end
def add_theme_to(paintingid, themeid)
p1 = Painting.get(paintingid)
t1 = Theme.get(themeid)
puts "\tAdding theme: #{t1.keyword} to #{p1.id} : #{p1.title}"
p1.themes << t1 unless p1.themes.include? t1
p1.save
end
#list all paintings
get '/paintings' do
@paintings = Painting.all
haml :index
end
get '/painting/:id.json' do
content_type :txt
@painting = Painting.get(params[:id])
@painting_json = @painting.to_json
@painting_json
end
#show painting
get '/painting/:id' do
@painting = Painting.get(params[:id])
haml :show
end
#test json
get '/testjson' do
json :foo => 'bar'
end
DataMapper.auto_upgrade!
%h3 Paintings
%ul#paintings
- @paintings.each do |painting|
%li{:id => "painting-#{painting.id}"}
= painting.title
%a.small(href="/painting/#{painting.id}")show
!!! 5
%html
%head
%meta(charset="utf-8")
%title Paintings
%body
= yield
%h3= @painting.title
%p Artist: #{@painting.artist}
%p Year: #{@painting.year}
%p Room: #{@painting.room_gallery}
%p Desc: #{@painting.description}
%a(href='/paintings') Back to index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment