Repository begleitend zum Blockseminar
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from flask import Flask, request, jsonify, render_template
  2. import mysql.connector
  3. app = Flask(__name__)
  4. def employee_data():
  5. # Connect to MySQL database
  6. conn = mysql.connector.connect(
  7. host='db', # Use the IP address or hostname of the MySQL container
  8. port='3306', # MySQL port
  9. user='root', # MySQL username
  10. password='root_password', # MySQL password
  11. database='employees' # Name of the database
  12. )
  13. # Create a cursor object to execute SQL commands
  14. cursor = conn.cursor()
  15. cursor.execute('SELECT Employee_Name, Title FROM employee_data')
  16. results = cursor.fetchall()
  17. cursor.close()
  18. conn.close()
  19. return results
  20. # Endpoint to add a new entry to the database
  21. @app.route('/add', methods=['POST'])
  22. def add_entry():
  23. # Connect to MySQL database
  24. conn = mysql.connector.connect(
  25. host='db', # Use the IP address or hostname of the MySQL container
  26. port='3306', # MySQL port
  27. user='root', # MySQL username
  28. password='root_password', # MySQL password
  29. database='employees' # Name of the database
  30. )
  31. # Create a cursor object to execute SQL commands
  32. cursor = conn.cursor()
  33. # Get data from request
  34. data = request.get_json()
  35. employee_name = data.get('employee_name')
  36. title = data.get('title')
  37. # SQL query to insert data into the employee_data table
  38. sql_query = "INSERT INTO employee_data (Employee_Name, Title) VALUES (%s, %s)"
  39. try:
  40. # Execute the SQL query
  41. cursor.execute(sql_query, (employee_name, title))
  42. # Commit the transaction
  43. conn.commit()
  44. cursor.close()
  45. conn.close()
  46. return jsonify({'message': 'Entry added successfully'}), 200
  47. except Exception as e:
  48. # Rollback in case of any error
  49. conn.rollback()
  50. cursor.close()
  51. conn.close()
  52. return jsonify({'error': str(e)}), 500
  53. # Endpoint to list all entries in the database
  54. @app.route('/entries', methods=['GET'])
  55. def get_entries():
  56. # Connect to MySQL database
  57. conn = mysql.connector.connect(
  58. host='db', # Use the IP address or hostname of the MySQL container
  59. port='3306', # MySQL port
  60. user='root', # MySQL username
  61. password='root_password', # MySQL password
  62. database='employees' # Name of the database
  63. )
  64. # Create a cursor object to execute SQL commands
  65. cursor = conn.cursor()
  66. # SQL query to select all entries from the employee_data table
  67. sql_query = "SELECT * FROM employee_data"
  68. # Execute the SQL query
  69. cursor.execute(sql_query)
  70. # Fetch all entries
  71. entries = cursor.fetchall()
  72. # Convert entries to JSON format
  73. entries_json = [{'Employee_Name': entry[0], 'Title': entry[1]} for entry in entries]
  74. cursor.close()
  75. conn.close()
  76. return jsonify(entries_json), 200
  77. @app.route('/')
  78. def index():
  79. #return jsonify({'Employee Data': employee_data()})
  80. entries = employee_data()
  81. return render_template('index.html', entries=entries)
  82. #return "Hello world"
  83. if __name__ == '__main__':
  84. #app.run(debug = True, host='0.0.0.0')
  85. app.run()