Skip to content

Commit

Permalink
adding reservation api-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aradradev committed Nov 1, 2023
1 parent 859eada commit cecf81c
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::V1::ReservationsController < ApplicationController

def index
current_user = User.find(params[:user_id])
@reservations = current_user.reservations.includes(:user, :reservation).order(created_at: :desc).all
@reservations = current_user.reservations.includes(:user, :car).order(created_at: :desc).all
if @reservations
render json: { status: { code: 200, message: 'Reservations retrieved successfully.', data: @reservations } }, status: :ok
else
Expand Down
142 changes: 142 additions & 0 deletions spec/integration/api/v1/reservations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
require 'swagger_helper'

describe 'Reservations API' do
path '/api/v1/users/{user_id}/reservations' do
get 'Retrieves a list of reservations for a user' do
tags 'Reservations'
produces 'application/json'
parameter name: :user_id, in: :path, type: :string

response '200', 'reservations found' do
run_test!
end

response '404', 'user not found' do
let(:user_id) { 'invalid_user_id' }
run_test!
end
end

post 'Creates a new reservation for a user' do
tags 'Reservations'
consumes 'application/json'
parameter name: :user_id, in: :path, type: :string
parameter name: :reservation, in: :body, schema: {
type: :object,
properties: {
location: { type: :string },
date: { type: :string, format: 'date' },
car_id: { type: :string }
},
required: %w[location date car_id]
}

response '201', 'reservation created' do
let(:user_id) { create(:user).id }
let(:reservation) { { location: 'Sample Location', date: '2023-12-31', car_id: create(:car).id } }
run_test!
end

response '404', 'user not found' do
let(:user_id) { 'invalid_user_id' }
let(:reservation) { { location: 'Sample Location', date: '2023-12-31', car_id: create(:car).id } }
run_test!
end

response '422', 'invalid request' do
let(:user_id) { create(:user).id }
let(:reservation) { { location: '', date: '2023-12-31', car_id: create(:car).id } }
run_test!
end
end
end

path '/api/v1/users/{user_id}/reservations/{id}' do
get 'Retrieves a reservation for a user' do
tags 'Reservations'
produces 'application/json'
parameter name: :user_id, in: :path, type: :string
parameter name: :id, in: :path, type: :string

response '200', 'reservation found' do
let(:user_id) { create(:user).id }
let(:id) { create(:reservation, user: User.find(user_id)).id }
run_test!
end

response '404', 'user not found' do
let(:user_id) { 'invalid_user_id' }
let(:id) { create(:reservation).id }
run_test!
end

response '404', 'reservation not found' do
let(:user_id) { create(:user).id }
let(:id) { 'invalid_id' }
run_test!
end
end

path '/api/v1/users/{user_id}/reservations/{id}' do
put 'Update the reservations of each cars added' do
tags 'Reservation'
consumes 'apllication/json'
parameter name: :user_id, in: :path, type: :string, required: true
parameter name: :id, in: :path, type: :string, required: true
parameter name: :reservation, in: :body, schema: {
type: :object,
properties: {
location: { type: :string },
date: { type: :string, format: 'date' },
car_id: { type: :string }
}
}
response '200', 'update successful' do
end
response '404', 'User not found' do
let(:user_id) { 'nil' }
let(:id) { create(:reservation, user: User.find(:user_id).id) }
let(:reservation) { { location: 'New location', date: '2023-11-01', car_id: create(:car).id } }
run_test!
end
response '404', 'Reservation not found' do
let(:user_id) { create(:user).id }
let(:id) { 'nil id' }
let(:reservation) { { location: 'City', date: '2023-11-02', car_id: create(:car).id } }
run_test!
end
response '422', 'Invalid Request' do
let(:user_id) { create(:user).id }
let(:id) { create(:reservation, user: User.find(:user_id).id) }
let(:reservation) { { location: ' ', date: '2023-11-03', car_id: create(:car).id } }
run_test!
end
end

delete 'Delete an existing reservation for a user' do
tags 'Reservations'
consumes 'application/json'
parameter name: :user_id, in: :path, type: :string, required: true
parameter name: :id, in: :path, type: :string, required: true

response '204', 'reservation deleted' do
let(:user_id) { create(:user).id }
let(:id) { create(:reservation, user: User.find(:user_id).id) }
run_test!
end

response '404', 'reservation not found' do
let(:user_id) { 'invalid user_id' }
let(:id) { create(:reservation).id }
run_test!
end

response '404', 'reservation not found' do
let(:user_id) { create(:user).id }
let(:id) { 'invalid id' }
run_test!
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/swagger_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
paths: {},
servers: [
{
url: 'https://{defaultHost}',
url: 'http://localhost:3000}',
variables: {
defaultHost: {
default: 'www.example.com'
Expand Down
126 changes: 125 additions & 1 deletion swagger/v1/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,130 @@ paths:
description: car deleted
'404':
description: car not found
"/api/v1/users/{user_id}/reservations":
get:
summary: Retrieves a list of reservations for a user
tags:
- Reservations
parameters:
- name: user_id
in: path
required: true
schema:
type: string
responses:
'200':
description: reservations found
'404':
description: user not found
post:
summary: Creates a new reservation for a user
tags:
- Reservations
parameters:
- name: user_id
in: path
required: true
schema:
type: string
responses:
'201':
description: reservation created
'404':
description: user not found
'422':
description: invalid request
requestBody:
content:
application/json:
schema:
type: object
properties:
location:
type: string
date:
type: string
format: date
car_id:
type: string
required:
- location
- date
- car_id
"/api/v1/users/{user_id}/reservations/{id}":
get:
summary: Retrieves a reservation for a user
tags:
- Reservations
parameters:
- name: user_id
in: path
required: true
schema:
type: string
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: reservation found
'404':
description: reservation not found
put:
summary: Update the reservations of each cars added
tags:
- Reservation
parameters:
- name: user_id
in: path
required: true
schema:
type: string
- name: id
in: path
required: true
schema:
type: string
responses:
'404':
description: Reservation not found
'422':
description: Invalid Request
requestBody:
content:
apllication/json:
schema:
type: object
properties:
location:
type: string
date:
type: string
format: date
car_id:
type: string
delete:
summary: Delete an existing reservation for a user
tags:
- Reservations
parameters:
- name: user_id
in: path
required: true
schema:
type: string
- name: id
in: path
required: true
schema:
type: string
responses:
'204':
description: reservation deleted
'404':
description: reservation not found
"/api/v1/users":
get:
summary: Retrieves a list of users
Expand Down Expand Up @@ -217,7 +341,7 @@ paths:
'404':
description: user not found
servers:
- url: https://{defaultHost}
- url: http://localhost:3000}
variables:
defaultHost:
default: www.example.com

0 comments on commit cecf81c

Please sign in to comment.