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 ( -