forked from Alexey/DAS_2024_1
Фух, готово. Осталось ридми.
This commit is contained in:
parent
1e9bdf2806
commit
4699fda797
@ -15,11 +15,15 @@ def handle_faculties():
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
dto = request.get_json()['dto']
|
dto = request.get_json()['dto']
|
||||||
faculty = faculty_service.create_faculty(dto['name'], dto['university_id'])
|
faculty = faculty_service.create_faculty(dto['name'], dto['university_id'])
|
||||||
|
|
||||||
|
# Проверьте, является ли faculty словарем или объектом
|
||||||
|
if isinstance(faculty, dict):
|
||||||
|
return jsonify({"id": faculty['id'], "name": faculty['name'], "university_id": faculty['university_id']})
|
||||||
|
|
||||||
return jsonify({"id": faculty.id, "name": faculty.name, "university_id": faculty.university_id})
|
return jsonify({"id": faculty.id, "name": faculty.name, "university_id": faculty.university_id})
|
||||||
|
|
||||||
return jsonify(faculty_service.get_all_faculties())
|
return jsonify(faculty_service.get_all_faculties())
|
||||||
|
|
||||||
|
|
||||||
@app.route('/faculties/<int:faculty_id>', methods=['GET', 'PUT', 'DELETE'])
|
@app.route('/faculties/<int:faculty_id>', methods=['GET', 'PUT', 'DELETE'])
|
||||||
def handle_faculty(faculty_id):
|
def handle_faculty(faculty_id):
|
||||||
if request.method == 'PUT':
|
if request.method == 'PUT':
|
||||||
@ -37,6 +41,18 @@ def handle_faculty(faculty_id):
|
|||||||
university = faculty_service.get_faculty(faculty_id)
|
university = faculty_service.get_faculty(faculty_id)
|
||||||
return jsonify({"id": university.id, "name": university.name, "university_id": university.university_id})
|
return jsonify({"id": university.id, "name": university.name, "university_id": university.university_id})
|
||||||
|
|
||||||
|
@app.route('/faculties/<int:faculty_id>/universities', methods=['POST'])
|
||||||
|
def add_university(faculty_id):
|
||||||
|
dto = request.get_json()
|
||||||
|
university_id = dto['university_id']
|
||||||
|
faculty_service.add_university_to_faculty(faculty_id, university_id)
|
||||||
|
return Response(status=204)
|
||||||
|
|
||||||
|
@app.route('/faculties/<int:faculty_id>/universities/<int:university_id>', methods=['DELETE'])
|
||||||
|
def remove_university(faculty_id, university_id):
|
||||||
|
faculty_service.remove_university_from_faculty(faculty_id, university_id)
|
||||||
|
return Response(status=204)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', use_reloader=False, port=8082)
|
app.run(host='0.0.0.0', use_reloader=False, port=8082)
|
||||||
|
@ -12,4 +12,8 @@ class Faculty(db.Model):
|
|||||||
"id": self.id,
|
"id": self.id,
|
||||||
"name": self.name,
|
"name": self.name,
|
||||||
"university_id": self.university_id
|
"university_id": self.university_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FacultyUniversity(db.Model):
|
||||||
|
faculty_id = db.Column(db.Integer, db.ForeignKey('faculty.id'), primary_key=True)
|
||||||
|
university_id = db.Column(db.Integer, db.ForeignKey('university.id'), primary_key=True)
|
||||||
|
@ -3,3 +3,14 @@ CREATE TABLE IF NOT EXISTS faculty (
|
|||||||
name VARCHAR(100) NOT NULL,
|
name VARCHAR(100) NOT NULL,
|
||||||
university_id INT NOT NULL
|
university_id INT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS university (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS faculty_university (
|
||||||
|
faculty_id INT REFERENCES faculty(id),
|
||||||
|
university_id INT REFERENCES university(id),
|
||||||
|
PRIMARY KEY (faculty_id, university_id)
|
||||||
|
);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from db import db, Faculty
|
from db import db, Faculty, FacultyUniversity
|
||||||
|
|
||||||
|
|
||||||
class FacultyService:
|
class FacultyService:
|
||||||
def get_all_faculties(self):
|
def get_all_faculties(self):
|
||||||
@ -11,7 +12,7 @@ class FacultyService:
|
|||||||
faculty = Faculty(name=name, university_id=university_id)
|
faculty = Faculty(name=name, university_id=university_id)
|
||||||
db.session.add(faculty)
|
db.session.add(faculty)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return faculty.to_dict()
|
return faculty
|
||||||
|
|
||||||
def update_faculty(self, faculty_id, name):
|
def update_faculty(self, faculty_id, name):
|
||||||
faculty = self.get_faculty(faculty_id)
|
faculty = self.get_faculty(faculty_id)
|
||||||
@ -24,5 +25,15 @@ class FacultyService:
|
|||||||
def delete_faculty(self, faculty_id):
|
def delete_faculty(self, faculty_id):
|
||||||
faculty = self.get_faculty(faculty_id)
|
faculty = self.get_faculty(faculty_id)
|
||||||
if faculty:
|
if faculty:
|
||||||
db.session.delete(faculty)
|
db.session.delete(Faculty.query.get(faculty_id))
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
def add_university_to_faculty(self, faculty_id, university_id):
|
||||||
|
association = FacultyUniversity(faculty_id=faculty_id, university_id=university_id)
|
||||||
|
db.session.add(association)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
def remove_university_from_faculty(self, faculty_id, university_id):
|
||||||
|
association = FacultyUniversity.query.filter_by(faculty_id=faculty_id, university_id=university_id).first()
|
||||||
|
if association:
|
||||||
|
db.session.delete(association)
|
||||||
|
db.session.commit()
|
@ -15,7 +15,7 @@ def handle_universities():
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
dto = request.get_json()['dto']
|
dto = request.get_json()['dto']
|
||||||
university = university_service.create_university(dto['name'])
|
university = university_service.create_university(dto['name'])
|
||||||
return jsonify({"id": university.id, "name": university.name, "established_at": university.established_at})
|
return jsonify(university)
|
||||||
|
|
||||||
universities = university_service.get_all_universities()
|
universities = university_service.get_all_universities()
|
||||||
return jsonify(universities)
|
return jsonify(universities)
|
||||||
@ -37,6 +37,18 @@ def handle_university(university_id):
|
|||||||
return Response(status=404)
|
return Response(status=404)
|
||||||
return jsonify(university)
|
return jsonify(university)
|
||||||
|
|
||||||
|
@app.route('/universities/<int:university_id>/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/<int:university_id>/faculties/<int:faculty_id>', 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__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', use_reloader=False, port=8081)
|
app.run(host='0.0.0.0', use_reloader=False, port=8081)
|
||||||
|
@ -14,3 +14,7 @@ class University(db.Model):
|
|||||||
"name": self.name,
|
"name": self.name,
|
||||||
"established_at": self.established_at.isoformat()
|
"established_at": self.established_at.isoformat()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FacultyUniversity(db.Model):
|
||||||
|
faculty_id = db.Column(db.Integer, db.ForeignKey('faculty.id'), primary_key=True)
|
||||||
|
university_id = db.Column(db.Integer, db.ForeignKey('university.id'), primary_key=True)
|
||||||
|
@ -1,4 +1,17 @@
|
|||||||
CREATE TABLE IF NOT EXISTS university (
|
CREATE TABLE IF NOT EXISTS university (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
name VARCHAR(100) NOT NULL,
|
||||||
|
established_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS faculty (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL
|
name VARCHAR(100) NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS faculty_university (
|
||||||
|
faculty_id INT REFERENCES faculty(id),
|
||||||
|
university_id INT REFERENCES university(id),
|
||||||
|
PRIMARY KEY (faculty_id, university_id)
|
||||||
|
);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from db import db, University
|
from db import db, University, FacultyUniversity
|
||||||
|
|
||||||
|
|
||||||
class UniversityService:
|
class UniversityService:
|
||||||
def get_all_universities(self):
|
def get_all_universities(self):
|
||||||
@ -28,3 +29,14 @@ class UniversityService:
|
|||||||
if university:
|
if university:
|
||||||
db.session.delete(University.query.get(university_id))
|
db.session.delete(University.query.get(university_id))
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
def add_faculty_to_university(self, university_id, faculty_id):
|
||||||
|
association = FacultyUniversity(university_id=university_id, faculty_id=faculty_id)
|
||||||
|
db.session.add(association)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
def remove_faculty_from_university(self, university_id, faculty_id):
|
||||||
|
association = FacultyUniversity.query.filter_by(university_id=university_id, faculty_id=faculty_id).first()
|
||||||
|
if association:
|
||||||
|
db.session.delete(association)
|
||||||
|
db.session.commit()
|
Loading…
Reference in New Issue
Block a user