diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index cfcf65f..f2bd3aa 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -105,10 +105,45 @@ a { align-self: center; } +button { + border-bottom: 5px solid black; + border-right: 3px solid black; + border-radius: 5px; +} + +.btn-link, .btn-link button { cursor: pointer; padding: 5px; font-size: 15px; +} + +.post-form, +.comment-form { + width: 60%; + display: flex; + flex-direction: column; + padding: 1rem; + gap: 1rem; + border: 3px solid black; + background-color: #f5f5f5; +} + +.comments, +.posts { + width: 80%; + display: flex; + flex-direction: column; + padding: 1rem; + gap: 1rem; +} + +.comment-btn, +.like-button { + cursor: pointer; + padding: 5px 10px; + margin-left: 5rem; + font-size: 15px; border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; @@ -241,3 +276,10 @@ ul { color: blue; border: none; } + +.form-elements { + display: flex; + flex-direction: row; + gap: 1rem; + margin: 1rem 0; +} diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index aba64e2..cf3ebcb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,5 +1,26 @@ class CommentsController < ApplicationController - def index; end + def new + @comment = Comment.new + @user = User.find(params[:user_id]) + @post = Post.find(params[:post_id]) + end - def show; end + def create + @comment = Comment.new(comment_params) + @comment.user_id = current_user.id + @comment.post_id = params[:post_id] + + if @comment.save + redirect_to user_post_path(user_id: @comment.user_id, id: @comment.post_id), + notice: 'Comment was successfully created.' + else + render 'new' + end + end + + private + + def comment_params + params.require(:comment).permit(:content, :text) + end end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 52d88b8..502eac6 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -1,5 +1,72 @@ class LikesController < ApplicationController - def index; end + before_action :set_like, only: %i[show edit update destroy] + # GET /likes or /likes.json + def index + @likes = Like.all + end + + # GET /likes/1 or /likes/1.json def show; end + + # GET /likes/new + def new + @post = Post.find(params[:id]) + @like = Like.new(author_id: params[:user_id], post_id: @post.id) + @like.save + redirect_to user_post_path + end + + # GET /likes/1/edit + def edit; end + + # POST /likes or /likes.json + def create + @like = Like.new(like_params) + + respond_to do |format| + if @like.save + format.html { redirect_to like_url(@like), notice: 'Like was successfully created.' } + format.json { render :show, status: :created, location: @like } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /likes/1 or /likes/1.json + def update + respond_to do |format| + if @like.update(like_params) + format.html { redirect_to like_url(@like), notice: 'Like was successfully updated.' } + format.json { render :show, status: :ok, location: @like } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /likes/1 or /likes/1.json + def destroy + @like.destroy + + respond_to do |format| + format.html { redirect_to likes_url, notice: 'Like was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_like + @like = Like.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def like_params + params.require(:like).permit(:AuthorId, :PostId, :UpdatedAt, :CreatedAt) + end end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 029b184..c103797 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,7 @@ class PostsController < ApplicationController def index @user = User.find(params[:user_id]) @posts = @user.posts + @current_user = current_user end def show @@ -9,4 +10,27 @@ def show @post = @user.posts.find(params[:id]) @comments = @post.comments end + + def new + @post = Post.new + end + + def create + @post = Post.new(post_params) + @post.author_id = current_user.id + @post.comments_counter = 0 + @post.likes_counter = 0 + + if @post.save + redirect_to user_posts_path, notice: 'Post was successfully created.' + else + render 'new' + end + end + + private + + def post_params + params.require(:post).permit(:title, :text) + end end diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..5a14028 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,13 @@ +
+
+

Add Comment

+ <%= form_with(model: @comment, url: user_post_comments_url(@post.author, @post), local: true) do |form| %> +
+ <%= form.text_area :text %> +
+
+ <%= form.submit 'Create Comment', class: 'comment-btn'%> +
+ <% end %> +
+
\ No newline at end of file diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index cbd34d2..8eb4765 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,7 +1,7 @@ - + diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 76bb528..a23692c 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,8 +1,8 @@
-
-
- User Photo +
+
+ User Photo
diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..9bebee6 --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,19 @@ +
+
+

Create New Post

+ <%= form_with(model: @post, url: user_posts_path, local: true) do |form| %> +
+ <%= form.label :title %> + <%= form.text_field :title, name: 'post[title]' %> +
+
+ <%= form.label :text %> + <%= form.text_area :text, name: 'post[text]' %> +
+ <%= form.submit 'Create Post', class: 'btn-link' %> + <% end %> +
+
+``` + +### \ No newline at end of file diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 5e4e072..4dc676a 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -21,6 +21,15 @@

<%= @post.text %>

+
+ <% if @current_user %> + <%= form_with(model: @post, url: like_user_post_path(user_id: @current_user.id, id: @post.id), method: :post, local: true) do |form| %> + <%= form.button type: :submit, class: 'like-button' do %> + Like + <% end %> + <% end %> + <% end %> +
@@ -31,5 +40,11 @@
<% end %> + +
+ <%= link_to new_user_post_comment_url(@user, @post), class: 'btn-link' do %> + + <% end %> +
diff --git a/config/routes.rb b/config/routes.rb index 3504bfa..06779d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,9 @@ root "users#index" resources :users, only: [:index, :show] do - resources :posts, only: [:index, :show] + resources :posts, only: [:index, :show, :new, :create] do + resources :comments, only: [:new, :create] + post 'like', on: :member + end end end