Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added backend/__pycache__/app.cpython-310.pyc
Binary file not shown.
Binary file added backend/__pycache__/database.cpython-310.pyc
Binary file not shown.
Binary file added backend/__pycache__/main.cpython-310.pyc
Binary file not shown.
Binary file added backend/__pycache__/models.cpython-310.pyc
Binary file not shown.
24 changes: 24 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import Flask, request, jsonify

app = Flask(__name__)

# This will store our integer value
current_value = 0

@app.route('/set_value', methods=['POST'])
def set_value():
global current_value
value = request.json.get('value')

if isinstance(value, int):
current_value = value
return jsonify({"message": "Value set successfully"}), 200
else:
return jsonify({"error": "Invalid input, please send an integer"}), 400

@app.route('/get_value', methods=['GET'])
def get_value():
return jsonify({"value": current_value})

if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0')
Binary file added backend/instance/points.db
Binary file not shown.
52 changes: 30 additions & 22 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
from fastapi import FastAPI, HTTPException, Depends
from typing import Annotated, List
from typing import List
from sqlalchemy.orm import Session
from pydantic import BaseModel
from database import SessionLocal, engine
import uvicorn
import models
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = [
'http://localhost:3000'
"http://localhost:3000", # Add your React Native app's origin
"exp://127.0.0.1:19000", # Expo Go development client
"exp://localhost:19000", # Expo Go development client
"exp://10.13.182.154:8081", # Replace with your Expo Go app's ID
]


app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_origins=origins,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*']
)

class PointsBase(BaseModel):
name:str
UCID:str
hashed_password:str
name: str
UCID: str
hashed_password: str
points: int

class PointsModel(PointsBase):
Expand All @@ -33,34 +36,39 @@ class PointsModel(PointsBase):
class Config:
from_attributes = True


def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

db_dependency = Depends(get_db)

db_dependency = Annotated[Session, Depends(get_db)]
models.Base.metadata.create_all(bind=engine)

@app.get("/")
def read_root():
return {"message": "Hello, FastAPI!"}
def sqlalchemy_model_to_dict(model):
return {column.name: getattr(model, column.name) for column in model.__table__.columns}

@app.post("/points/", response_model=PointsModel)
async def create_point(point: PointsBase, db: db_dependency):
db_point = models.Points(**point.model_dump())
async def create_point(point: PointsBase, db: Session = db_dependency):
point_dict = point.dict()
db_point = models.Points(**point_dict)
db.add(db_point)
db.commit()
db.refresh(db_point)
return db_point
# Convert the SQLAlchemy model instance to a dictionary
db_point_dict = sqlalchemy_model_to_dict(db_point)
return db_point_dict

@app.get("/points/by-min-score/", response_model=List[PointsModel])
async def read_points_by_min_score(db: Session = db_dependency, min_score: int = 0):
points = db.query(models.Points).filter(models.Points.points >= min_score).all()
return [sqlalchemy_model_to_dict(point) for point in points]

@app.get("/points/top-ten/", response_model=List[PointsModel])
async def read_top_ten_points(db: Session = db_dependency):
top_points = db.query(models.Points).order_by(models.Points.points.desc()).limit(10).all()
return [sqlalchemy_model_to_dict(point) for point in top_points]

@app.get("/points/",response_model=List[PointsModel])
async def read_points(db: db_dependency, skip: int = 0, limit: int = 100):
points = db.query(models.Points).offset(skip).limit(limit).all()
return points

#When you run with uvicorn main:app --reload
#Make sure to append /docs to url to get that nice screen to see all the stuff
Binary file modified backend/points.db
Binary file not shown.
38 changes: 0 additions & 38 deletions frontend/ecoscan/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,9 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
import { NavigationContainer,DarkTheme} from "@react-navigation/native";
import Tabs from './src/navigation/tabs';
import Login from './src/screens/LoginPage';
import api from './api'

const App = () => {

const [points,setPoints] = useState([]);
const [formData,setFormData] = useState({
name: '',
UCID: '',
hashed_password: '',
points: 0
});

const fetchPoints = async () => {
const response = await api.get('/points/');
setPoints(response.data)
};

useEffect(() => {
fetchPoints();
}, []);

const handleInputChange = (event) => {
const value = event.target.type === 'checkbox' ? event.target.checked : event.target.value;
setFormData({
...formData,
[event.target.name] : value,
});
};

const handleFormSubmit = async (event) => {
event.preventDefault(); //makes us only control submitting the form
await api.post('/points/',formData);
fetchPoints();
setFormData({
name: '',
UCID: '',
hashed_password: '',
points: 0
});
};

const customDarkTheme = {
...DarkTheme,
colors: {
Expand Down
7 changes: 0 additions & 7 deletions frontend/ecoscan/api.js

This file was deleted.

44 changes: 25 additions & 19 deletions frontend/ecoscan/src/components/LeaderBoardTable.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import { View, Text, FlatList } from 'react-native';
import tw from 'twrnc';
import leaderboardData from './leaderboard.json'; // Import the JSON file

const Leaderboard = ({ data }) => {
const renderItem = ({ item }) => (
<View style={tw`flex-row justify-between border-b border-gray-300 py-2`}>
<Text style={tw`flex-1`}>{item.rank}</Text>
<Text style={tw`flex-3`}>{item.name}</Text>
<Text style={tw`flex-1 text-right`}>{item.points}</Text>
</View>
);
const Leaderboard = () => {
const [data, setData] = useState([]);

useEffect(() => {
// const fetchData = async () => {
// try {
// const response = await fetch('http://127.0.0.1:8000/points/top-ten/');
// const jsonData = await response.json();
// setData(jsonData);
// } catch (error) {
// console.error('Error fetching data:', error);
// }
// };

// fetchData();
setData(leaderboardData);
}, []);

return (
<View style={tw`p-4`}>
<View style={tw`flex-row justify-between mb-2`}>
<Text style={tw`font-bold text-xl`}>Rank</Text>
<Text style={tw`font-bold text-xl`}>Name</Text>
<Text style={tw`font-bold text-xl`}>Points</Text>
</View>
<View>
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={(item) => item.id.toString()} // Assuming each item has a unique ID
keyExtractor={item => item.id.toString()}
renderItem={({ item }) => (
<Text>{item.name} - {item.points} Points</Text>
)}
/>
</View>
);
};

export default Leaderboard;
export default Leaderboard;
11 changes: 11 additions & 0 deletions frontend/ecoscan/src/components/leaderboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{"name": "Ethan", "UCID": "30942823", "hashed_password": "894uidjfafkdj8", "points": 60, "id": 2},
{"name": "Olivia", "UCID": "30942823", "hashed_password": "894uidjfafkdj8", "points": 60, "id": 1},
{"name": "Zach", "UCID": "30172354", "hashed_password": "1233", "points": 13, "id": 5},
{"name": "Zach", "UCID": "30172354", "hashed_password": "1233", "points": 13, "id": 4},
{"name": "Zach", "UCID": "30172354", "hashed_password": "1233", "points": 13, "id": 3},
{"name": "Jim", "UCID": "30172353", "hashed_password": "stasdfg", "points": 12, "id": 8},
{"name": "Jim", "UCID": "30172353", "hashed_password": "stasdfg", "points": 12, "id": 7},
{"name": "Jun", "UCID": "31231", "hashed_password": "asdfsa", "points": 12, "id": 6},
{"name": "Aiden", "UCID": "10123", "hashed_password": "string", "points": 3, "id": 9}
]
6 changes: 3 additions & 3 deletions frontend/ecoscan/src/screens/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {View, Text } from 'react-native';
import Header from '../components/Header';
import Stats from '../components/Stats';
import ProgressBar from '../components/ProgressBar';
import Pull from './exampleFetchApi';

import Leaderboard from '../components/LeaderBoardTable';
// Home screen component
const HomePage = () => {
return (
<View>

<Header name="Jun Chao" imageUrl="../assets/newpfp.jpg"/>
<Stats points="12"/>
<Pull/>
<ProgressBar points="0.9"/>
<Text style={{ fontSize: 24, textAlign: 'center', marginTop: 20 }}>Home </Text>
<Leaderboard/>
</View>

);
};

Expand Down
69 changes: 0 additions & 69 deletions frontend/ecoscan/src/screens/exampleFetchApi.js

This file was deleted.