Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Friendship
- include DataMapper::Resource
- property :id, Serial
- property :created_at, DateTime
- belongs_to :source, 'User', :key => true
- belongs_to :target, 'User', :key => true
- validates_presence_of :source
- validates_presence_of :target
- end
- class User
- include DataMapper::Resource
- property :id, Serial
- property :email, String, :required => true, :format => :email_address, :unique => true
- property :password, BCryptHash, :required => true
- property :avatar_url, String
- property :phone_number, BCryptHash, :unique => true
- property :created_at, DateTime
- has n, :friendships, :child_key => [ :source_id ]
- has n, :friends, self, :through => :friendships, :via => :target
- has n, :votes
- has n, :favorites
- has n, :memberships
- def polls_voted_on
- self.votes.map { |v| v.option.poll }
- end
- def polls_favorited
- self.favorites.map(&:poll)
- end
- end
- class Qurb < Sinatra::Application
- post '/users/:user_id/friendships' do
- friendship = Friendship.new(:source_id => params[:user_id], :target_id => params[:friend_id])
- if friendship.save
- {friendship: friendship}.to_json
- else
- {errors: friendship.errors.to_h}.to_json
- end
- end
- get '/user/:user_id/friendships' do
- user = User.first(:id => params[:user_id])
- if user
- {friends: user.friendships.to_h}.to_json
- else
- {errors: friendships.errors.to_h}.to_json
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment