From 877f0425d03a3699c3ed403facd1c69098b89105 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Thu, 20 Jun 2019 22:47:39 +0200 Subject: [PATCH] Homies and money moves --- app/controllers/homies_controller.rb | 2 ++ app/controllers/money_moves_controller.rb | 23 ++++++++++++ app/models/concerns/error_response.rb | 0 app/models/concerns/json_response.rb | 5 +++ app/models/homie.rb | 4 +++ app/models/money_move.rb | 4 +++ config/database.yml | 8 ++--- config/routes.rb | 2 ++ db/migrate/20190620194110_create_homies.rb | 10 ++++++ .../20190620200006_create_money_moves.rb | 11 ++++++ db/schema.rb | 35 +++++++++++++++++++ test/controllers/homies_controller_test.rb | 7 ++++ .../money_moves_controller_test.rb | 7 ++++ test/fixtures/homies.yml | 9 +++++ test/fixtures/money_moves.yml | 13 +++++++ test/models/homie_test.rb | 7 ++++ test/models/money_move_test.rb | 7 ++++ 17 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 app/controllers/homies_controller.rb create mode 100644 app/controllers/money_moves_controller.rb create mode 100644 app/models/concerns/error_response.rb create mode 100644 app/models/concerns/json_response.rb create mode 100644 app/models/homie.rb create mode 100644 app/models/money_move.rb create mode 100644 db/migrate/20190620194110_create_homies.rb create mode 100644 db/migrate/20190620200006_create_money_moves.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/homies_controller_test.rb create mode 100644 test/controllers/money_moves_controller_test.rb create mode 100644 test/fixtures/homies.yml create mode 100644 test/fixtures/money_moves.yml create mode 100644 test/models/homie_test.rb create mode 100644 test/models/money_move_test.rb diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb new file mode 100644 index 0000000..33e6db4 --- /dev/null +++ b/app/controllers/homies_controller.rb @@ -0,0 +1,2 @@ +class HomiesController < ApplicationController::API +end diff --git a/app/controllers/money_moves_controller.rb b/app/controllers/money_moves_controller.rb new file mode 100644 index 0000000..2218cf2 --- /dev/null +++ b/app/controllers/money_moves_controller.rb @@ -0,0 +1,23 @@ +class MoneyMovesController < ApplicationController::API + def list + json_response(MoneyMove.find(:all).order(:created_at)) + end + + def create + money_move = MoneyMove.new(money_move_params) + if money_move.save + json_response(money_move) + else + error_response(:bad_request) + end + end + + def money_move_params + params.require(:money_moves).permit( + :description, + :amount, + :from_homie_id, + :to_homie_id + ) + end +end diff --git a/app/models/concerns/error_response.rb b/app/models/concerns/error_response.rb new file mode 100644 index 0000000..e69de29 diff --git a/app/models/concerns/json_response.rb b/app/models/concerns/json_response.rb new file mode 100644 index 0000000..cfb0caa --- /dev/null +++ b/app/models/concerns/json_response.rb @@ -0,0 +1,5 @@ +module Response + def json_response(object, status = :ok) + render json: object, status: status + end +end diff --git a/app/models/homie.rb b/app/models/homie.rb new file mode 100644 index 0000000..d4af353 --- /dev/null +++ b/app/models/homie.rb @@ -0,0 +1,4 @@ +class Homie < ApplicationRecord + has_many :money_moves_from, class_name: 'MoneyMove', column: :from_homie_id + has_many :money_moves_to, class_name: 'MoneyMove', column: :to_homie_id +end diff --git a/app/models/money_move.rb b/app/models/money_move.rb new file mode 100644 index 0000000..f1b1a61 --- /dev/null +++ b/app/models/money_move.rb @@ -0,0 +1,4 @@ +class MoneyMove < ApplicationRecord + belongs_to :from_homie, class_name: 'Homie' + belongs_to :to_homie, class_name: 'Homie' +end diff --git a/config/database.yml b/config/database.yml index 7436e50..a11bfa2 100644 --- a/config/database.yml +++ b/config/database.yml @@ -23,10 +23,10 @@ default: &default development: <<: *default - username: "docker", - password: "docker", - host: localhost, - port: 5431, + username: "docker" + password: "docker" + host: localhost + port: 5432 database: roraccounting_development # The specified database role being used to connect to postgres. diff --git a/config/routes.rb b/config/routes.rb index 787824f..cfbefac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ Rails.application.routes.draw do + resources :money_moves + resources :homies # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20190620194110_create_homies.rb b/db/migrate/20190620194110_create_homies.rb new file mode 100644 index 0000000..e1fb29f --- /dev/null +++ b/db/migrate/20190620194110_create_homies.rb @@ -0,0 +1,10 @@ +class CreateHomies < ActiveRecord::Migration[5.2] + def change + create_table :homies do |t| + t.text :name, null: false + t.integer :importance, null: false, default: 5 + t.text :about + t.timestamps + end + end +end diff --git a/db/migrate/20190620200006_create_money_moves.rb b/db/migrate/20190620200006_create_money_moves.rb new file mode 100644 index 0000000..4b1f662 --- /dev/null +++ b/db/migrate/20190620200006_create_money_moves.rb @@ -0,0 +1,11 @@ +class CreateMoneyMoves < ActiveRecord::Migration[5.2] + def change + create_table :money_moves do |t| + t.text :description + t.decimal :amount, precision: 12, scale: 3 + t.integer :from_homie_id, null: false + t.integer :to_homie_id, null: false + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..a8ef8b5 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,35 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2019_06_20_200006) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "homies", force: :cascade do |t| + t.text "name", null: false + t.integer "importance", default: 5, null: false + t.text "about" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "money_moves", force: :cascade do |t| + t.text "description" + t.decimal "amount", precision: 12, scale: 3 + t.integer "from_homie_id", null: false + t.integer "to_homie_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/homies_controller_test.rb b/test/controllers/homies_controller_test.rb new file mode 100644 index 0000000..db62ccb --- /dev/null +++ b/test/controllers/homies_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HomiesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/controllers/money_moves_controller_test.rb b/test/controllers/money_moves_controller_test.rb new file mode 100644 index 0000000..129af8e --- /dev/null +++ b/test/controllers/money_moves_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MoneyMovesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/homies.yml b/test/fixtures/homies.yml new file mode 100644 index 0000000..ca81a8f --- /dev/null +++ b/test/fixtures/homies.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyText + importance: 1 + +two: + name: MyText + importance: 1 diff --git a/test/fixtures/money_moves.yml b/test/fixtures/money_moves.yml new file mode 100644 index 0000000..20984ee --- /dev/null +++ b/test/fixtures/money_moves.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + description: MyText + amount: 9.99 + from_homie: one + to_homie: one + +two: + description: MyText + amount: 9.99 + from_homie: two + to_homie: two diff --git a/test/models/homie_test.rb b/test/models/homie_test.rb new file mode 100644 index 0000000..bd8c17a --- /dev/null +++ b/test/models/homie_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HomieTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/money_move_test.rb b/test/models/money_move_test.rb new file mode 100644 index 0000000..cb7105f --- /dev/null +++ b/test/models/money_move_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MoneyMoveTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end