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
86 changes: 86 additions & 0 deletions api/bookBazarBackend/admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# blueprints/admin.py
from flask import Blueprint, request, jsonify, current_app
from db import query, execute
import time

bp = Blueprint('admin', __name__, url_prefix='/admin')

# 1. View reported users (User Report Dashboard)
@bp.route('/reports/dashboard', methods=['GET'])
def reported_users_dashboard():
sql = """
SELECT user_id, COUNT(*) AS report_count
FROM user_reports
GROUP BY user_id
"""
try:
reports = query(sql)
return jsonify(reports)
except Exception as e:
return jsonify({'error': str(e)}), 500

# 2. View flagged listings for review
@bp.route('/flagged-listings', methods=['GET'])
def flagged_listings():
sql = 'SELECT * FROM listings WHERE flagged = 1'
try:
listings = query(sql)
return jsonify(listings)
except Exception as e:
return jsonify({'error': str(e)}), 500

# 3. Remove a flagged listing
@bp.route('/flagged-listings/<int:listingId>', methods=['DELETE'])
def remove_flagged_listing(listingId):
# This might mark the listing as removed rather than deleting from DB.
sql = 'UPDATE listings SET status = "removed" WHERE id = ?'
try:
execute(sql, [listingId])
return jsonify({'message': 'Flagged listing removed.'})
except Exception as e:
return jsonify({'error': str(e)}), 500

# 4. Check server health (uptime and performance)
@bp.route('/server/health', methods=['GET'])
def server_health():
uptime = time.time() - current_app.config.get('START_TIME', time.time())
return jsonify({'status': 'Server is running', 'uptime': uptime})

# 5. Ban a user who has multiple reports
@bp.route('/users/ban', methods=['POST'])
def ban_user():
data = request.get_json()
userId = data.get('userId')
if not userId:
return jsonify({'error': 'userId is required.'}), 400
sql = 'UPDATE users SET banned = 1 WHERE id = ?'
try:
execute(sql, [userId])
return jsonify({'message': 'User has been banned.'})
except Exception as e:
return jsonify({'error': str(e)}), 500

# 6. Detect spam or duplicate listings
@bp.route('/spam', methods=['GET'])
def detect_spam():
sql = """
SELECT title, COUNT(*) AS occurrence
FROM listings
GROUP BY title
HAVING occurrence > 1
"""
try:
spam = query(sql)
return jsonify(spam)
except Exception as e:
return jsonify({'error': str(e)}), 500

# 7. View system logs for diagnostics
@bp.route('/logs', methods=['GET'])
def view_logs():
sql = 'SELECT * FROM system_logs ORDER BY timestamp DESC'
try:
logs = query(sql)
return jsonify(logs)
except Exception as e:
return jsonify({'error': str(e)}), 500
Empty file added api/bookBazarBackend/buyer
Empty file.
Empty file added api/bookBazarBackend/seller
Empty file.
Empty file.
165 changes: 165 additions & 0 deletions database-files/BookBazarDDL.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
DROP DATABASE IF EXISTS TextbookMarketplace;
CREATE DATABASE IF NOT EXISTS TextbookMarketplace;
SHOW DATABASES;
USE TextbookMarketplace;

CREATE TABLE Users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
phone_number VARCHAR(15),
role ENUM('buyer', 'seller', 'admin', 'bookstore_manager') NOT NULL,
total_sales INT DEFAULT 0,
rating FLOAT DEFAULT 0.0
);

CREATE TABLE Textbooks (
book_id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
author VARCHAR(255),
isbn VARCHAR(20) UNIQUE,
class_code VARCHAR(20),
`condition` ENUM('new', 'like new', 'good', 'fair', 'poor')
);

CREATE TABLE Listings (
listing_id INT PRIMARY KEY AUTO_INCREMENT,
seller_id INT,
book_id INT,
price DECIMAL(10,2) NOT NULL,
status ENUM('active', 'sold', 'removed') DEFAULT 'active',
date_listed DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (seller_id) REFERENCES Users(user_id),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);

CREATE TABLE SalesTransactions (
transaction_id INT PRIMARY KEY AUTO_INCREMENT,
listing_id INT,
buyer_id INT,
seller_id INT,
date_purchased DATETIME DEFAULT CURRENT_TIMESTAMP,
buyer_rating FLOAT,
FOREIGN KEY (listing_id) REFERENCES Listings(listing_id),
FOREIGN KEY (buyer_id) REFERENCES Users(user_id),
FOREIGN KEY (seller_id) REFERENCES Users(user_id)
);

CREATE TABLE Reviews (
review_id INT PRIMARY KEY AUTO_INCREMENT,
buyer_id INT,
seller_id INT,
rating FLOAT NOT NULL,
comment TEXT,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (buyer_id) REFERENCES Users(user_id),
FOREIGN KEY (seller_id) REFERENCES Users(user_id)
);

CREATE TABLE Messages (
message_id INT PRIMARY KEY AUTO_INCREMENT,
listing_id INT,
buyer_id INT,
seller_id INT,
message_content TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (listing_id) REFERENCES Listings(listing_id),
FOREIGN KEY (buyer_id) REFERENCES Users(user_id),
FOREIGN KEY (seller_id) REFERENCES Users(user_id)
);

CREATE TABLE Wishlist (
wishlist_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
book_id INT,
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);

CREATE TABLE PriceAlerts (
alert_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
book_id INT,
target_price DECIMAL(10,2),
FOREIGN KEY (user_id) REFERENCES Users(user_id),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);

CREATE TABLE BulkPricing (
bulk_id INT PRIMARY KEY AUTO_INCREMENT,
seller_id INT,
discount DECIMAL(5,2),
quantity INT,
FOREIGN KEY (seller_id) REFERENCES Users(user_id)
);

CREATE TABLE Promotions (
promo_id INT PRIMARY KEY AUTO_INCREMENT,
listing_id INT,
start_date DATETIME,
end_date DATETIME,
cost DECIMAL(10,2),
FOREIGN KEY (listing_id) REFERENCES Listings(listing_id)
);

CREATE TABLE ListingAnalytics (
analytics_id INT PRIMARY KEY AUTO_INCREMENT,
listing_id INT,
total_views INT DEFAULT 0,
clicks INT DEFAULT 0,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (listing_id) REFERENCES Listings(listing_id)
);

CREATE TABLE BookstoreAnalytics (
analytics_id INT PRIMARY KEY AUTO_INCREMENT,
book_id INT,
total_listings INT DEFAULT 0,
total_sales INT DEFAULT 0,
avg_resale_price DECIMAL(10,2),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);

CREATE TABLE SystemLogs (
log_id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
event_type VARCHAR(255),
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES Users(user_id)
);

CREATE TABLE Reports (
report_id INT PRIMARY KEY AUTO_INCREMENT,
reported_user_id INT,
reported_by_user_id INT,
reason TEXT,
report_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (reported_user_id) REFERENCES Users(user_id),
FOREIGN KEY (reported_by_user_id) REFERENCES Users(user_id)
);


CREATE TABLE ServerHealth (
server_id INT PRIMARY KEY AUTO_INCREMENT,
server_name VARCHAR(100),
uptime_percentage DECIMAL(5,2),
status ENUM('online', 'offline', 'maintenance'),
last_downtime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE CompetitorBenchmarking (
benchmark_id INT PRIMARY KEY AUTO_INCREMENT,
book_id INT,
bookstore_price DECIMAL(10,2),
student_resale_avg_price DECIMAL(10,2),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);


CREATE TABLE InventoryAlerts (
alert_id INT PRIMARY KEY AUTO_INCREMENT,
book_id INT,
stock_status ENUM('low', 'out of stock'),
FOREIGN KEY (book_id) REFERENCES Textbooks(book_id)
);
Loading