diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 40bf048..c714418 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,37 +2,15 @@ - - - - - - - - - - - - - - - - - - - - - - - + + + + - - - @@ -47,37 +25,46 @@ + + @@ -339,25 +407,26 @@ - - + - + - + @@ -380,11 +449,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - @@ -392,7 +503,6 @@ - @@ -400,7 +510,6 @@ - @@ -408,23 +517,6 @@ - - - - - - - - - - - - - - - - - @@ -436,27 +528,10 @@ - - - - - - - - - - - - - - - - - @@ -464,7 +539,6 @@ - @@ -472,7 +546,6 @@ - @@ -480,7 +553,6 @@ - @@ -488,7 +560,6 @@ - @@ -496,23 +567,6 @@ - - - - - - - - - - - - - - - - - @@ -536,15 +590,6 @@ - - - - - - - - - @@ -552,15 +597,6 @@ - - - - - - - - - @@ -568,7 +604,6 @@ - @@ -576,7 +611,6 @@ - @@ -584,7 +618,6 @@ - @@ -592,7 +625,6 @@ - @@ -600,15 +632,6 @@ - - - - - - - - - @@ -616,7 +639,6 @@ - @@ -624,15 +646,6 @@ - - - - - - - - - @@ -640,41 +653,12 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -683,18 +667,178 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + + + + + + + + + + + + + + + + + diff --git a/Gemfile b/Gemfile index 2c89434..e256b7b 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,7 @@ gem 'puma', '~> 3.7' # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1.7' gem 'jwt' +gem 'rack-cors', :require => 'rack/cors' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 80bed93..589b3b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,6 +79,7 @@ GEM mini_portile2 (~> 2.3.0) puma (3.11.2) rack (2.0.4) + rack-cors (1.0.2) rack-test (0.8.2) rack (>= 1.0, < 3) rails (5.1.5) @@ -161,6 +162,7 @@ DEPENDENCIES listen (>= 3.0.5, < 3.2) mysql2 (>= 0.4.10) puma (~> 3.7) + rack-cors rails (~> 5.1.5) rspec-rails (~> 3.5) shoulda-matchers (~> 3.1) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 0000000..8efb548 --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,46 @@ +class ArticlesController < ApplicationController + before_action :set_article, only: [:show, :update, :destroy] + skip_before_action :authorize_request, only: [:index, :show] + + # GET /articles + def index + @article = Article.all.to_json(include: :user) + + json_response(@article) + end + + # POST /articles + def create + @article = current_user.articles.create!(article_params) + json_response(@article, :created) + end + + # GET /articles/:id + def show + json_response(@article) + end + + # PUT /articles/:id + def update + @article.update(article_params) + head :no_content + end + + # DELETE /articles/:id + def destroy + @article.destroy + head :no_content + end + + private + + def article_params + # whitelist params + params.permit(:title, :post) + end + + def set_article + @article = Article.find(params[:id]) + end + +end diff --git a/app/controllers/authentication_controller.rb b/app/controllers/authentication_controller.rb index e5cc080..837ac3d 100644 --- a/app/controllers/authentication_controller.rb +++ b/app/controllers/authentication_controller.rb @@ -4,7 +4,8 @@ class AuthenticationController < ApplicationController def authenticate auth_token = AuthenticateUser.new(auth_params[:email], auth_params[:password]).call - json_response(auth_token: auth_token) + user = User.find_by(email: auth_params[:email]) + json_response(auth_token: auth_token, user: user) end private diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c44c9f0..fa0f59d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,7 @@ class UsersController < ApplicationController skip_before_action :authorize_request, only: :create + before_action :set_user, only: [:show] + # POST /signup # return authenticated token upon signup def create @@ -9,6 +11,10 @@ class UsersController < ApplicationController json_response(response, :created) end + def show + json_response(@user) + end + private def user_params @@ -19,4 +25,7 @@ class UsersController < ApplicationController :password_confirmation ) end + def set_user + @user = User.find(params[:id]) + end end diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 0000000..0c8bb68 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,5 @@ +class Article < ApplicationRecord + belongs_to :user + + validates_presence_of :title, :post +end diff --git a/app/models/user.rb b/app/models/user.rb index 6b3058f..c8d05c9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ApplicationRecord has_secure_password has_many :todos, foreign_key: :created_by + has_many :articles validates_presence_of :name, :email, :password_digest validates_uniqueness_of :email diff --git a/config/application.rb b/config/application.rb index 7ae0794..74d1470 100644 --- a/config/application.rb +++ b/config/application.rb @@ -24,7 +24,16 @@ module VeretoApi # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. + # cors config + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', :headers => :any, :methods => [:get, :post, :options] + end + end + config.eager_load_paths << Rails.root.join('lib') + config.autoload_paths << Rails.root.join('lib') # Only loads a smaller set of middleware suitable for API only apps. # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. diff --git a/config/routes.rb b/config/routes.rb index 1266c8f..67de6eb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,8 @@ Rails.application.routes.draw do resources :todos do resources :items end - + resources :articles + resources :users post 'auth/login', to: 'authentication#authenticate' post 'signup', to: 'users#create' end diff --git a/db/migrate/20180219100642_create_articles.rb b/db/migrate/20180219100642_create_articles.rb new file mode 100644 index 0000000..02a916a --- /dev/null +++ b/db/migrate/20180219100642_create_articles.rb @@ -0,0 +1,11 @@ +class CreateArticles < ActiveRecord::Migration[5.1] + def change + create_table :articles do |t| + t.string :title + t.text :post + t.references :user, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 903c0d4..4318f51 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,16 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180216131546) do +ActiveRecord::Schema.define(version: 20180219100642) do + + create_table "articles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| + t.string "title" + t.text "post" + t.bigint "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_articles_on_user_id" + end create_table "items", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "name" @@ -36,5 +45,6 @@ ActiveRecord::Schema.define(version: 20180216131546) do t.datetime "updated_at", null: false end + add_foreign_key "articles", "users" add_foreign_key "items", "todos" end diff --git a/spec/controllers/articles_controller_spec.rb b/spec/controllers/articles_controller_spec.rb new file mode 100644 index 0000000..70b28de --- /dev/null +++ b/spec/controllers/articles_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ArticlesController, type: :controller do + +end diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb new file mode 100644 index 0000000..632e856 --- /dev/null +++ b/spec/models/article_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Article, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end