Skip to content

Commit

Permalink
ouf
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence33 committed Jun 18, 2019
1 parent 168842d commit be06da1
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/stories.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/stories.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the stories controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
53 changes: 53 additions & 0 deletions app/controllers/stories_controller.rb
@@ -0,0 +1,53 @@
class StoriesController < ApplicationController

# before_action :authenticate_user!, only: [:create, :update, :destroy]
# before_action :check_story_owner, only: [:update, :destroy]
# before_action :current_user_x_story_id, only: [:show, :destroy]


def index
@user = current_user
@stories = @user.stories

# if @stories.empty?
# render json: { }, :status => 200
# else
# render json: { @stories.to_a }, :status => 200
# end
if @stories.empty?
render json: [], :status => 200
else
render json: @stories.to_a , :status => 200
end
end

def create
@user = current_user
@story = @user.stories.new(data: params[:data])

if @story.save
render json: {}, :status => 201
else
render json: {}, :status => 400
end

end

private

def check_story_owner
unless current_user.id == Story.find(params[:id]).user.id
redirect_to root_path
end
end

def current_user_x_story_id
unless current_user.id == params[:user_id]
redirect_to root_path
end
end

def story_params
params.require(:story).permit(:data)
end
end
2 changes: 2 additions & 0 deletions app/helpers/stories_helper.rb
@@ -0,0 +1,2 @@
module StoriesHelper
end
4 changes: 4 additions & 0 deletions app/models/story.rb
@@ -1,2 +1,6 @@
class Story < ApplicationRecord
belongs_to :user



end
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -4,6 +4,6 @@ class User < ApplicationRecord
#:recoverable, :rememberable, :validatable
devise :database_authenticatable, :registerable


has_many :stories, dependent: :destroy

end
4 changes: 4 additions & 0 deletions config/application.rb
Expand Up @@ -11,6 +11,10 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2


config.action_controller.default_protect_from_forgery = false


# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/devise.rb
Expand Up @@ -73,7 +73,7 @@
# config.http_authenticatable = false

# If 401 status code should be returned for AJAX requests. True by default.
# config.http_authenticatable_on_xhr = true
config.http_authenticatable_on_xhr = true

# The realm used in Http Basic Authentication. 'Application' by default.
# config.http_authentication_realm = 'Application'
Expand Down
1 change: 1 addition & 0 deletions config/initializers/session_store.rb
@@ -0,0 +1 @@
Rails.application.config.session_store :cookie_store, key: '_inklewriter_session'
8 changes: 7 additions & 1 deletion config/routes.rb
Expand Up @@ -3,5 +3,11 @@
}
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'pages#index'

post 'stories', to: 'stories#create'

resources :users do
resources :stories
end


end
7 changes: 7 additions & 0 deletions test/controllers/stories_controller_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class StoriesControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit be06da1

Please sign in to comment.