Guest User

Untitled

a guest
Dec 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. require 'sinatra'
  2. require 'dm-core'
  3. require 'date'
  4.  
  5. DataMapper::setup(:default, {:adapter => 'yaml', :path => 'db'})
  6.  
  7. class Apartment
  8. include DataMapper::Resource
  9.  
  10. property :id, Serial
  11. property :address, String, :required => true
  12. property :neighborhood, String
  13. property :pets_allowed, Boolean, :default => false
  14. property :sq_ft, Integer
  15. end
  16.  
  17. DataMapper.finalize
  18.  
  19. ### Routes for main pages
  20. get '/' do
  21. # matches main route for the application, itp.nyu.edu/~irs221/sinatra/apt_listings/
  22.  
  23. # Use these later in the erb file
  24. @availabilities = ''
  25. @total_postings = Apartment.count
  26.  
  27. for entry in Apartment.all
  28. @availabilities += <<-HTML
  29. <p><a href='/~irs221/sinatra/apt_listings/apartment/#{entry.id}'>#{entry.id}. #{entry.address} #{entry.neighborhood}</a></p>
  30. HTML
  31. end
  32.  
  33. # Show the html from views/index.erb
  34. erb :index
  35. end
  36.  
  37. get '/new_post' do
  38. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/new_post
  39.  
  40. # Show the html from views/postlisting.erb
  41. erb :postlisting
  42. end
  43.  
  44. get '/filter_big' do
  45. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/filter_big
  46.  
  47. # Use these later in the erb file
  48. @output = ""
  49. @title = "Filter by size"
  50. big_apartments = Apartment.all(:sq_ft.gte => 800)
  51. @total_postings = big_apartments.count
  52.  
  53. for apt in big_apartments
  54. @output += <<-HTML
  55. <p><a href='/~irs221/sinatra/apt_listings/apartment/#{apt.id}'>#{apt.address}</a>: #{apt.sq_ft} square feet</p>
  56. HTML
  57. end
  58.  
  59. # Show the html from views/default.erb
  60. erb :default
  61.  
  62. end
  63.  
  64. get '/filter_pets' do
  65. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/filter_pets
  66.  
  67. # Use these later in the erb file
  68. @output = ""
  69. @title = "Pets allowed"
  70. pet_apartments = Apartment.all(:pets_allowed => true)
  71. @total_postings = pet_apartments.count
  72.  
  73. for apt in pet_apartments
  74. @output += <<-HTML
  75. <p><a href='/~irs221/sinatra/apt_listings/apartment/#{apt.id}'>#{apt.address}</a></p>
  76. HTML
  77. end
  78.  
  79. # Show the html from views/default.erb
  80. erb :default
  81. end
  82.  
  83. get '/filter_neighborhood' do
  84. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/filter_neighborhood
  85.  
  86. # Use these later in the erb file
  87. @output = ""
  88. @title = "Filter by neighborhood"
  89. loc_apartments = Apartment.all(:order => [:neighborhood.desc])
  90. @total_postings = loc_apartments.count
  91.  
  92. for apt in loc_apartments
  93. @output += <<-HTML
  94. <p><a href='/~irs221/sinatra/apt_listings/apartment/#{apt.id}'>#{apt.address}</a>
  95. <a href='/~irs221/sinatra/apt_listings/neighborhood/#{apt.neighborhood}'>#{apt.neighborhood}</a></p>
  96. HTML
  97. end
  98.  
  99. # Show the html from views/default.erb
  100. erb :default
  101. end
  102.  
  103. get '/neighborhood/:name' do
  104. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/neighborhood/Midtown
  105.  
  106. # Use these later in the erb file
  107. @output = ""
  108. @title = params[:name]
  109. loc_apartments = Apartment.all(:neighborhood => params[:name])
  110.  
  111. for apt in loc_apartments
  112. @output += <<-HTML
  113. <p><a href='/~irs221/sinatra/apt_listings/apartment/#{apt.id}'>#{apt.address}</a></p>
  114. HTML
  115. end
  116.  
  117. # Show the html from views/default.erb
  118. erb :default
  119. end
  120.  
  121. get '/apartment/:id' do
  122. # matches itp.nyu.edu/~irs221/sinatra/apt_listings/apartment/5
  123. apartment = Apartment.get(params[:id])
  124.  
  125. # Use these later in the erb file
  126. @title = apartment.address
  127. @details = <<-HTML
  128. <p>#{apartment.neighborhood}</p>
  129. <p>Pets allowed? #{apartment.pets_allowed}</p>
  130. <p>Square feet: #{apartment.sq_ft}</p>
  131. HTML
  132.  
  133. # Show the html from views/apartment.erb
  134. erb :apartment
  135. end
  136.  
  137. ### Routes for DataMapper actions
  138. post '/save_info' do
  139.  
  140. # Save the information from the form in the DataMapper object
  141. apt = Apartment.new
  142.  
  143. apt.address = params[:address]
  144. apt.neighborhood = params[:neighborhood]
  145.  
  146. if (params[:pets] == "true")
  147. apt.pets_allowed = true;
  148. end
  149.  
  150. apt.sq_ft = Integer(params[:sqft])
  151.  
  152. apt.save
  153.  
  154. # Show the html from views/savedpost.erb
  155. erb :savedpost
  156. end
Add Comment
Please, Sign In to add comment