Skip to content
This repository was archived by the owner on Aug 8, 2019. It is now read-only.

Commit f5f782f

Browse files
author
Frank Davis Condezo Fabian
authored
Merge pull request #13 from codeableorg/feature-create-club
Feature create club
2 parents d36a1a1 + 4679861 commit f5f782f

18 files changed

Lines changed: 448 additions & 36 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ client/npm-debug.log*
4848
client/yarn-debug.log*
4949
client/yarn-error.log*
5050

51-
.env
51+
client/.env

api/app/controllers/clubs_controller.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ def show
99
render json: @club
1010
end
1111

12+
def create
13+
club = Club.new(club_params)
14+
if club.save
15+
render json: club, status: :created
16+
else
17+
render json: { errors: club.errors}, status: :unprocessable_entity
18+
end
19+
end
20+
1221
rescue_from ActiveRecord::RecordNotFound do |e|
1322
render json: { message: e.message }, status: :not_found
1423
end
@@ -18,4 +27,8 @@ def set_club
1827
@club = Club.find(params[:id])
1928
end
2029

21-
end
30+
def club_params
31+
params.permit(:name, :address, :schedule, :image)
32+
end
33+
34+
end

api/app/models/club.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
class Club < ApplicationRecord
2+
has_one_attached :image
3+
4+
validates :name, presence: true
5+
validates :address, presence: true
6+
# validates :schedule, presence: true
7+
before_save :set_parse, on: [ :create, :update ]
8+
9+
private
10+
def set_parse
11+
self.schedule = JSON.parse(self.schedule) if self.schedule.class != Hash
12+
end
13+
214
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class ClubSerializer < ActiveModel::Serializer
2+
include Rails.application.routes.url_helpers
3+
4+
attributes :id, :name, :address, :image, :schedule
5+
6+
def image
7+
# rails_blob_path(object.image, only_path: true) if object.image.attached?
8+
url_for(object.image) if self.object.image.attached?
9+
end
10+
end

api/config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
config.active_record.verbose_query_logs = true
4646

4747

48+
routes.default_url_options[:host] = 'localhost:4000'
49+
4850
# Raises error for missing translations
4951
# config.action_view.raise_on_missing_translations = true
5052

api/config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Rails.application.routes.draw do
2-
scope '/api' do
2+
scope :api do
33
post '/login', to: 'sessions#login'
44
post '/register', to: 'sessions#register'
55
delete '/logout', to: 'sessions#destroy'

api/db/schema.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_07_11_201636) do
13+
ActiveRecord::Schema.define(version: 2019_07_11_223219) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -44,6 +44,17 @@
4444
t.datetime "updated_at", null: false
4545
end
4646

47+
create_table "sport_fields", force: :cascade do |t|
48+
t.string "name"
49+
t.string "description"
50+
t.integer "price_day"
51+
t.integer "price_night"
52+
t.bigint "club_id"
53+
t.datetime "created_at", null: false
54+
t.datetime "updated_at", null: false
55+
t.index ["club_id"], name: "index_sport_fields_on_club_id"
56+
end
57+
4758
create_table "users", force: :cascade do |t|
4859
t.string "name"
4960
t.string "token"
@@ -56,4 +67,5 @@
5667
end
5768

5869
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
70+
add_foreign_key "sport_fields", "clubs"
5971
end

api/db/seeds.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
puts "Init seed"
12
User.destroy_all
23
User.create(name: 'Lian Nivin', email: 'liam@kampu.pe', role: "regular", password: '123456')
34
User.create(name: 'Cristian Berly', email: 'berli@kampu.pe', role: "owner", password: '123456')
45

56
clubs = Club.create([{name: "Club #1"}, {name: "Club #2"}, {name: "Club #3"}])
67
SportField.create(name: "SportField #1", club_id: 1);
78
SportField.create(name: "SportField #2", club_id: 2);
8-
SportField.create(name: "SportField #3", club_id: 1);
9+
SportField.create(name: "SportField #3", club_id: 1);
10+
11+
Club.create(
12+
name: 'Club golden',
13+
address: 'Jr cayumba 440',
14+
schedule: {
15+
'monday-friday': {
16+
start: '8',
17+
end: '22'
18+
},
19+
'saturday': {
20+
start: '8',
21+
end: '22'
22+
},
23+
'sunday': {
24+
start: '8',
25+
end: '22'
26+
},
27+
}
28+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'rails_helper'
2+
3+
describe ClubsController do
4+
5+
before do
6+
@user_params = {
7+
name: 'Club golden',
8+
address: 'Jr cayumba 440',
9+
schedule: JSON.generate({
10+
'monday-friday': {
11+
start: '8',
12+
end: '22'
13+
},
14+
'saturday': {
15+
start: '8',
16+
end: '22'
17+
},
18+
'sunday': {
19+
start: '8',
20+
end: '22'
21+
},
22+
})
23+
}
24+
end
25+
26+
describe 'POST create' do
27+
it 'returns http status created' do
28+
post :create, params: @user_params
29+
expect(response.status).to eq(201)
30+
expect(response).to have_http_status(:created)
31+
end
32+
33+
it 'returns the club created' do
34+
post :create, params: @user_params
35+
expected_club = JSON.parse(response.body)
36+
expect(expected_club).to have_key("id")
37+
expect(expected_club["name"]).to eq("Club golden")
38+
end
39+
end
40+
41+
end

client/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
REACT_APP_VERSION=$npm_package_version
2+
REACT_APP_API_URL=http://localhost:4000/api
3+
REACT_APP_API_URL_PRODUCTION=

0 commit comments

Comments
 (0)