Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aa31832
initial commit
kennychen-07 Apr 14, 2025
f5ef5e6
added dtb
kennychen-07 Apr 14, 2025
f632f3a
added item routes
kennychen-07 Apr 14, 2025
8ed9187
Made changes to the hope py template file
Aidan-Z-A Apr 15, 2025
e740b6c
Made changes to the home py template file
Aidan-Z-A Apr 15, 2025
c833d22
Merge branch 'main' of https://github.com/Aidan-Z-A/SwapSphere
Aidan-Z-A Apr 15, 2025
80915ed
Made the first page "buyer home"
Aidan-Z-A Apr 15, 2025
0a99be0
Created the seller "home" page
Aidan-Z-A Apr 15, 2025
5f87c72
Docstrings/created main home pages based off of the wireframes that w…
Aidan-Z-A Apr 15, 2025
103bfd2
moved the old pages into a file for clarity and so that I dont delete…
Aidan-Z-A Apr 15, 2025
6c773ae
added a buyer_routes file to the backend
Aidan-Z-A Apr 15, 2025
d357cf2
added and created some flask routes for seller_routes
Aidan-Z-A Apr 15, 2025
de310b7
Made more docstring notes and changed around the functions to reflect…
Aidan-Z-A Apr 15, 2025
cd3dc54
Merge branch 'main' of https://github.com/itsayaanpatel/SwapSphere
Aidan-Z-A Apr 15, 2025
be0c54f
added more notes and docstrings and changed functions to reflect thes…
Aidan-Z-A Apr 15, 2025
52b63ec
added 3 individual pages for buyer as well as routes that correspond
mitchmcnew Apr 15, 2025
c64fafb
added an add new items route
Aidan-Z-A Apr 15, 2025
f835a3c
Updated 3 pages and added corresponding routes
mitchmcnew Apr 15, 2025
c5556a6
added code to the 3 pages
mitchmcnew Apr 15, 2025
2acef3b
Mock Data SQL files
itsayaanpatel Apr 15, 2025
796da2d
Remove faulty trade and trade items
itsayaanpatel Apr 15, 2025
15ee511
Update Valid trade and trade items
itsayaanpatel Apr 15, 2025
8cf59e9
Removed Sample pre-existing sql files
itsayaanpatel Apr 15, 2025
99c9af6
fix: Remove duplicate import in __init__.py
itsayaanpatel Apr 16, 2025
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
6 changes: 0 additions & 6 deletions api/.env.template

This file was deleted.

2 changes: 2 additions & 0 deletions api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .items.routes import items_bp
app.register_blueprint(items_bp, url_prefix='/api')
Empty file.
37 changes: 37 additions & 0 deletions api/backend/cash_deals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Create new Blueprint object for negotiating cash deals
cash_deals = Blueprint('cash_deals', __name__)

# Get trade details and allow the user to propose a cash deal
@cash_deals.route('/negotiate_deal/<trade_id>', methods=['GET'])
def negotiate_cash_deal(trade_id):
cursor = db.get_db().cursor()
query = f'''
SELECT t.trade_id, t.proposer_id, t.receiver_id, t.status, t.fairness_score, t.cash_adjustment
FROM Trades t
WHERE t.trade_id = {trade_id}
'''
cursor.execute(query)

trade_data = cursor.fetchall()

response = make_response(jsonify(trade_data))
response.status_code = 200
return response

# Update the cash deal with a partial cash adjustment proposal
@cash_deals.route('/negotiate_deal/<trade_id>', methods=['PUT'])
def update_cash_deal(trade_id):
deal_info = request.json
cash_adjustment = deal_info['cash_adjustment']

query = f'''
UPDATE Trades
SET cash_adjustment = {cash_adjustment}
WHERE trade_id = {trade_id}
'''

cursor = db.get_db().cursor()
cursor.execute(query)
db.get_db().commit()

return 'Cash adjustment updated successfully!'
Empty file.
20 changes: 20 additions & 0 deletions api/backend/products/market_valuations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Create a new Blueprint object for market valuations
market_valuations = Blueprint('market_valuations', __name__)


# Get real-time market valuations for all items
@market_valuations.route('/market_valuations', methods=['GET'])
def get_market_valuations():
cursor = db.get_db().cursor()
query = '''
SELECT item_id, title, estimated_value, category
FROM Items
WHERE status = 'Available'
'''
cursor.execute(query)

valuations = cursor.fetchall()

response = make_response(jsonify(valuations))
response.status_code = 200
return response
147 changes: 147 additions & 0 deletions api/backend/sellers/seller_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""
seller_routes.py

endpoints:

1. GET /seller/items:
Retrieves all items listed by a seller. Expects a query parameter "seller_id" which
corresponds to the user_id in the Users table.

2. GET /seller/trade_history:
Retrieves the trade history for items offered by the seller. This endpoint joins the
Trades, Trade_Items, and Items tables to return trade details for items offered by the seller.

3. POST /seller/items:
Adds (uploads) a new item to the seller’s inventory. The required fields are seller_id,
title, description, category, and estimated_value. The status is set to 'Available' by default.

4. PUT /seller/items/<item_id>:
Updates the details for an existing item in the seller's inventory. Accepts updated
values for title, description, category, estimated_value, and optionally status.

5. DELETE /seller/items/<item_id>:
Deletes an item from the seller's inventory.

This blueprint is registered with the URL prefix "/seller". For instance, GET /seller/items
will return the items for the specified seller.

MORE Notes:
- The database schema uses: Users, Items, Trades, Trade_Items, etc.
- Sample data has been seeded by the provided SQL statements.
- The implementation uses one GET route for items, one GET route for trade history,
one POST route for adding an item, one PUT route for updating an item, and one DELETE
route for removing an item.
"""

from flask import Blueprint, request, jsonify, make_response, current_app
from backend.db_connection import db

# Create the Blueprint for seller routes.
seller = Blueprint('seller', __name__)

@seller.route('/items', methods=['GET'])
def get_seller_items():
"""
GET /seller/items

Retrieves all items listed by the seller.
Expects a query parameter 'seller_id' (which corresponds to the user_id in the Users table).

Returns:
A JSON response containing a list of items with the following fields:
- item_id
- title
- description
- category
- estimated_value
- status
- created_at
"""
seller_id = request.args.get('seller_id')
if not seller_id:
return make_response(jsonify({"error": "seller_id query parameter is required"}), 400)

query = """
SELECT item_id, title, description, category, estimated_value, status, created_at
FROM Items
WHERE user_id = %s
"""
cursor = db.get_db().cursor()
cursor.execute(query, (seller_id,))
items = cursor.fetchall()

return make_response(jsonify(items), 200)

@seller.route('/trade_history', methods=['GET'])
def get_trade_history():
"""
GET /seller/trade_history

Retrieves the trade history for items offered by the seller.
Expects a query parameter 'seller_id' (which is used as the offered_by field in Trade_Items).

Returns:
A JSON response containing a list of trades with the following fields:
- trade_id
- item_id
- title (from the Items table)
- status (from the Trades table)
- cash_adjustment (from the Trades table)
- created_at (from the Trades table)
"""
seller_id = request.args.get('seller_id')
if not seller_id:
return make_response(jsonify({"error": "seller_id query parameter is required"}), 400)

query = """
SELECT t.trade_id, i.item_id, i.title, t.status, t.cash_adjustment, t.created_at
FROM Trades t
JOIN Trade_Items ti ON t.trade_id = ti.trade_id
JOIN Items i ON ti.item_id = i.item_id
WHERE ti.offered_by = %s
ORDER BY t.created_at DESC
"""
cursor = db.get_db().cursor()
cursor.execute(query, (seller_id,))
trade_history = cursor.fetchall()

return make_response(jsonify(trade_history), 200)

@seller.route('/items', methods=['POST'])
def add_new_item():
"""
POST /seller/items

Adds a new item to the seller's inventory.
Expects a JSON payload with the following fields:
- seller_id: The ID of the seller (user_id from Users).
- title: The title of the item.
- description: A description of the item.
- category: The category to which the item belongs.
- estimated_value: The estimated value of the item.

Returns:
A JSON response confirming the item was added along with the new item_id.
"""
data = request.json
seller_id = data.get('seller_id')
title = data.get('title')
description = data.get('description')
category = data.get('category')
estimated_value = data.get('estimated_value')

if not (seller_id and title and estimated_value and category):
return make_response(jsonify({
"error": "Missing required fields (seller_id, title, estimated_value, category)"
}), 400)

query = """
INSERT INTO Items (user_id, title, description, category, estimated_value, status)
VALUES (%s, %s, %s, %s, %s, 'Available')
"""
cursor = db.get_db().cursor()
cursor.execute(query, (seller_id, title, description, category, estimated_value))
db.get_db().commit()
new_item_id = cursor.lastrowid

return make_response(jsonify({"message": "Item added successfully", "item_id": new_item_id}), 200)
59 changes: 59 additions & 0 deletions api/backend/trades/trade_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from flask import Blueprint, request, jsonify, make_response
from backend.db_connection import db

# Create a new Blueprint object for trades
trades = Blueprint('trades', __name__)

# Get trades for a specific user
@trades.route('/trades/<user_id>', methods=['GET'])
def get_user_trades(user_id):
cursor = db.get_db().cursor()
query = f'''
SELECT t.trade_id, t.proposer_id, t.receiver_id, t.status, t.fairness_score, t.cash_adjustment
FROM Trades t
WHERE t.proposer_id = {user_id} OR t.receiver_id = {user_id}
'''
cursor.execute(query)

trades_data = cursor.fetchall()

response = make_response(jsonify(trades_data))
response.status_code = 200
return response

# Get specific trade details by trade ID
@trades.route('/trades/details/<trade_id>', methods=['GET'])
def get_trade_details(trade_id):
cursor = db.get_db().cursor()
query = f'''
SELECT t.trade_id, t.proposer_id, t.receiver_id, t.status, t.fairness_score, t.cash_adjustment
FROM Trades t
WHERE t.trade_id = {trade_id}
'''
cursor.execute(query)

trade_data = cursor.fetchall()

response = make_response(jsonify(trade_data))
response.status_code = 200
return response

# Update trade status, fairness score, and cash adjustment
@trades.route('/trades/<trade_id>', methods=['PUT'])
def update_trade_status(trade_id):
trade_info = request.json
status = trade_info['status']
fairness_score = trade_info['fairness_score']
cash_adjustment = trade_info['cash_adjustment']

query = f'''
UPDATE Trades
SET status = '{status}', fairness_score = {fairness_score}, cash_adjustment = {cash_adjustment}
WHERE trade_id = {trade_id}
'''

cursor = db.get_db().cursor()
cursor.execute(query)
db.get_db().commit()

return 'Trade status updated successfully!'
Empty file added api/items/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions api/items/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from flask import Blueprint, jsonify
from api.models import db, Item

items_bp = Blueprint('items', __name__)

@items_bp.route('/items', methods=['GET'])
def get_items():
"""Implementation for [Jake-1], [Emma-1]"""
try:
items = Item.query.all()
return jsonify([{
'id': item.id,
'title': item.title,
'category': item.category,
'estimated_value': item.estimated_value
} for item in items]), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
Loading