diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ecabe61..0174f6a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,7 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. - protect_from_forgery with: :exception + protect_from_forgery with: :null_session helper_method :current_user @@ -9,19 +9,13 @@ class ApplicationController < ActionController::Base render '/welcome' end - def amiloggedin - amiloggedin = !!session[:current_user_id] - - render json: current_user - end - private def current_user if session[:current_user_id] @current_user ||= User.find(session[:current_user_id]) else - @current_user = false + @current_user = nil end end diff --git a/app/controllers/confessions_controller.rb b/app/controllers/confessions_controller.rb index 25107d4..07ec266 100644 --- a/app/controllers/confessions_controller.rb +++ b/app/controllers/confessions_controller.rb @@ -1,2 +1,26 @@ class ConfessionsController < ApplicationController + # skip_before_action :verify_authenticity_token + + def create + transgression = Transgression.find(params[:transgression_id]) + + @confession = transgression.confessions.new(confession_params) + + if @confession.save + + else + render json: { + error: { + message: @confession.errors.full_messages.to_sentence + } + } + end + end + + private + + def confession_params + return params.require(:confession) + .permit(:description, :occurred_at) + end end diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index e9743d4..effe77a 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -1,4 +1,5 @@ class SessionController < ApplicationController + # skip_before_action :verify_authenticity_token, only: :create def create user = User.find_by(email: user_params[:email]) @@ -19,6 +20,11 @@ class SessionController < ApplicationController redirect_to root_path end + def current_sinner + + end + + private def user_params diff --git a/app/controllers/transgressions_controller.rb b/app/controllers/transgressions_controller.rb index 9b6ff2b..57bc164 100644 --- a/app/controllers/transgressions_controller.rb +++ b/app/controllers/transgressions_controller.rb @@ -1,52 +1,29 @@ class TransgressionsController < ApplicationController before_action :require_current_user + # skip_before_action :verify_authenticity_token, only: :create - def new - @transgression = Transgression.new + def index + @transgressions = current_user.transgressions.includes(:confessions) end def create - @transgression = current_user.transgressions - .new(transgression_params) + @transgression = current_user.transgressions.new(transgression_params) - @confession = @transgression.confessions.new(confession_params) + if @transgression.save - if @transgression.save && @confession.save - redirect_to transgressions_path else - flash[:message] = @transgression.errors.full_messages.to_sentence - render :new + render json: { + error: { + message: @transgression.errors.full_messages.to_sentence + } + } end end - def edit - end - - def update - # params[:id] - end - - def show - end - - def index - @transgressions = current_user.transgressions - .includes(:confessions) - .group_by { |x| x.sin_type } - end - - def delete - end - private def transgression_params return params.require(:transgression) .permit(:sin_type, :description) end - - def confession_params - return params.require(:confession) - .permit(:description, :occurred_at) - end end diff --git a/app/views/confessions/create.json.jbuilder b/app/views/confessions/create.json.jbuilder new file mode 100644 index 0000000..f655d88 --- /dev/null +++ b/app/views/confessions/create.json.jbuilder @@ -0,0 +1,7 @@ +json.transgression_id @confession.transgression_id + +json.confession do + json.id @confession.id + json.description @confession.description + json.occurred_at time_ago_in_words(@confession.created_at) + " ago" +end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 50fcd76..1142722 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -5,13 +5,6 @@ The Sins We Commit - <%= javascript_include_tag 'login_signup.js' %> diff --git a/app/views/session/current_sinner.json.jbuilder b/app/views/session/current_sinner.json.jbuilder new file mode 100644 index 0000000..74d9a70 --- /dev/null +++ b/app/views/session/current_sinner.json.jbuilder @@ -0,0 +1,7 @@ +if current_user + json.current_user do + json.email current_user.email + end +else + json.current_user nil +end diff --git a/app/views/transgressions/create.json.jbuilder b/app/views/transgressions/create.json.jbuilder new file mode 100644 index 0000000..5a3fe6b --- /dev/null +++ b/app/views/transgressions/create.json.jbuilder @@ -0,0 +1,7 @@ +json.transgression do + json.id @transgression.id + json.sin_type @transgression.sin_type + json.description @transgression.description + json.authored_at time_ago_in_words(@transgression.created_at) + " ago" + json.confessions [] +end diff --git a/app/views/transgressions/index.html.erb b/app/views/transgressions/index.html.erb deleted file mode 100644 index c252c40..0000000 --- a/app/views/transgressions/index.html.erb +++ /dev/null @@ -1,22 +0,0 @@ - -<% @transgressions.each do |sin_type, trans| %> -

- For the sin of <%= sin_type %>, your weaknesses are: -

- -<% end %> diff --git a/app/views/transgressions/index.json.jbuilder b/app/views/transgressions/index.json.jbuilder new file mode 100644 index 0000000..0233cb1 --- /dev/null +++ b/app/views/transgressions/index.json.jbuilder @@ -0,0 +1,15 @@ +json.sinner current_user.email + +json.transgressions(@transgressions) do |trans| + + json.id trans.id + json.sin_type trans.sin_type + json.description trans.description + json.authored_at time_ago_in_words(trans.created_at) + " ago" + + json.confessions(trans.confessions) do |conf| + json.id conf.id + json.description conf.description + json.occurred_at time_ago_in_words(conf.created_at) + " ago" + end +end diff --git a/app/views/transgressions/new.html.erb b/app/views/transgressions/new.html.erb deleted file mode 100644 index 3fbd28d..0000000 --- a/app/views/transgressions/new.html.erb +++ /dev/null @@ -1,21 +0,0 @@ -
- -
- -
-
Your first confession!
-
-
- -
diff --git a/config/routes.rb b/config/routes.rb index 7b58590..ebdc12a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,20 +1,19 @@ Rails.application.routes.draw do - resources :confessions - resources :transgressions root 'application#welcome' - get '/amiloggedin' => 'application#amiloggedin' + resources :transgressions, only: [:index, :create], defaults: { format: :json } do + resources :confessions, only: [:create], shallow: true + end # resources :users, only: [:create] post '/users' => 'users#create' - # sessiony stuff - # get '/session' => 'session#current_user' - # angular? - + # session stuff + get '/session' => 'session#current_sinner', defaults: { format: :json } post '/session' => 'session#create' delete '/session' => 'session#destroy' + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes".