Skip to content

Commit

Permalink
Merge pull request #194 from RailsGirlsCPH/api_prog_and_meeting_part_2
Browse files Browse the repository at this point in the history
Api prog and meeting part 2
  • Loading branch information
sibsmc authored Jan 12, 2020
2 parents 6e8d8ce + f4220a2 commit a0034fe
Show file tree
Hide file tree
Showing 20 changed files with 292 additions and 221 deletions.
2 changes: 2 additions & 0 deletions server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'capybara', '~> 3.30'
gem 'selenium-webdriver'
gem 'rspec-rails', '~> 3.9'
Expand Down
16 changes: 16 additions & 0 deletions server/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
bcrypt (3.1.13)
bindex (0.8.1)
binding_of_caller (0.8.0)
debug_inspector (>= 0.0.1)
bootsnap (1.4.5)
msgpack (~> 1.0)
builder (3.2.4)
Expand All @@ -73,6 +75,7 @@ GEM
regexp_parser (~> 1.5)
xpath (~> 3.2)
childprocess (3.0.0)
coderay (1.1.2)
coffee-rails (5.0.0)
coffee-script (>= 2.2.0)
railties (>= 5.2.0)
Expand All @@ -83,6 +86,7 @@ GEM
concurrent-ruby (1.1.5)
crass (1.0.5)
database_cleaner (1.7.0)
debug_inspector (0.0.3)
diff-lcs (1.3)
erubi (1.9.0)
execjs (2.7.0)
Expand All @@ -98,6 +102,7 @@ GEM
activesupport (>= 4.2.0)
i18n (1.7.1)
concurrent-ruby (~> 1.0)
interception (0.5)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
jwt (2.2.1)
Expand All @@ -120,6 +125,15 @@ GEM
nio4r (2.5.2)
nokogiri (1.10.7)
mini_portile2 (~> 2.4.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rescue (1.5.0)
interception (>= 0.5)
pry (>= 0.12.0)
pry-stack_explorer (0.4.9.3)
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
pg (1.2.2)
public_suffix (4.0.1)
puma (4.3.1)
Expand Down Expand Up @@ -241,6 +255,8 @@ DEPENDENCIES
jwt
listen (>= 3.0.5, < 3.3)
pg
pry-rescue
pry-stack_explorer
puma (~> 4.3)
rails (~> 6.0)
rspec-rails (~> 3.9)
Expand Down
9 changes: 5 additions & 4 deletions server/app/controllers/api/v1/api_users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class Api::V1::ApiUsersController < ApplicationController
before_action :set_api_user, only: [:show, :update, :destroy]
#xbefore_action :set_default_response_format


#GET /api_users
def index
@api_users = ApiUser.includes(:wishes).all
@api_users = ApiUser.includes(:wishes).includes(wishes: :programminglanguage).includes(wishes: :meetinginterval).all
#Explanation regarding includes: not necessary to link programming languages and meeting interval to ApiUser, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future.
@api_users = @api_users.where(mentor: true) if params[:mentor] == 'true'
@api_users = @api_users.where(mentee: true) if params[:mentee] == 'true'
end
Expand All @@ -22,7 +23,7 @@ def show

# PUT /api_users/:id
def update
@api_user.update(api_user_params)
@api_user.update!(api_user_params)
head :no_content
end

Expand All @@ -36,7 +37,7 @@ def destroy

def api_user_params

params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :mentor, :mentee)
params.require(:api_user).permit(:first_name, :last_name, :city, :email, :password_digest, :username, :mentor, :mentee)
end

def set_api_user
Expand Down
8 changes: 5 additions & 3 deletions server/app/controllers/api/v1/wishes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ class Api::V1::WishesController < ApplicationController
before_action :set_api_user
before_action :set_api_user_wish, only: [:show, :update, :destroy]


#GET /api_users/:api_user_id/wishes
def index
json_response(@api_user.wishes)
@wishes = @api_user.wishes.includes(:programminglanguage).includes(:meetinginterval)
@wishes = @wishes.where(available_offline: true) if params[:available_offline] == 'true'
@wishes = @wishes.where(available_online: true) if params[:available_online] == 'true'
#Explanation regarding includes: not necessary to link programming languages and meeting interval to Wishes, but it means there is only one call to the database during which it pulls all the information linked by foreign keys in case it needs it in the future.
end

# Post /api_users/:api_user_id/wishes
Expand Down Expand Up @@ -33,7 +35,7 @@ def destroy
private

def wish_params
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id)
params.permit(:available_offline, :available_online, :goal, :api_user_id, :id, :programminglanguage_id, :meetinginterval_id)
end

def set_api_user
Expand Down
4 changes: 3 additions & 1 deletion server/app/models/api_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ApiUser < ApplicationRecord
# Can write model association here for example
has_many :wishes, dependent: :destroy
# validation
validates_presence_of :email, :password_digest
validates_presence_of :email, :password_digest, :username, :mentor, :mentee
validates :email, uniqueness: true, on: [:create, :update]
validates :username, uniqueness: true, on: [:create, :update]
end

2 changes: 1 addition & 1 deletion server/app/models/meetinginterval.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Meetinginterval < ApplicationRecord
has_many :wishes
validates_presence_of :interval

end
1 change: 1 addition & 0 deletions server/app/models/programminglanguage.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Programminglanguage < ApplicationRecord
has_many :wishes
validates_presence_of :language
end
4 changes: 4 additions & 0 deletions server/app/models/wish.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Wish < ApplicationRecord
belongs_to :api_user
belongs_to :programminglanguage
delegate :language, :to => :programminglanguage
belongs_to :meetinginterval
delegate :interval, :to => :meetinginterval
# validation
validates_presence_of :goal
validate :online_or_offline
Expand Down
3 changes: 2 additions & 1 deletion server/app/views/api/v1/api_users/_api_user.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
json.call(api_user, :id, :mentor, :mentee)

json.wishes api_user.wishes do |wish|
json.call(wish, :id, :goal)
json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval)
end

4 changes: 4 additions & 0 deletions server/app/views/api/v1/api_users/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ json.array! @api_users do |api_user|
json.partial! 'api_user', api_user: api_user
end

json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end

5 changes: 5 additions & 0 deletions server/app/views/api/v1/wishes/_wish.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


json.call(wish, :id, :goal, :available_online, :available_offline, :language, :interval)


5 changes: 5 additions & 0 deletions server/app/views/api/v1/wishes/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


json.array! @wishes do |wish|
json.partial! 'wish', wish: wish
end
1 change: 1 addition & 0 deletions server/db/migrate/20190623093635_create_api_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def change

t.string :first_name
t.string :last_name
t.string :username
t.string :city
t.string :email
t.string :password_digest
Expand Down
1 change: 1 addition & 0 deletions server/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
create_table "api_users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.string "username"
t.string "city"
t.string "email"
t.string "password_digest"
Expand Down
12 changes: 6 additions & 6 deletions server/db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
Programminglanguage.destroy_all
Wish.destroy_all
users = ApiUser.create([
{first_name: 'Mary', last_name: 'Black', city: 'Dublin', mentor: true, mentee: false, email: '[email protected]', password_digest: 'password1'},
{first_name: 'Bob', last_name: 'Dylan', city: 'New York', mentor: true, mentee: true, email: '[email protected]', password_digest: 'password2'},
{first_name: 'Jane', last_name: 'Smith', city: 'Edinburgh', mentor: false, mentee: true, email: '[email protected]', password_digest: 'password3'}
{first_name: 'Mary', last_name: 'Black', city: 'Dublin', mentor: true, mentee: false, email: '[email protected]', password_digest: 'password1', username: 'user1'},
{first_name: 'Bob', last_name: 'Dylan', city: 'New York', mentor: true, mentee: true, email: '[email protected]', password_digest: 'password2', username: 'user2'},
{first_name: 'Jane', last_name: 'Smith', city: 'Edinburgh', mentor: false, mentee: true, email: '[email protected]', password_digest: 'password3', username: 'user3' }
])
Meetinginterval.create([{interval: 'weekly'}, {interval: 'daily'}])
Programminglanguage.create([{language: 'python'},{language: 'SCALA'}])

users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good'}])
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter'}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python'}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code real good', programminglanguage_id: Programminglanguage.second.id, meetinginterval_id: Meetinginterval.first.id}])
users.second.wishes.create([{available_offline: true, available_online: true, goal: 'Get smarter', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.first.id}])
users.first.wishes.create([{available_offline: false, available_online: true, goal: 'Code really well in Python', programminglanguage_id: Programminglanguage.first.id, meetinginterval_id: Meetinginterval.second.id}])

p "Created #{ApiUser.count} api_user entries"
p "Created #{Meetinginterval.count} meeting interval entries"
Expand Down
5 changes: 3 additions & 2 deletions server/spec/factories/api_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
password_digest {Faker::Lorem.word} #Misc doesn't seem to work {Faker::Misc.password}
first_name {Faker::Name.first_name}
last_name {Faker::Name.last_name}
username {Faker::Name.name}
city {Faker::Address.city}
mentor {Faker::Boolean.boolean(true_ratio: 0.5)}
mentee {Faker::Boolean.boolean(true_ratio: 0.5)}
mentor {Faker::Boolean.boolean(true_ratio: 1)}
mentee {Faker::Boolean.boolean(true_ratio: 1)}
end
end
Loading

0 comments on commit a0034fe

Please sign in to comment.