-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rb
More file actions
53 lines (39 loc) · 1.11 KB
/
server.rb
File metadata and controls
53 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'sinatra'
require 'mongoid'
require 'sinatra/namespace'
Mongoid.configure do |config|
config.clients.default = {
database: 'booklist_dev',
hosts: ['localhost:27017'],
}
end
# Mongoid.load!("Users/jack.sutton/booklist/mogoid.yml")
class Book
include Mongoid::Document
field :title, type: String
field :author, type: String
field :isbn, type: String
validates :title, presence: true
validates :author, presence: true
validates :isbn, presence: true
index({ title: 'text' })
index({ isbn:1 }, { unique: true, name: "isbn_index" })
scope :title, -> (title) { where(title: /^#{title}/) }
scope :isbn, -> (isbn) { where(isbn: isbn) }
scope :author, -> (author) { where(author: author) }
end
get '/' do
'Welcome to BookList!'
end
namespace '/api/v1' do
before do
content_type 'application/json'
end
get '/books' do
books = Book.all
[:title, :isbn, :author].each do |filter|
books = books.send(filter, params[filter]) if params[filter]
end
Book.all.to_json
end
end