Skip to content

Commit 49641ed

Browse files
authored
Merge pull request #12 from tuanle03/develop
Release v17.12.2023
2 parents bc43fa6 + 048e848 commit 49641ed

File tree

9 files changed

+103
-60
lines changed

9 files changed

+103
-60
lines changed

.github/workflows/rspec.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Run RSpec tests
2+
on: [push]
3+
jobs:
4+
run-rspec-tests:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v2
8+
- name: Set up Ruby
9+
uses: ruby/setup-ruby@v1
10+
with:
11+
ruby-version: 3.2.0
12+
bundler-cache: true
13+
- name: Run tests
14+
run: |
15+
bundle exec rspec

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ group :development, :test do
5656
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
5757
gem "debug", platforms: %i[ mri mingw x64_mingw ]
5858
gem 'rspec-rails'
59+
gem 'factory_bot_rails'
5960
end
6061

6162
group :development do

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ GEM
108108
concurrent-ruby (~> 1.0)
109109
zeitwerk (~> 2.6)
110110
erubi (1.12.0)
111+
factory_bot (6.4.2)
112+
activesupport (>= 5.0.0)
113+
factory_bot_rails (6.4.2)
114+
factory_bot (~> 6.4)
115+
railties (>= 5.0.0)
111116
globalid (1.2.1)
112117
activesupport (>= 6.1)
113118
i18n (1.14.1)
@@ -273,6 +278,7 @@ DEPENDENCIES
273278
debug
274279
devise
275280
devise-jwt
281+
factory_bot_rails
276282
importmap-rails
277283
jbuilder
278284
puma (~> 5.0)

README.md

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,69 @@
1-
# README
1+
# Awesome Blog API
22

3-
This README would normally document whatever steps are necessary to get the
4-
application up and running.
3+
Welcome to the Awesome Blog API repository! This project is built using Ruby on Rails, Grape API, JWT (JSON Web Tokens), and Swagger. It provides a robust backend for a web blog application, allowing you to create, retrieve, update, and delete blog posts, comments, and more through a RESTful API.
54

6-
Things you may want to cover:
5+
## Table of Contents
76

8-
* Ruby version
7+
- [Features](#features)
8+
- [Prerequisites](#prerequisites)
9+
- [Getting Started](#getting-started)
10+
- [Installation](#installation)
11+
- [JWT Authentication](#jwt-authentication)
12+
- [Swagger Documentation](#swagger-documentation)
13+
- [Contributing](#contributing)
14+
- [License](#license)
915

10-
* System dependencies
16+
## Features
1117

12-
* Configuration
18+
- User authentication and authorization using JWT.
19+
- CRUD operations for blog posts, comments, and user profiles.
20+
- API documentation using Swagger.
21+
- Consistent and clean codebase following Ruby on Rails best practices.
1322

14-
* Database creation
23+
## Prerequisites
1524

16-
* Database initialization
25+
Before you begin, ensure you have the following prerequisites installed:
1726

18-
* How to run the test suite
27+
- Ruby (version 3.2.0)
28+
- Ruby on Rails (version 7.0)
1929

20-
* Services (job queues, cache servers, search engines, etc.)
30+
## Getting Started
2131

22-
* Deployment instructions
32+
### Installation
2333

24-
* ...
34+
1. Clone the repository:
35+
36+
```bash
37+
git clone [email protected]:tuanle03/code_learn_api.git
38+
cd code_learn_api
39+
```
40+
41+
2. Install dependencies:
42+
```bash
43+
bundle install
44+
```
45+
3. Set up the database:
46+
```bash
47+
rails db:create db:migrate
48+
```
49+
4. Start project
50+
```bash
51+
rails server
52+
```
53+
## JWT Authentication
54+
This project uses JSON Web Tokens (JWT) for user authentication. Make sure to include the JWT token in the Authorization header of your requests:
55+
```bash
56+
Authorization: Bearer YOUR_JWT_TOKEN
57+
```
58+
59+
## Swagger Documentation
60+
Explore the API endpoints and test them using the Swagger documentation at `http://localhost:3000/doc`.
61+
62+
## Contributing
63+
We welcome contributions! If you find a bug or have an enhancement in mind, please open an issue or submit a pull request.
64+
65+
## License
66+
This project is licensed under the MIT License.
67+
68+
Happy coding!
69+
Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,2 @@
11
class FeedbacksController < ApplicationController
2-
3-
before_action :authenticate_user!, except: [:index, :show]
4-
belongs_to :user
5-
belongs_to :post
6-
7-
def index
8-
@feedbacks = Feedback.all
9-
end
10-
11-
def new
12-
@feedback = Feedback.new
13-
end
14-
15-
def show
16-
@feedback = Feedback.find(params[:id])
17-
end
18-
19-
def create
20-
@feedback = Feedback.new(feedback_params)
21-
if @feedback.save
22-
redirect_to @feedback
23-
else
24-
render :new
25-
end
26-
end
27-
28-
def update
29-
@feedback = Feedback.find(params[:id])
30-
if @feedback.update(feedback_params)
31-
redirect_to @feedback
32-
else
33-
render :edit
34-
end
35-
end
36-
37-
def destroy
38-
@feedback = Feedback.find(params[:id])
39-
@feedback.destroy
40-
redirect_to feedbacks_path
41-
end
42-
43-
private
44-
45-
def feedback_params
46-
params.require(:feedback).permit(:user_id, :post_id, :content)
47-
end
482
end

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class User < ApplicationRecord
77

88
def generate_jwt
99
payload = { user_id: id, exp: 1.day.from_now.to_i }
10-
JWT::Encoder.encode(payload, Rails.application.credentials.devise_jwt_secret_key)
10+
JWT.encode(payload, Rails.application.credentials.devise_jwt_secret_key, 'HS256')
1111
end
1212
end

spec/factories/users.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FactoryBot.define do
2+
factory :user do
3+
email { '[email protected]' }
4+
password { 'password123' }
5+
end
6+
end

spec/models/user_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe User, type: :model do
4+
describe 'generate_jwt' do
5+
it 'generates a valid JWT token' do
6+
user = create(:user) # Use FactoryBot to create a user
7+
8+
jwt_token = user.generate_jwt
9+
10+
decoded_token = JWT.decode(jwt_token, Rails.application.credentials.devise_jwt_secret_key, true, algorithm: 'HS256')
11+
expect(decoded_token[0]['user_id']).to eq(user.id)
12+
end
13+
end
14+
end

spec/rails_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
# instead of true.
3939
config.use_transactional_fixtures = true
4040

41+
config.include FactoryBot::Syntax::Methods
42+
4143
# You can uncomment this line to turn off ActiveRecord support entirely.
4244
# config.use_active_record = false
4345

0 commit comments

Comments
 (0)