diff --git a/app/assets/stylesheets/new_comment.css b/app/assets/stylesheets/new_comment.css new file mode 100644 index 0000000..fc2d432 --- /dev/null +++ b/app/assets/stylesheets/new_comment.css @@ -0,0 +1,6 @@ +.new_comment_section { + margin-top: 40px; + border-top: 3px solid rgb(221, 226, 145); + box-shadow: 2px 5px 5px black; + padding-left: 10px; +} diff --git a/app/assets/stylesheets/new_post.css b/app/assets/stylesheets/new_post.css new file mode 100644 index 0000000..5aa4596 --- /dev/null +++ b/app/assets/stylesheets/new_post.css @@ -0,0 +1,42 @@ +.new_post_section { + margin: 20px; + margin-left: 125px; +} + +.new_post_section > a { + text-decoration: underline; + font-size: 1.5rem; + font-weight: bolder; + color: rgb(100, 120, 163); +} + +.new_post_title { + margin-left: 35px; + color: rgb(100, 120, 163); + text-decoration: underline; +} + +.back_btn_link { + margin-left: 35px; +} + +.back_btn { + margin-left: 35px; + box-shadow: 2px 5px 5px black; +} + +.create_post_header { + margin-left: 35px; +} + +.Holder > form { + margin-left: 40px; +} + +.field { + margin-bottom: 20px; +} + +.actions { + margin-bottom: 10px; +} diff --git a/app/assets/stylesheets/post_index.css b/app/assets/stylesheets/post_index.css index 264e0b2..70c44e8 100644 --- a/app/assets/stylesheets/post_index.css +++ b/app/assets/stylesheets/post_index.css @@ -1,6 +1,5 @@ .comments_section { width: 97%; - padding-left: 10px; border: 3px solid black; margin: auto; margin-bottom: 50px; @@ -10,6 +9,7 @@ .comments_line { display: flex; margin-bottom: 6px; + padding-left: 10px; } .comment { diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..2513ba5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,5 @@ class ApplicationController < ActionController::Base + def current_user + @user = User.first + end end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..31d06dd --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,34 @@ +class CommentsController < ApplicationController + before_action :find_user + before_action :find_post + + def new + @comment = Comment.new + end + + def create + @comment = Comment.new(comment_params) + @comment.author = @user + @comment.post = @post + if @comment.save + flash[:notice] = 'Comment created successfully.' + redirect_to user_post_path(@user, @post) + else + render 'new' + end + end + + private + + def find_user + @user = ApplicationController.new.current_user + end + + def find_post + @post = Post.find(params[:post_id]) + end + + def comment_params + params.require(:comment).permit(:text) + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 303a8f2..22944a8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -3,11 +3,75 @@ def index @user = User.find(params[:user_id]) @posts = @user.posts @recent_comments_by_post = @posts.to_h { |post| [post.id, post.recent_comments] } + + @first_user = User.first end def show @user = User.find(params[:user_id]) @post = Post.find(params[:id]) @recent_comments = @post.recent_comments + @new_comment = @post.comments.build + end + + def new + @user = User.find(params[:user_id]) + @post = @user.posts.build + @post.comments_counter = 0 + @post.likes_counter = 0 + end + + def create + @user = User.find(params[:user_id]) + @post = @user.posts.build(post_params) + @post.comments_counter = 0 + @post.likes_counter = 0 + if @post.save + redirect_to user_post_path(@user, @post), notice: 'Post was successfully created.' + else + render :new + end + end + + def like + @user = User.find(params[:user_id]) + @post = Post.find(params[:id]) + if already_liked?(@user, @post) + redirect_to user_post_path(@user, @post) + else + @like = @post.likes.new(author_id: @user.id, post_id: @post.id) + + if @like.save + redirect_to user_post_path(@user, @post) + else + redirect_to user_posts_path(@user) + end + end + end + + def unlike + @user = User.find(params[:user_id]) + @post = Post.find(params[:id]) + @like = Like.find_by(author_id: @user.id, post_id: @post.id) + + if @like + if @like.destroy + redirect_to user_post_path(@user, @post), notice: 'Post unliked successfully.' + else + redirect_to user_post_path(@user, @post), alert: 'Error occurred while unliking the post.' + end + else + redirect_to user_post_path(@user, @post), alert: 'You have not liked this post.' + end + end + + private + + def post_params + params.require(:post).permit(:title, :text) + end + + def already_liked?(user, post) + Like.exists?(author_id: user.id, post_id: post.id) end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7ee3956..863f239 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,5 +5,6 @@ def index def show @user = User.find(params[:id]) + @first_user = ApplicationController.new.current_user end end diff --git a/app/models/like.rb b/app/models/like.rb index ab57967..3fb12d2 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,10 +1,11 @@ class Like < ApplicationRecord - belongs_to :post belongs_to :author, class_name: 'User' + belongs_to :post, class_name: 'Post' after_save :update_likes_counter + after_destroy :update_likes_counter def update_likes_counter - post.increment!(:likes_counter) + post.update(likes_counter: post.likes.count) end end diff --git a/app/models/user.rb b/app/models/user.rb index f222214..2300edb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,7 +3,7 @@ class User < ApplicationRecord validates :posts_counter, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } has_many :comments, foreign_key: :user_id - has_many :likes, foreign_key: :user_id + has_many :likes, foreign_key: :author_id has_many :posts, foreign_key: :author_id def recent_posts diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb new file mode 100644 index 0000000..9bcc701 --- /dev/null +++ b/app/views/comments/_form.html.erb @@ -0,0 +1,7 @@ +
Add comment
+ <%= form.text_area :text%> + <%= form.submit "Comment"%> +<% end %> +