From 4699fda79733706230cc4fd157f53ee0d15b8e5d Mon Sep 17 00:00:00 2001 From: maksim Date: Sat, 5 Oct 2024 23:40:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A4=D1=83=D1=85,=20=D0=B3=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=BE.=20=D0=9E=D1=81=D1=82=D0=B0=D0=BB=D0=BE=D1=81?= =?UTF-8?q?=D1=8C=20=D1=80=D0=B8=D0=B4=D0=BC=D0=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kashin_maxim_lab_3/faculty_service/app.py | 18 +++++++++++++++++- kashin_maxim_lab_3/faculty_service/db.py | 6 +++++- kashin_maxim_lab_3/faculty_service/init.sql | 11 +++++++++++ kashin_maxim_lab_3/faculty_service/service.py | 17 ++++++++++++++--- kashin_maxim_lab_3/university_service/app.py | 14 +++++++++++++- kashin_maxim_lab_3/university_service/db.py | 4 ++++ kashin_maxim_lab_3/university_service/init.sql | 15 ++++++++++++++- .../university_service/service.py | 14 +++++++++++++- 8 files changed, 91 insertions(+), 8 deletions(-) diff --git a/kashin_maxim_lab_3/faculty_service/app.py b/kashin_maxim_lab_3/faculty_service/app.py index 2b77b2e..4845397 100644 --- a/kashin_maxim_lab_3/faculty_service/app.py +++ b/kashin_maxim_lab_3/faculty_service/app.py @@ -15,11 +15,15 @@ def handle_faculties(): if request.method == 'POST': dto = request.get_json()['dto'] 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(faculty_service.get_all_faculties()) - @app.route('/faculties/', methods=['GET', 'PUT', 'DELETE']) def handle_faculty(faculty_id): if request.method == 'PUT': @@ -37,6 +41,18 @@ def handle_faculty(faculty_id): university = faculty_service.get_faculty(faculty_id) return jsonify({"id": university.id, "name": university.name, "university_id": university.university_id}) +@app.route('/faculties//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//universities/', 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__': app.run(host='0.0.0.0', use_reloader=False, port=8082) diff --git a/kashin_maxim_lab_3/faculty_service/db.py b/kashin_maxim_lab_3/faculty_service/db.py index ce7cbb2..23e6b0d 100644 --- a/kashin_maxim_lab_3/faculty_service/db.py +++ b/kashin_maxim_lab_3/faculty_service/db.py @@ -12,4 +12,8 @@ class Faculty(db.Model): "id": self.id, "name": self.name, "university_id": self.university_id - } \ No newline at end of file + } + +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) diff --git a/kashin_maxim_lab_3/faculty_service/init.sql b/kashin_maxim_lab_3/faculty_service/init.sql index d45c502..6a6319e 100644 --- a/kashin_maxim_lab_3/faculty_service/init.sql +++ b/kashin_maxim_lab_3/faculty_service/init.sql @@ -3,3 +3,14 @@ CREATE TABLE IF NOT EXISTS faculty ( name VARCHAR(100) 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) +); diff --git a/kashin_maxim_lab_3/faculty_service/service.py b/kashin_maxim_lab_3/faculty_service/service.py index 35bbd94..65d98db 100644 --- a/kashin_maxim_lab_3/faculty_service/service.py +++ b/kashin_maxim_lab_3/faculty_service/service.py @@ -1,4 +1,5 @@ -from db import db, Faculty +from db import db, Faculty, FacultyUniversity + class FacultyService: def get_all_faculties(self): @@ -11,7 +12,7 @@ class FacultyService: faculty = Faculty(name=name, university_id=university_id) db.session.add(faculty) db.session.commit() - return faculty.to_dict() + return faculty def update_faculty(self, faculty_id, name): faculty = self.get_faculty(faculty_id) @@ -24,5 +25,15 @@ class FacultyService: def delete_faculty(self, faculty_id): faculty = self.get_faculty(faculty_id) if faculty: - db.session.delete(faculty) + db.session.delete(Faculty.query.get(faculty_id)) 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() \ No newline at end of file diff --git a/kashin_maxim_lab_3/university_service/app.py b/kashin_maxim_lab_3/university_service/app.py index 9b64131..ccc9a72 100644 --- a/kashin_maxim_lab_3/university_service/app.py +++ b/kashin_maxim_lab_3/university_service/app.py @@ -15,7 +15,7 @@ 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}) + return jsonify(university) universities = university_service.get_all_universities() return jsonify(universities) @@ -37,6 +37,18 @@ def handle_university(university_id): 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) diff --git a/kashin_maxim_lab_3/university_service/db.py b/kashin_maxim_lab_3/university_service/db.py index ae44985..4c218b1 100644 --- a/kashin_maxim_lab_3/university_service/db.py +++ b/kashin_maxim_lab_3/university_service/db.py @@ -14,3 +14,7 @@ class University(db.Model): "name": self.name, "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) diff --git a/kashin_maxim_lab_3/university_service/init.sql b/kashin_maxim_lab_3/university_service/init.sql index c95328c..4bfd43b 100644 --- a/kashin_maxim_lab_3/university_service/init.sql +++ b/kashin_maxim_lab_3/university_service/init.sql @@ -1,4 +1,17 @@ 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, name VARCHAR(100) NOT NULL -); \ No newline at end of file +); + +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) +); diff --git a/kashin_maxim_lab_3/university_service/service.py b/kashin_maxim_lab_3/university_service/service.py index 9f55e03..2884c38 100644 --- a/kashin_maxim_lab_3/university_service/service.py +++ b/kashin_maxim_lab_3/university_service/service.py @@ -1,4 +1,5 @@ -from db import db, University +from db import db, University, FacultyUniversity + class UniversityService: def get_all_universities(self): @@ -28,3 +29,14 @@ class UniversityService: if university: db.session.delete(University.query.get(university_id)) 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() \ No newline at end of file