Commit 9a20418b authored by Tobias Kuhn's avatar Tobias Kuhn
Browse files

update api with money transaction

parent 043e52cc
class Api::V1::MoneyTransactionsController < Api::V1::BaseController
before_action :set_money_transaction, only: %i[show edit update destroy]
def index
money_transactions = MoneyTransaction.all
render json: MoneyTransactionSerializer.new(money_transactions).serialized_json, status: :ok
end
def show
render json: MoneyTransactionSerializer.new(@money_transaction).serialized_json, status: :ok
end
def create
@money_transaction = MoneyTransaction.new(money_transaction_params)
if @money_transaction.save
render status: 201, json: MoneyTransactionSerializer.new(@money_transaction).serialized_json
else
render json: @money_transaction.errors, status: 422
end
end
def update
if @money_transaction.update(money_transaction_params)
render status: :ok, json: MoneyTransactionSerializer.new(@money_transaction).serializable_hash.to_json
else
render json: @money_transaction.errors, status: 422
end
end
def destroy
begin
@money_transaction.destroy
rescue ActiveRecord::RecordNotFound
render json: { "error": 'Money Transaction not found' }, status: :not_found
return
rescue ActiveRecord::InvalidForeignKey
render json: { "error": 'Money Transaction cannot be deleted' }, status: :forbidden
return
rescue StandardError
raise
rescue Exception
raise
end
render json: { head: :no_content }, status: 204
end
private
def set_money_transaction
@money_transaction = MoneyTransaction.find(params[:id])
end
def money_transaction_params
params.require(:money_transaction).permit(:creditor_id, :debitor_id, :amount, :paid_at)
end
end
......@@ -3,7 +3,7 @@ class Api::V1::UsersController < Api::V1::BaseController
def index
users = User.all
render json: UserSerializer.new(users).serialized_json
render json: UserSerializer.new(users).serialized_json, status: :ok
end
def create
......@@ -16,7 +16,7 @@ class Api::V1::UsersController < Api::V1::BaseController
end
def show
render json: UserSerializer.new(@user).serialized_json
render json: UserSerializer.new(@user).serialized_json, status: :ok
end
def update
......
class MoneyTransactionSerializer
include FastJsonapi::ObjectSerializer
attributes :amount, :paid_at
belongs_to :creditor, redord_type: :user
belongs_to :debitor, redord_type: :user
end
\ No newline at end of file
......@@ -11,7 +11,7 @@ development:
pool: 5
timeout: 5000
username: postgres
password: admin
password: tobias
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
......@@ -23,8 +23,7 @@ test:
pool: 5
timeout: 5000
username: postgres
password: admin
password: tobias
production:
url: <%= ENV["DATABASE_URL"] %>
......@@ -13,6 +13,7 @@ Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users, only: [:index, :create, :show, :update, :destroy]
resources :money_transactions, only: [:index, :create, :show, :update, :destroy]
end
end
end
......@@ -96,7 +96,276 @@ paths:
example: "good@hier.net"
"422":
description: password can't be blank, name can't exist, e-mail can't exist
"/api/v1/money_transactions":
get:
summary: list all the money transactions
tags:
- Money Transaction
produces:
- application/json
responses:
'200':
description: successful
schema:
type: object
properties:
data:
type: array
items:
type: object
properties:
id:
type: string
type:
type: string
attributes:
type: object
properties:
amount:
type: string
paid_at:
type: string
nullable: true
required:
- amount
- paid_at
relationships:
type: object
properties:
creditor:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
required:
- id
- type
required:
- data
debitor:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
required:
- id
- type
required:
- data
required:
- debitor
- creditor
required:
- id
- type
- attributes
- relationships
post:
summary: Creates a money transaction
tags:
- Money Transaction
consumes:
- application/json
produces:
- application/json
parameters:
- name: money_transaction
in: body
schema:
type: object
properties:
data:
type: object
properties:
type:
type: string
attributes:
type: object
properties:
amount:
type: string
paid_at:
type: string
nullable: true
required:
- amount
- paid_at
relationships:
type: object
properties:
creditor_id:
type: string
debitor_id:
type: string
required:
- debitor_id
- creditor_id
required:
- type
- attributes
- relationships
responses:
'201':
description: money transaction created
'422':
description: creditor and debitor must not be the same
"/api/v1/money_transactions/{id}":
get:
summary: show a money transaction
tags:
- Money Transaction
produces:
- application/json
parameters:
- name: id
in: path
type: string
required: true
responses:
'200':
description: successful
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
type: object
properties:
amount:
type: string
paid_at:
type: string
nullable: true
required:
- amount
- paid_at
relationships:
type: object
properties:
creditor:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
required:
- id
- type
required:
- data
debitor:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
required:
- id
- type
required:
- data
required:
- debitor
- creditor
required:
- id
- type
- attributes
- relationships
patch:
summary: Updates a money transaction
tags:
- Money Transaction
consumes:
- application/json
produces:
- application/json
parameters:
- name: id
in: path
type: string
required: true
- name: money_transaction
in: body
schema:
type: object
properties:
data:
type: object
properties:
id:
type: string
type:
type: string
attributes:
type: object
properties:
amount:
type: string
paid_at:
type: string
nullable: true
required:
- amount
- paid_at
relationships:
type: object
properties:
creditor_id:
type: string
debitor_id:
type: string
required:
- debitor_id
- creditor_id
required:
- id
- type
- attributes
- relationships
responses:
'200':
description: 'money transaction updated: Von 10 Dollar auf 5 Dollar'
delete:
summary: Deletes a money transaction
tags:
- Money Transaction
consumes:
- application/json
produces:
- application/json
parameters:
- name: id
in: path
type: string
required: true
responses:
'204':
description: money transaction updated
"/api/v1/users/{id}":
get:
summary: show user
......
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