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 @@
+
<%= @post.text %>
+
Add Comment
+ <%= form_with(model: @comment, url: user_post_comments_url(@post.author, @post), local: true) do |form| %> +