from flask import Flask, jsonify, request, Response from service import UniversityService from db import db app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://university_user:password@university_db:5432/universitydb' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) university_service = UniversityService() @app.route('/universities', methods=['GET', 'POST']) def handle_universities(): if request.method == 'POST': dto = request.get_json()['dto'] university = university_service.create_university(dto['name']) return jsonify(university) universities = university_service.get_all_universities() return jsonify(universities) @app.route('/universities/', methods=['GET', 'PUT', 'DELETE']) def handle_university(university_id): if request.method == 'PUT': dto = request.get_json()['dto'] updated_university = university_service.update_university(university_id, dto['name']) return jsonify(updated_university) if updated_university else Response(status=404) elif request.method == 'DELETE': university_service.delete_university(university_id) return Response(status=200) university = university_service.get_university(university_id) if university is None: return Response(status=404) return jsonify(university) @app.route('/universities//faculties', methods=['POST']) def add_faculty(university_id): dto = request.get_json() faculty_id = dto['faculty_id'] university_service.add_faculty_to_university(university_id, faculty_id) return Response(status=204) @app.route('/universities//faculties/', methods=['DELETE']) def remove_faculty(university_id, faculty_id): university_service.remove_faculty_from_university(university_id, faculty_id) return Response(status=204) if __name__ == '__main__': app.run(host='0.0.0.0', use_reloader=False, port=8081)