Commit 043e52cc authored by Sophie Daringer's avatar Sophie Daringer
Browse files

Add api endpoints create update delete

parent 02993fe7
class Api::V1::UsersController < Api::V1::BaseController
before_action :set_user, only: %i[show edit update destroy]
def index
users = User.all
render json: UserSerializer.new(users).serialized_json
end
def create
@user = User.new(user_params)
if @user.save
render status: 201, json: UserSerializer.new(@user).serialized_json
else
render json: @user.errors, status: 422
end
end
def show
user = User.find(params[:id])
render json: UserSerializer.new(user).serialized_json
def show
render json: UserSerializer.new(@user).serialized_json
end
def update
if @user.update(user_params)
render status: :ok, json: UserSerializer.new(@user).serializable_hash.to_json
else
render json: @user.errors, status: 422
end
end
def destroy
begin
@user.destroy
rescue ActiveRecord::RecordNotFound
render json: { "error": 'User not found' }, status: :not_found
return
rescue ActiveRecord::InvalidForeignKey
render json: { "error": 'User cannot be deleted' }, status: :forbidden
return
rescue StandardError
raise
rescue Exception
raise
end
render json: { head: :no_content }, status: 204
end
private
def set_user
@user = User.find(params[:id])
end
end
\ No newline at end of file
# Only allow a list of trusted parameters through.
def user_params
p = params.require(:data).permit(:type, attributes: %i[name email password])
if p[:type] == 'user'
p[:attributes]
else
nil
end
end
end
# frozen_string_literal: true
# file /spec/requests/users_spec.rb
require 'swagger_helper'
RSpec.describe 'Stand Alone API', type: :request do
describe 'Users' do
fixtures :users
before { @user = users(:mando) }
path '/api/v1/users' do
get 'list all the users' do
tags 'User'
produces 'abpplication/json'
response(200, 'successful') do
schema type: :object,
properties: {
data: {
type: :array,
items: {
type: :object,
properties: {
id: { type: :string },
type: { type: :string },
attributes: {
type: :object,
properties: {
name: { type: :string },
email: { type: :string }
},
required: %w[name email]
}
},
required: %w[id type attributes]
}
}
}
run_test!
end
end
# echtes json:api
# curl -X POST "http://localhost:3000/api/v1/users/" \
# -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' \
# --data "{\"data\":{\"type\":\"user\",\"attributes\":{\"name\":\"Ember\",\"email\":\"ember@hier.com\",\"password\":\"geheim\"}}}"
post 'Creates a user' do
tags 'User'
consumes 'application/json'
produces 'application/json'
parameter name: :user,
in: :body,
schema: {
type: :object,
properties: {
data: {
type: :object,
properties: {
type: { type: :string },
attributes: {
type: :object,
properties: {
name: { type: :string },
email: { type: :string },
password: { type: :string }
},
required: %w[name email password]
}
}
}
}
}
# Rails Style
# parameter name: :user,
# in: :body,
# schema: {
# type: :object,
# properties: {
# user: {
# type: :object,
# properties: {
# name: { type: :string },
# email: { type: :string },
# password: { type: :string }
# },
# required: %w[name email password]
# }
# }
# }
response '201', 'user created' do
let(:user) do
{ data: { type: 'user', attributes: { name: 'Good', email: 'good@hier.com', password: 'asecret' } } }
end
run_test!
after do |example|
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
end
end
response '422', "password can't be blank, name can't exist, e-mail can't exist" do
let(:user) do
u = User.first
{ data: { type: 'user', attributes: { name: u.name, email: u.email } } }
end
run_test!
after do |example|
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
end
end
end
end
path '/api/v1/users/{id}' do
get 'show user' do
tags 'User'
produces 'application/json'
parameter name: 'id', in: :path, type: :string
response 200, 'successful' do
schema type: :object,
properties: {
data: {
type: :object,
properties: {
id: { type: :string },
type: { type: :string },
attributes: {
type: :object,
properties: {
name: { type: :string },
email: { type: :string }
},
required: %w[name email]
}
},
required: %w[id type attributes]
}
}
let(:id) do
u = User.create!(name: 'Luke', email: 'luke@skywalker.net', password: '1234567', password_confirmation: '1234567')
u.id
end
run_test!
end
end
patch 'Updates a users data' do
tags 'User'
consumes 'application/json'
produces 'application/json'
parameter name: 'id', in: :path, type: :string
parameter name: :user,
in: :body,
schema: {
type: :object,
properties: {
data: {
type: :object,
properties: {
type: { type: :string },
id: { type: :string },
attributes: {
type: :object,
properties: {
name: { type: :string },
email: { type: :string },
password: { type: :string }
},
required: %w[name email password]
}
}
}
}
}
# # rails style
# parameter name: :user,
# in: :body,
# schema: {
# type: :object,
# properties: {
# data: {
# type: :object,
# properties: {
# type: { type: :string },
# attributes: {
# type: :object,
# properties: {
# name: { type: :string },
# email: { type: :string },
# password: { type: :string }
# },
# required: %w[name email password]
# }
# }
# }
# }
# }
response '200', 'user updated: Raider heisst jetzt Twix' do
let(:id) do
u = User.create!(name: 'Raider', email: 'raider@skywalker.net', password: '1234567', password_confirmation: '1234567')
u.id
end
let(:user) do
{ data: { type: 'user', attributes: { name: 'Twix', email: 'twix@skywalker.net' } } }
end
run_test!
after do |example|
example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) }
end
end
end
delete 'Deletes a users' do
tags 'User'
consumes 'application/json'
produces 'application/json'
parameter name: 'id', in: :path, type: :string
response '204', 'user updated' do
let(:id) do
u = User.create!(name: 'Raider', email: 'raider@skywalker.net', password: '1234567', password_confirmation: '1234567')
u.id
end
let(:user) do
{ user: { name: 'Twix', email: 'twix@skywalker.net' } }
end
run_test!
end
end
end
end
end
......@@ -11,7 +11,7 @@ paths:
tags:
- User
produces:
- abpplication/json
- application/json
responses:
"200":
description: successful
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment