from flask import Flask, jsonify, request import sqlite3 import json app = Flask(__name__) # sqlite query function def query_db(sql_query): conn = sqlite3.connect('drive_records.db') cursor = conn.cursor() cursor.execute(sql_query) rows = cursor.fetchall() conn.close() return rows # Function to return all drive records in database def get_all_drive_records(): get_all_drives = "SELECT * FROM drive_records" rows = query_db(get_all_drives) drives = [] for row in rows: drive = { 'id': row[0], 'serial': row[1], 'model': row[2], 'flavor': row[3], 'capacity': row[4], 'TBW': row[5], 'smart': row[6] } drives.append(drive) return jsonify(drives) # Function to check if a serial number exists in the database def check_serial_exists(serial): return bool(query_db("SELECT * FROM drive_records WHERE serial=?", (serial,))) # Route to check if a serial number exists in the database @app.route('/check', methods=['GET']) def check(): serial_lookup = request.args.get('serial_lookup') if not serial_lookup: return jsonify({'error': 'No serial number provided'}), 400 exists = check_serial_exists(serial_lookup) return jsonify({'serial_number_exists': exists}) # Route to get all drive records in JSON format @app.route('/drives', methods=['GET']) def index(): return get_all_drive_records() if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)