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 @@ +
+<%= form_with model: [user, post, comment], scope: "comment" do |form|%> +

Add comment

+ <%= form.text_area :text%> + <%= form.submit "Comment"%> +<% end %> +
\ No newline at end of file diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..8675ad9 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,2 @@ +#

New comment

+#<%= render partial: "form" locals: {comment: @comment}%> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4edbdbd..03f3e65 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,8 @@ <%= stylesheet_link_tag 'user_show.css' %> <%= stylesheet_link_tag 'post_index.css' %> <%= stylesheet_link_tag 'post_show.css' %> + <%= stylesheet_link_tag 'new_post.css' %> + <%= stylesheet_link_tag 'new_comment.css' %> <%= javascript_importmap_tags %> diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb new file mode 100644 index 0000000..99c1fc2 --- /dev/null +++ b/app/views/posts/_form.html.erb @@ -0,0 +1,19 @@ + + + +

Create a new Post

+<%= form_with model: [@user, @post], local: true do |form| %> +
+ <%= form.label :title %> + <%= form.text_field :title %> +
+ +
+ <%= form.label :text %> + <%= form.text_area :text %> +
+ +
+ <%= form.submit 'Create Post' %> +
+<% end %> \ No newline at end of file diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index efda295..9f53410 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,5 +1,11 @@ <%= render partial: "shared/person_info" %> +
+<% if @user.id == @first_user.id %> + <%= link_to 'New Post', new_user_post_path(@user) %> +<% end %> +
+
<% @posts.each_with_index do |post, i| %> diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..ad1b887 --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,2 @@ +

New Post

+<%= render partial: "form", locals: {post: @post}%> \ No newline at end of file diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 4590645..f567356 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -1 +1,3 @@ -<%= render partial: "shared/post_info_version_2", locals: {post: @post}%> \ No newline at end of file +<%= render partial: "shared/post_info_version_2", locals: {post: @post,current_user: @user}%> + + diff --git a/app/views/shared/_post_info_version_2.html.erb b/app/views/shared/_post_info_version_2.html.erb index ed2e9b8..3ee82f0 100644 --- a/app/views/shared/_post_info_version_2.html.erb +++ b/app/views/shared/_post_info_version_2.html.erb @@ -7,6 +7,13 @@ Comments: <%= post.comments_counter %> + + <% if current_user.present? %> +
+ <%= button_to "Like", like_user_post_path(current_user, post), method: :post, remote: true %> + <%= button_to "Unlike", unlike_user_post_path(current_user, post), method: :delete, remote: true %> +
+<% end %>
@@ -15,4 +22,5 @@ <%= render partial: "shared/comments_info", locals: { comment: comment } %> <% end %> <% end %> +<%= render partial: "comments/form", locals: { user: @user, post: @post, comment: @new_comment } %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index c0c32b6..6a1caf7 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -6,6 +6,12 @@ <%= @user.bio %>

+ +
+ <% if @user.id == @first_user.id %> + <%= link_to 'New Post', new_user_post_path(@user) %> + <% end %> +
<% @user.recent_posts.each_with_index do |post, i| %> diff --git a/config/routes.rb b/config/routes.rb index f12f2ec..65666cd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,12 @@ Rails.application.routes.draw do root "users#index" - resources :users, only: [:index, :new, :create, :show, :update] do - resources :posts, only: [:index, :new, :create, :show, :update] + resources :users do + resources :posts do + member do + post 'like' + delete 'unlike' + end + resources :comments, only: [:create, :update, :destroy] + end end end