forked from Alexey/DAS_2024_1
43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
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({"id": university.id, "name": university.name, "established_at": university.established_at})
|
|
|
|
universities = university_service.get_all_universities()
|
|
return jsonify(universities)
|
|
|
|
|
|
@app.route('/universities/<int:university_id>', 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)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', use_reloader=False, port=8081)
|