From fb0a6c7dfd403fd0cd168bbaaae0a1547472d4b0 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Mon, 18 May 2020 11:35:34 +0200 Subject: [PATCH] Current sync --- README.md | 26 +--- app/controllers/money_moves_controller.rb | 2 +- app/models/homie.rb | 8 +- app/models/money_move.rb | 25 +++- client/src/cash/MakeMoneyMove.js | 134 +++++++++++------- client/src/homies/Homie.css | 0 client/src/homies/Homie.js | 55 +++++++ config/database.yml | 2 +- .../20190620200006_create_money_moves.rb | 3 +- db/schema.rb | 3 +- db/seeds.rb | 2 + 11 files changed, 170 insertions(+), 90 deletions(-) create mode 100644 client/src/homies/Homie.css create mode 100644 client/src/homies/Homie.js diff --git a/README.md b/README.md index 7db80e4..5e1b628 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,6 @@ # README -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... +## Running +docker run --name gangstadb -d -p 5432:5432 -e POSTGRES_PASSWORD=docker roraccounting +PORT=3001 bundle exec rails s +PORT=3000 yarn --cwd client start diff --git a/app/controllers/money_moves_controller.rb b/app/controllers/money_moves_controller.rb index fe8e3a8..c0002ed 100644 --- a/app/controllers/money_moves_controller.rb +++ b/app/controllers/money_moves_controller.rb @@ -4,7 +4,7 @@ class MoneyMovesController < ApplicationController end def create - money_move = MoneyMove.new(money_move_params) + money_move = MoneyMove.create_move(money_move_params) if money_move.save json_response(money_move) else diff --git a/app/models/homie.rb b/app/models/homie.rb index 81e6bfa..9fe94fa 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -1,14 +1,12 @@ class Homie < ApplicationRecord - has_many :money_moves_from, class_name: 'MoneyMove', foreign_key: :from_homie_id - has_many :money_moves_to, class_name: 'MoneyMove', foreign_key: :to_homie_id + has_many :money_moves def self.cash(importance) - got = Homie.all.joins(:money_moves_to).group(:id).order(:id).sum(:amount) - owe = Homie.all.joins(:money_moves_from).group(:id).order(:id).sum(:amount) + totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) result = [] Homie.where(["importance > ?", importance]).map do |homie| - total = got.fetch(homie.id, 0) - owe.fetch(homie.id,0) + total = totals.fetch(homie.id, 0) { homie: homie, amount: total } end end diff --git a/app/models/money_move.rb b/app/models/money_move.rb index f1b1a61..4f87be8 100644 --- a/app/models/money_move.rb +++ b/app/models/money_move.rb @@ -1,4 +1,25 @@ class MoneyMove < ApplicationRecord - belongs_to :from_homie, class_name: 'Homie' - belongs_to :to_homie, class_name: 'Homie' + belongs_to :homie + + def create_move(params) + common_params = { description: params[:description] } + + move_from = MoneyMove.new( + common_params + { + homie_id: params[:from_homie_id], + amount: -BigDecimal.new(params[:amount]) + } + ) + move_to = MoneyMove.new( + common_params + { + homie_id: params[:from_homie_id], + amount: BigDecimal.new(params[:amount]) + } + ) + + MoneyMove.transaction do + move_from.save! + move_to.save! + end + end end diff --git a/client/src/cash/MakeMoneyMove.js b/client/src/cash/MakeMoneyMove.js index d7fca83..2943a7c 100644 --- a/client/src/cash/MakeMoneyMove.js +++ b/client/src/cash/MakeMoneyMove.js @@ -1,68 +1,92 @@ import React, { useState, useEffect } from 'react'; -import { Modal, Select, TextInput, Collection, CollectionItem } from 'react-materialize'; +import {Icon, Select, TextInput, Button} from 'react-materialize'; import './Cash.css'; import axios from 'axios'; const MakeMoneyMove = (props) => { - const [selectedFrom, setSelectedFrom] = useState(null); + const [selectedFrom, setSelectedFrom] = useState(""); + const [selectedTo, setSelectedTo] = useState(""); + const [homiesCash, setHomiesCash] = useState([]); + const [amountToMove, setAmountToMove] = useState(null); + + useEffect( () => { + const getCashForHomies = async () => { + try { + const cash = await axios.get(`/homies/cash`); + setHomiesCash(cash.data); + } catch (e) { + console.log("Error fetching", e); + } + }; + getCashForHomies(); + }, []); + + const handleAmountChange = (e) => { + setAmountToMove(parseFloat(e.target.value)) + } + + const homieToOptionMapper = (homieCash) => { + const homie = homieCash.homie; + return ( + + ); + }; + + const homieOptions = homiesCash.map(homieToOptionMapper); + + const handleFromHomieChange = (e) => { + console.log("from homie", e.target.value); + setSelectedFrom(e.target.value); + }; + const filteredHomieCashes = homiesCash.filter( (homieCash) => homieCash.homie.id !== parseInt(selectedFrom) ); + const filteredHomieOptions = filteredHomieCashes.map(homieToOptionMapper); + + const handleToHomieChange = (e) => { + setSelectedTo(e.target.value); + } + + const handleSubmit = (e) => { + e.preventDefault(); + console.log("Submitting"); + } + + const formComplete = () => ( + selectedFrom !== "" && + selectedFrom !== selectedTo && + selectedTo !== "" && + amountToMove > 0 + ); return ( -
+
+

Make Money Move

- - - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - - - Alvin - + + + - + + + + +
+ +
+
); diff --git a/client/src/homies/Homie.css b/client/src/homies/Homie.css new file mode 100644 index 0000000..e69de29 diff --git a/client/src/homies/Homie.js b/client/src/homies/Homie.js new file mode 100644 index 0000000..94b207c --- /dev/null +++ b/client/src/homies/Homie.js @@ -0,0 +1,55 @@ +import React, { useState, useEffect } from 'react'; +import {Icon, Select, TextInput, Button} from 'react-materialize'; +import './Homie.css'; +import axios from 'axios'; + +const Homie = (props) => { + + const [homieName, setHomieName] = useState(""); + + const handleSubmit = (e) => { + e.preventDefault(); + console.log("Submitting"); + } + + const formComplete = () => ( + selectedFrom !== "" && + selectedFrom !== selectedTo && + selectedTo !== "" && + amountToMove > 0 + ); + +return ( +
+
+

Homie Data

+ + + + + + + + +
+ +
+ +
+); + +} + +export default Homie; diff --git a/config/database.yml b/config/database.yml index a11bfa2..e92de8d 100644 --- a/config/database.yml +++ b/config/database.yml @@ -23,7 +23,7 @@ default: &default development: <<: *default - username: "docker" + username: "postgres" password: "docker" host: localhost port: 5432 diff --git a/db/migrate/20190620200006_create_money_moves.rb b/db/migrate/20190620200006_create_money_moves.rb index 4b1f662..1855a17 100644 --- a/db/migrate/20190620200006_create_money_moves.rb +++ b/db/migrate/20190620200006_create_money_moves.rb @@ -3,8 +3,7 @@ class CreateMoneyMoves < ActiveRecord::Migration[5.2] 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.integer :homie_id, null: false t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index a8ef8b5..0a594d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -26,8 +26,7 @@ ActiveRecord::Schema.define(version: 2019_06_20_200006) do 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.integer "homie_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2a..cb67f38 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,5 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) +# +#