mirror of
https://github.com/gabehf/BudgetBuddy.git
synced 2026-03-07 21:48:14 -08:00
Merge branch 'main' of https://github.com/jacobmveber-01839764/BudgetBuddy
This commit is contained in:
commit
ab1db7dbae
8 changed files with 90 additions and 18 deletions
17
API.md
17
API.md
|
|
@ -248,3 +248,20 @@ Response:
|
|||
"decimal": int,
|
||||
}
|
||||
```
|
||||
## GET /income
|
||||
Get the total amount of income in each category
|
||||
|
||||
Response:
|
||||
```json
|
||||
{
|
||||
"status": int,
|
||||
"income_by_category": {
|
||||
"example_category": {
|
||||
"currency": string,
|
||||
"whole": int,
|
||||
"decimal": int,
|
||||
},
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -1,19 +1,31 @@
|
|||
import React, { useContext } from 'react';
|
||||
import { AppContext } from '../context/AppContext';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { getSessionKey } from "../utils/utils";
|
||||
import logo from './widget_logos/expenses_logo.png';
|
||||
import './css/ExpenseTotal.css'
|
||||
|
||||
const ExpenseTotal = () => {
|
||||
const { expenses } = useContext(AppContext);
|
||||
const [expense, setExpense] = useState(0.0);
|
||||
|
||||
const total = expenses.reduce((total, item) => {
|
||||
return (total += item.cost);
|
||||
}, 0);
|
||||
useEffect(() => {
|
||||
fetch('https://api.bb.gabefarrell.com/w/expenses/month', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'x-session-key': getSessionKey(),
|
||||
},
|
||||
})
|
||||
.then(data => data.json())
|
||||
.then((data) => {
|
||||
let monthExpense = data.whole
|
||||
monthExpense += (data.decimal / 100)
|
||||
setExpense(monthExpense)
|
||||
})
|
||||
.catch(console.log)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<div className='widget p-4'>
|
||||
<img src={logo}></img>
|
||||
<span>This Month's Expenses: ${total}</span>
|
||||
<img src={logo} className='expenseTotalIcon'></img>
|
||||
<span className='widgetText'>This Month's Expenses: ${expense}</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
.widgetText {
|
||||
padding-left: 1em;
|
||||
color: #333;
|
||||
}
|
||||
|
|
@ -84,9 +84,9 @@ export default function Login() {
|
|||
<p className="forgot-password">
|
||||
Don't have an account? <a href="/signup">sign up</a>
|
||||
</p>
|
||||
<p className="forgot-password">
|
||||
{/* <p className="forgot-password">
|
||||
Forgot <a href="#">password?</a>
|
||||
</p>
|
||||
</p> */}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -1,14 +1,11 @@
|
|||
export function checkLogin() {
|
||||
var cookies = document.cookie.split(';');
|
||||
for (var i = 0; i < cookies.length; i++) {
|
||||
var cookie = cookies[i].trim();
|
||||
if (cookie.indexOf('session=') === 0) {
|
||||
if (getSessionKey() !== null) {
|
||||
// The "session" cookie exists
|
||||
return true;
|
||||
return true
|
||||
} else {
|
||||
// The "session" cookie doesn't exist
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// The "session" cookie doesn't exist
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getSessionKey() {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ import (
|
|||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type IBCResponse struct {
|
||||
Status int `json:"status"`
|
||||
IncomeByCategory map[string]money.Money `json:"income_by_category"`
|
||||
}
|
||||
|
||||
func GetMonthIncome(w http.ResponseWriter, r *http.Request) {
|
||||
// get session key from request
|
||||
session := r.Header.Get("x-session-key")
|
||||
|
|
@ -53,3 +58,39 @@ func GetMonthIncome(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
w.Write(ret)
|
||||
}
|
||||
|
||||
func IncomeByCategory(w http.ResponseWriter, r *http.Request) {
|
||||
// get session key from request
|
||||
session := r.Header.Get("x-session-key")
|
||||
|
||||
// get collection handle from db
|
||||
var userCollection = db.Client.Database("budgetbuddy").Collection("users")
|
||||
|
||||
var user = db.UserSchema{}
|
||||
|
||||
err := userCollection.FindOne(context.Background(), bson.D{primitive.E{Key: "session", Value: session}}).Decode(&user)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
fmt.Fprintf(w, "{\"error\":\"invalid session key\"}")
|
||||
return
|
||||
}
|
||||
|
||||
var response IBCResponse
|
||||
|
||||
response.IncomeByCategory = make(map[string]money.Money)
|
||||
for _, e := range user.Income {
|
||||
response.IncomeByCategory[e.Category] = money.Add(e.Amount, response.IncomeByCategory[e.Category])
|
||||
}
|
||||
|
||||
response.Status = 200
|
||||
|
||||
ret, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "{\"error\":\"problem marshalling response\"}")
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(ret)
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ func Router() *chi.Mux {
|
|||
|
||||
// income
|
||||
r.Get("/income/month", GetMonthIncome)
|
||||
r.Get("/income", IncomeByCategory)
|
||||
|
||||
return r
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue